How do I permanently change window titles?

Which files are involved in generating the window title? For example, if you open Rhythmbox, you see the program name as the window title. But it’s not generated by the .desktop file. Which file(s) do I need to edit to add text to the title of an application? I want the changes to be permanent

I use Ubuntu GNOME 15.04 with GNOME 3.16.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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:

enter image description here

into:

enter image description here

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:

enter image description here

enter image description here

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

enter image description here

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

  1. The script uses both wmctrl and xdotool:

    sudo apt-get install wmctrl
    sudo apt-get install xdotool
    
  2. Copy the script into an empty file save it as setwindowname.py

  3. Create the file ~/window_names.txt (exactly named window_names.txt), add your applications in the format:

    <application> <window_name>
    

    e.g.

    gedit Text editor
    

    The window name may include spaces.

  4. Test-run the script from the terminal by the command:

    python3 /path/to/setwindowname.py
    
  5. 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.

Solution 2

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 with sudo 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:

  1. music.desktop or the name you gave the application

    with Exec=music and Hidden=true (add the last one if not present)

  2. rhythmbox.desktop

    with Exec=rhythmbox %U and Hidden=true

  3. 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.

  1. 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:

    1. [email protected] is an array that stores all the positional parameters.
    2. ${@:2} means: All the positional parameters but the first
    3. Let’s say that I call a program this way:

      awesome-program foo bar baz fooz
      
      • ${@:1} would be foo bar baz fooz
      • ${@:2} would be bar baz fooz
      • ${@:3} would be baz fooz
      • ${@:4} would be fooz
      • ${@:5} is empty because there aren’t more positional parameters.
    4. [email protected], ${@} and ${@:1} are the same thing: All the positional parameters.

    5. Learn more about this topic here: http://wiki.bash-hackers.org/scripting/posparams

Solution 3

Just use the translation file!

I ran into this very problem challenge with Thunar: It became annoying when Compiz Scale displayed the Thunar window title as “AnyFolder – File Manager” and comsumed space with unnecessary information.

I tried the above mentioned ways with xdotool and wmctrl but they were not permanent since Thunar changes its window title every time I go to another folder. And it felt exaggerated to have a script sitting around in the background to change the title back every time Thunar alters it.

An easier solution was to use the translation file which comes with every application. You simply need to re-compile the translation source file with a tool called msgfmt included in the gettext package. I just did this for gThumb -just for fun- and there it works, too.

1. Get source code for the concerned application.
Type
$ sudo apt-get source APPLICATION or head over to http://packages.ubuntu.com, get the source and look for for a folder called po: it contains plain-text language files. Look for your language. Let’s say you’re from Brazil, that file would be pt_BR.po

2. Alter the file to your needs
Look for the exact! window title that you want to get rid off or change.
In Thunar’s case it was “File Manager” and in pt_BR.po I found

:. set window title
: ../thunar/thunar-window.c:3154 ../Thunar.desktop.in.in.h:3
: ../thunar/thunar-settings.desktop.in.h:1
msgid "File Manager"
msgstr "Gerenciador de arquivos"

where msgid is the signal the application sends and msgstr what the langpack translates it to.
Change msgstr to whatever you like, i.e. msgstr "o_-".
I learned that if you leave the translation empty like this msgstr "" the application will fall back to msgid. If you want it to be “empty” use whitespace. Or a dot. Or any funky ASCII symbol.

3. Get the compiler
As mentioned you need msgfmt from gettext package, so you need to type
$ sudo apt-get install gettext

4. Compile
Open a terminal, go to the folder with your altered pt_BR.po and let msgfmt check the file for errors:
$ msgfmt --check --check-accelerators=_ -o /dev/null pt_BR.po
If it’s fine, go ahead and compile it:
$ msgfmt -c -o pt_BR.mo pt_BR.po

5. Place file
Have a look how the original translation in /usr/share/locale/YOUR_LANGUAGE/LC_MESSAGES/ or /usr/share/locale-langpack/YOUR_LANGUAGE/LC_MESSAGES/ is named and place your file there. If it doesn’t exist in both places, put it in one of them and experiment a little with the file name: lowercase, Camelcase or something like , i.e. gthumb.mo or YOUR-APP.mo.
Make a backup of the original first:
$ sudo cp /usr/share/locale/YOUR_LANGUAGE/LC_MESSAGES/YOUR-APP.mo /usr/share/locale/YOUR_LANGUAGE/LC_MESSAGES/YOUR-APP.mo.BAK
then copy your new translation there
$ sudo cp pt_BR.mo /usr/share/locale/YOUR_LANGUAGE/LC_MESSAGES/YOUR-APP.mo

If YOUR_APP is running, close all instances of it.
Test It!

Hint
If you’re running Ubuntu with EN localisation like I am, just use any *.po, delete or comment all unchanged msgid/msgstr couples from it and put it in /usr/share/locale/en/LC_MESSAGES/.

Sources:
http://wiki.xfce.org/translations/translation_guidance_in_xfce

Solution 4

Replace (parts of) window titles with wmctrl

Since my own answer using the application’s translation file wasn’t sufficient for several cases for me, I built upon Jacob Vlijm’s solution and modified his script to replace certain strings in window titles. I hope it’s okay to post this a separate answer. Credits go to Jacob Vlijm for the idea and setting up the script!

For example, I like to read Some fancy website instead of Some fancy website - Mozilla Firefox. Compiz scale plugin is much clearer now as I also removed - File manager and other strings.

Changes I made:

  1. use // as separator in window_names.txt to allow white-spaces
  2. use wmctrl instead of xdotool to modify the window title (since xdotool doesn’t handle non-ASCII characters very well): wmctrl -i -r [WindowIdentifier] -N 'new Title'
  3. some extra lines in the script to do the string replacement etc.

The modified window_names.txt is structured like this:
[process name]//[exact text to replace]//[replacement text]

firefox// - Mozilla Firefox// (whitespace!)
thunar// - File Manager// ° xyz
gedit// - gedit// - 123

The pimped 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("//"))
    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(None, 4)
            winID = line[0] ; pid = line[2] ; currentTitle = line[4]
            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]:
                newTitle = str.replace(currentTitle, app[1], app[2])
                subprocess.Popen(["wmctrl", "-i", "-r", winID, "-N", newTitle])
        curr_1 = curr_2
        time.sleep(0.3)
    except:
        pass

Again: Credits go to Jacob Vlijm for the idea and setting up the script!

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply