How do I permanently change window titles?
The initial name is usually set by the (code within) the application itself. You can however change it, e.g. by using xdotool
, which you would have to install first:
sudo apt-get install xdotool
Then, once it is installed, you can set another window name by (e.g.) the command:
xdotool search --name "Unity LauncherSwitcher" set_window --name "Monkey"
This will change:
into:
Note
As you can see, the command exists of:
xdotool search --name "<current_name>" set_window --name "<new_name>"
The <current_name>
has to match exactly.
Edit alternatively
From your comment, I understand the command above did not work. I tested it with different applications, and it worked...
But for a reason I do not understand, NOT with Rhytmbox
(!! might be a bug)
An alternative is to use wmctrl
, which you also would have to install:
sudo apt-get install wmctrl
Then use the command:
xprop -id "$(wmctrl -l | grep 'Rhythmbox' | awk '{ print $1 }')" -set WM_NAME "Music"
I succesfully tested it on Rhythmbox
:
Make the changes permanent?
As mentioned, the default window name is defined in the code of the application.
Changing the default window title would need to change the code. That can be done if the code is available, would need recompiling in many cases and depends on the used language, among other things. A general instruction would be quite impossible and beyond (outside) the scope of AU in my opinion.
EDIT 2
Flexibly manage/set custom window names per application from a single file
In your original question, you were wondering if there was some kind of a file to set the title of new windows of a specific application. As explained, that is not the case, however, it can be created.
In the setup below, you can very easily define on a per application base how new windows are named. Simply create a file in ~
(your home directory), named window_names.txt
. For each of the applications you want to set a specific windowname for, add a line of, subsequenty, the application and the desired window name:
A textfile, defining window names per application
gnome-terminal Monkey eats
gedit Banana
rhythmbox if he runs out of peanuts
Explanation
The setup exists of a simple background script. The script is very light, so it won't have any noticable effect on the performance whatsoever.
Once the script starts, it reads the file ~/window_names.txt
and loads the settings per application. Then it keeps an eye on newly created windows. If a window appears that belongs to one of the applications, definied in the file, it sets the window name accordingly.
How to set up
-
The script uses both
wmctrl
andxdotool
:sudo apt-get install wmctrl sudo apt-get install xdotool
Copy the script into an empty file save it as
setwindowname.py
-
Create the file
~/window_names.txt
(exactly namedwindow_names.txt
), add your applications in the format:<application> <window_name>
e.g.
gedit Text editor
The window name may include spaces.
-
Test-run the script from the terminal by the command:
python3 /path/to/setwindowname.py
-
If all works as expected, add it to startup applications: Dash > Startup Applications > Add
Note that in some cases, a script might break if it starts when the desktop is not fully loaded yet. If that migh be the case, the command to add to startup applications would be:
/bin/bash -c "sleep 15&&python3 /path/to/setwindowname.py"
The script
#!/usr/bin/env python3
import subprocess
import time
import os
f = os.environ["HOME"]+"/"+"window_names.txt"
change = []
lines = open(f).read().splitlines()
for l in lines:
try:
change.append([l.split()[0], (" ").join(l.split()[1:])])
except IndexError:
pass
get = lambda cmd: subprocess.check_output(cmd).decode("utf-8").strip()
curr_1 = []
while True:
try:
curr_2 = get(["wmctrl", "-lp"]).splitlines()
new = [w for w in curr_2 if not w in curr_1]
for item in new:
line = item.split(); pid = line[2]
procs = get(["ps", "-e"]).splitlines()
match = [l for l in procs if pid in l][0]
for app in [app for app in change if app[0] in match]:
subprocess.Popen(["xdotool", "set_window", "--name", app[1], line[0]])
curr_1 = curr_2
time.sleep(0.3)
except:
pass
Notes
- If the file
~/window_names.txt
is edited, the script needs to be restarted. - It works no matter how an application is started; it also works when applications are run from the terminal / and or with
sudo
. - It does not interefere with the the right-click option of any application.
- The script also "maintains" the window name; if another process changes the window name afterwards (opening tabs, change directories e.g.) the script sees the window as a "new window" and maintains the window name of your choice.
The title is generated by the application executable and there isn't a easy way to edit it.
Changing the title of a application once it's open:
- Install
xdotool
withsudo apt-get install xdotool
- Get the pid of the application with
pgrep myapp
(i.e.pgrep rythmbox
) -
Change the window title (replacing
HERE_THE_PID
with the process id of the application):xdotool search --onlyvisible --pid HERE_THE_PID --name "\a\b\c" set_window --name "$new_name"
Making a script to launch any application with a custom title:
-
Create a bash script called
change-title
with these contents:#!/bin/bash ## The new name of the window is the first argument passed to the script: new_name="${1}" ## application is all arguments but the first (the title) application=${@:2} ## Run the app in background and in quiet mode: $application &> /dev/null & ## Get the pid of the app: app_pid=$! ## Wait until the window appears: until xdotool search --onlyvisible --pid $app_pid &> /dev/null; do sleep 0.01; done ## Change the window name: xdotool \ search \ --onlyvisible \ --pid $app_pid \ --name "\a\b\c" \ set_window \ --name "$new_name" ## Comment this line to run the app in background: wait $app_pid
Make it executable with
chmod a+x change-title
- Run it with
./change-title "NewTitle" theapp --its-arguments
To make the change permanent:
- Run
gksudo gedit /usr/local/bin/custom-title
(fx. user/local/bin/music) - On the text editor paste the above script and save it.
- Run
sudo chmod a+x /usr/local/bin/custom-title
- Run
gksudo gedit /usr/share/applications/rhythmbox.desktop
-
On this file, there is a line (the line number is 35 on mine) that says:
Exec=rhythmbox %U
-
Replace it with this with the name of the created file:
Exec=custom title (fx. Exec=music)
- Save the file as a new custom-name.desktop fx. music.desktop. Be careful that you do not overwrite the original file!
To make the change effective for the "open with" function
- Run
gksudo gedit /usr/share/applications/rhythmbox-device.desktop
- Change
rhythmbox
to the name used in the previous .desktop file. - Save the file.
- Kill active processes of rhythmbox with system monitor or with
sudo kill rhythmbox
- Plugin your phone/music player and press "Open with (Music application)" to see if it worked.
(Summary) Overview of the files you should have
To make the application fully functional, you should have 3 .desktop files in /usr/share/applications
when finished:
-
music.desktop
or the name you gave the applicationwith
Exec=music
andHidden=true
(add the last one if not present) -
rhythmbox.desktop
with
Exec=rhythmbox %U
andHidden=true
-
rhythmbox-device.desktop
with
Exec=music --select-source %U
and WITHOUT the Hidden entry
This means that you always open the last file and the title is always as it should. The only way is resets itself is when you refresh your session with ALT+F2+R.
- And you should have the bash script in
/usr/local/bin
ofcourse.
Final notes:
- When I'm using
--name "\a\b\c"
is to fix a bug, don't replace with the application name! - If do you uncomment the last line of the script, the script will not end until the app is closed.
-
Explaination of
${@:2}
in the code requested by @Maud Kon:-
$@
is an array that stores all the positional parameters. -
${@:2}
means: All the positional parameters but the first -
Let's say that I call a program this way:
awesome-program foo bar baz fooz
-
${@:1}
would befoo bar baz fooz
-
${@:2}
would bebar baz fooz
-
${@:3}
would bebaz fooz
-
${@:4}
would befooz
-
${@:5}
is empty because there aren't more positional parameters.
-
$@
,${@}
and${@:1}
are the same thing: All the positional parameters.- Learn more about this topic here: http://wiki.bash-hackers.org/scripting/posparams
-