Use dmenu to raise running applications
August 16, 2012
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:
#!/bin/sh
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 &
else
# 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
fi;
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.
Holy crap I so wanted this script. Thank you!
[…] To add ad main.c at line 1441; I modified manually main.c and executed the slackbuild; but it won't compile. Background why I want this is this article by urukrama : https://urukrama.wordpress.com/2012/0…-applications/ […]
Hi, urukrama –
I just wanted to stop by and thank you for your brilliant work with this blog. I love the Openbox Guide and Openbox FAQ, in particular – but your blog has so many interesting things to offer that I hardly feel comfortable singling out those two.
Please keep up the excellent work.
Regards,
R3nCi
Hi,
I understand the idea of the script, brilliant! But i’m a bit dumb here, how to use the script?
I have a keybinding in Openbox assigned to it, so that when I press W-p, it launches this script. See https://urukrama.wordpress.com/openbox-guide/#Key_mouse for more information on how to do so.