ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#!/bin/bash
# stop all instance of this script if "killall" provided as first argument
if [ "$1" == "killall" ]; then
scriptname=$(basename "$0")
pattern="[0-9]* /bin/bash .*$scriptname$"
pids=$(ps ax -o pid,cmd | grep -P "$pattern" | sed 's/^ *//;s/ *$//' | grep -Pv ' grep|killall$' | cut -d" " -f1)
if [ "$pids" != "" ]; then
kill -TERM $pids
echo "$(echo '$pids' | wc -l) instances stopped"
else
echo "None found to stop"
fi
exit 0
fi
# ask for window
echo -en "\nClick the window you want to set its title "
id=$(printf %i $(xwininfo | grep 'Window id' | cut -d" " -f4))
# fail if no window id
if [ "$id" == "" ]; then
echo 'Error: Window id not found'
exit 1
else
echo "- Got it"
fi
# ask for title
read -e -p "Enter target title: " title
# fail if no title
if [ "$title" == "" ]; then
echo "Error: No title to set"
exit 1
fi
# define loop as a function, so we can run it in background
windowByIdSetStickyTitle() {
local id title curr_title
id="$1"
title="$2"
while true; do
# get current title
curr_title="$(xdotool getwindowname $id 2>/dev/null)"
# exit if we can't find window anymore
if [ $? -ne 0 ]; then
echo "Window id does not exist anymore"
break
fi
# update title if changed
if [ "$curr_title" != "$title" ]; then
xdotool set_window --name "$title" $id
fi
# needed else you will eat up a significant amount of cpu
sleep 3
done
}
# infinite loop
windowByIdSetStickyTitle $id "$title" &
# done
echo "Sticky title set"
exit 0
It works wonderfully, but rather than always filling in the two script prompts ("Click the window you want to set its title" and "Enter target title:"), I''d like to automatically insert Chromium's initial window title ("Google - Chromium" no quotes) and rename it "." (period, no quotes) every time the script is run.
Could I avail on veteran scripters to insert this automation in this fine script?
Many thanks,
Jake
Last edited by astrogeek; 02-21-2019 at 01:16 PM.
Reason: Added CODE tags
Can you go and edit your original post to add [code] [/code] tags around the script you found? It will be much easier to read that way since it will preserve the spacing.
Since I'm quite new at this, could I ask you to be a little more specific?
I see several $1's, perhaps not all pertaining to exact window, and of course some $2's need replacing as well.
To enable $1=Google - Chromium and $2=. --how would I go about that?
Thanks again for posting,
Jake
I suppose I can admit a little guilt with not reading every line of the script.
OK. First.
Seems to me that you grabbed a script and you're running it. But meanwhile you have no experience with writing scripts. Is this a true of false assumption?
Yes there are plenty of people here who can write full scripts, debug anything in that script, or edit it for you. Count me as not the person to do exactly that. But I will explain my inputs better.
What I can do is explain better how to edit and what I was thinking. Otherwise I feel it would be beneficial for you to look up some references on bash and learn a bit about it, especially if you plan to tinker with it.
$1, $2, $3 are special variables for bash that represent the passing arguments of the script, if any. They don't always exist. $# is a special variable too. The $# tells you 'how many arguments someone called a script with'.
I do see the $1's being used and they are for a killall command. Well, do you, or don't you need that part at all? I haven't checked it much to determine it.
Later in the script you see the line:
Code:
read -e -p "Enter target title: " title
That line is reading input from the user to take in the string for the window name. And it assigns whatever you type in to the variable named 'title'. You reference that variable with the notation $title. You also can assign it to a passing argument variable with something like this:
Code:
title=$1
or:
Code:
title=$2
These are choices you can make when modifying that code. You can even then print it back out to ensure you've gotten it correctly:
Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.
I have done that for you this time, welcome to the Programming forum!
Another part that I haven't covered too well is identifying the window.
In that script the mouse click is used to get the ID of the window and it saves it to the variable $id.
This is used in the while loop to identify the window using that id for the command identified by scasey.
Seems you always wish to rename the window with "." to shorten it to nearly nothing.
You can modify that line to change the name without needing to read in any name.
Meanwhile I'm hard pressed to determine how you avoid the click. That is how the script chooses what window you wish to rename.
My take is that the OP only wants to rename the window that the click would identify with
"Google - Chromium"...if that's the case, they (should) only need to replace the $title in
Code:
xdotool set_window --name "$title" $id
with that string, and the $id with the . (dot)
But, in perusing the man page for xdotool, it appears that set_window --name wants the window ID, not the name being displayed, so a modification to my suggestion:
Code:
# ask for window
echo -en "\nClick the window you want to set its title "
id=$(printf %i $(xwininfo | grep 'Window id' | cut -d" " -f4))
# change the title
xdotool set_window --name "$id" .
Yes, I don't either see a way to avoid the click.
It's not clear to me why the script needs to loop...why it just can't be those two lines.
I think scasey has it fine in their last rendition.
And I think the killall stuff is just part of some junk that had other purposes beyond what they grabbed this script for. Or grabbed this chunk of script for. Poorly written script, has a forever loop and this is potentially why it has a killall.
The danger would be what my suspicion is, is that the OP just grabbed something and "it works!", but they have no idea about anything else in the script.
I think scasey has it fine in their last rendition.
And I think the killall stuff is just part of some junk that had other purposes beyond what they grabbed this script for. Or grabbed this chunk of script for. Poorly written script, has a forever loop and this is potentially why it has a killall.
The danger would be what my suspicion is, is that the OP just grabbed something and "it works!", but they have no idea about anything else in the script.
Thank you.
I agree, the script is very strange. I also agree that the OP doesn't understand what they have and would add to your implicit caution about running scripts when on doesn't know what, exactly they do.
I'd add the same caution about explicitly following advice posted here, including mine. No one is perfect.
You've certainly done a good job of explaining to the OP what the various piece parts of the script mean. Kudos for that.
Wow, I can not thank you all enough. I stepped away from the computer for an hour and I returned deluged with help, all the way to a too-kind moderator who corrected the syntax of my original post. I'm going to put together all the advice now and will post back with results, but I first wanted to express my gratitude.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.