Jun 22
wmctrl
I came across wmctrl recently which can be to interact with a EWMH/NetWM compatible X Window Manager such as Enlightenment, Icewm, Kwin, Metacity and Sawfish. From the description of wmctrl: “Wmctrl provides command line access to almost all the features defined in the EWMH specification. For example it can maximize windows, make them sticky, set them to be always on top. It can switch and resize desktops and perform many other useful operations.” More information on wmctrl is available at http://www.sweb.cz/tripie/utils/wmctrl/
I installed it on my Ubuntu Edgy system from the repository
sudo apt-get install wmctrl
The usefulness of this program is quite evident. I list some examples below.
- Launch a program on a different desktop:
Create the file LaunchProgram.sh in a folder in your path (I used /usr/bin):1 2 3 4 5 6 7 8 9 10 11 12 13
#!/bin/bash if [ $# -ne 2 ] then message="Usage\n\t$0 Program DesktopNumber" message="$message \n\nExample\n\t$0 'nautilus /usr' 1" zenity --error --text="$message" else currentDesktop=`wmctrl -d | grep '*' | cut -d' ' -f1` wmctrl -s $2 && $1 wmctrl -s $currentDesktop fi
$# is the number of command line arguments passed to the script. We want exactly 2 arguments. $0 is the file’s name which was used to launch the script. The desktop number should be between 0 and (number of desktops - 1). This script assumes that correct values are passed.
zenity is used to display a dialog with the error message.
In the output of wmctrl -d, the current desktop has a * next to the desktop number. We use grep to get that line and then extract the desktop number which is the first field (hence -f1). The -d’ ‘ option tells cut that the fields are separated by a single space.
$1 onwards represent the arguments passed to the script. We switch to the desktop number passed as the second argument and launch the command passed as the first argument.
Grant execute permission on the script and launch an example:
chmod a+x LaunchProgram.sh LaunchProgram.sh "nautilus /usr/bin" 1
- Cycle through desktops:
There was a requirement to switch desktops automatically at a pre-defined interval. The desktops had running applications (such as vnc clients connected to different computers) which had to be monitored. A shell script combined with wmctrl provides the solution. Adjust the parameter to ’sleep’ as required.1 2 3 4 5 6 7 8 9 10 11 12
#!/bin/bash numberOfDesktops=`wmctrl -d | wc -l` for (( ; ; )) do for (( i = 0; i < $numberOfDesktops; i++ )) do wmctrl -s $i sleep 10s done done
wmctrl -d lists the desktops managed by the window manager (each on a new line). We count the number of lines to get the number of desktops.
- Change window properties:
The -b switch can be used to change a window’s properties. The command below launches a terminal and activates the fullscreen mode (which is not the same as maximizing the window).gnome-terminal && wmctrl -r :ACTIVE: -b toggle,fullscreenThe -r switch is used to select a target window for an action. It accepts, by default, a window name. :ACTIVE: is a special window name string which implies the currently active window. The -b switch is used to add, remove or toggle upto two window properties simultaneously. We are toggling the fullscreen property (which will also get the window out of fullscreen if it is already in that mode).
I tested the above command with gnome-terminal, nautilus and gthumb. It did not work with gthumb. However, gthumb has a -f switch for the purpose.
In the command above, I have discounted the possibility of the window becoming inactive before wmctrl had a chance. This can be dealt with by identifying the window in a more specific way.
gnome-terminal && wmctrl -r "abhijeet@ApplicationServer" -b toggle,fullscreen
will look for a window with “abhijeet@ApplicationServer” as part of the title. The -F switch can be used for an exact match (exact title and case sensitive).
If you want to create a launcher to start programs manipulated as above, put this line in the command box:
bash -c "gnome-terminal && wmctrl -r :ACTIVE: -b toggle,fullscreen"

July 8th, 2007 at 10:29 pm
You just made my day. The script for launching programs on different desktops is exactly what I was looking for. Thanks a lot.