For a very long time I’ve used a script that either raises a running application or, if the application has not been launched yet, launches a new instance of the application. I use that script for some of my most used applications–like Thunar–and it has helped me enormously to keep my desktop tidy.

Tonight I decided it would be great to be able to do that with any application. Wouldn’t it be nifty if I could use this script in combination with dmenu?

It would indeed. Here is the result:


CMD=$(dmenu_path | dmenu -b -fn '-*-nu-*-*-*-*-*-*-*-*-*-*-*-*' -nb '#545043' -nf '#D2C7A7' -sb '#D2C7A7' -sf '#545043')

# if no instance of the app has been started, launch one now
if [ -z "`wmctrl -lx | grep -i $CMD`" ]; then
$CMD &
# search for existing app on any desktop and move it to the current desktop
app_on_any_desk=`wmctrl -lx | grep -i $CMD | cut -d ' ' -f 1`
wmctrl -i -R $app_on_any_desk

If no instance of the application you launched with dmenu is found this script will launch a new one. If there is an instance running, it will move it to the current desktop and focus it. If you prefer to move to the desktop where the application is currently running instead, change the last wmctrl command to wmctrl -i -a $app_on_any_desk.

You’ll need to make the script executable (chmod +x path_to_script) and you’ll need both wmctrl and dmenu installed. Dmenu is part of the suckless-tools package on Debian Testing and recent Ubuntu versions.

The colours dmenu uses in this script are the Erthe colours. Change them to your liking if you don’t like brown.

You can find this script (and a few others) also on my new github repository.