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.

During my recent stint with Firebox, and before that while I was trying out wmii, I’ve grown very quickly accustomed to dmenu. After only using it for a few hours, I fell in love with it: it is so convenient to launch an application when you don’t have your hands on the mouse; launch dmenu, just type a few letters from the application name, make sure you have the right app selected, press enter and your app shows up.

Unfortunately, Firebox is still too unstable to use as a primary window manager, and I haven’t grown accustomed to tiling window managers (yet?). So I began to wonder whether it is possible to run dmenu or something similar in Openbox and Pekwm, and – guess what? – it is very easy! (Why did I ever think otherwise?)

First, you’ll need to install dmenu. You can download the source code from the dwm/wmii website and install that (‘sudo make clean install’ in Ubuntu). To launch dmenu in Openbox and Pekwm, I use the following command:

$(dmenu_path | dmenu -b -nb '#E0E9D0' -sb '#a6b38d' -sf '#070806')

This launches dmenu at the bottom of the screen (-b), with a colour scheme to match the Aeterna Openbox and Gtk theme. You can change the settings (everything that comes after ‘dmenu’ in the above command) to suit your preferences; read the man pages (‘man dmenu’) to see what options you have.

I have this bound to Alt+F3 in both Pekwm and Openbox (Alt+F1 launches the root menu and Alt+F2 launches gmrun). In Pekwm, I added the following to keys file:

KeyPress = "Mod1 F3" { Actions = "Exec $(dmenu_path | dmenu -b -nb '#E0E9D0' -sb '#a6b38d' -sf '#070806') &" }

To get dmenu to work in Openbox was a little more complex. If I added the above command to the rc.xml file dmenu wouldn’t launch (Openbox-Message: Failed to execute ‘$(dmenu_path | dmenu’)’: Failed to execute child process “$(dmenu_path” (No such file or directory) say the xsessions-errors.). I’ve tried adjusting the command in several ways, but couldn’t find one that did work, so I ended up creating a script to launch dmenu in Openbox. It is very straightforward:

#!/bin/sh
exec $(dmenu_path | dmenu -b -nb '#E0E9D0' -sb '#a6b38d' -sf '#070806')

I saved the file as OBdmenu in ~/.scripts, where I keep all my scripts, made it executable (chmod +x ~/.scripts/OBdmenu), and added the following to the rc.xml file

For Openbox, this is what I added in the <keyboard> section of the rc.xml file:

<keybind key="A-F3">
<action name="Execute">
<execute>/home/urukrama/.scripts/OBdmenu</execute>
</action>
</keybind>

Here is a screenshot of dmenu running at the bottom of the screen in Openbox:

OBdmenu

The only downside to using dmenu in this way is that the applications launched by it don’t show up with their application name in your favourite system monitor, but as the command you used to launch dmenu (OBdmenu in my Openbox session, $(dmenu_path etc. in my Pekwm session). I have no idea how to get around this, but it is only a detail. Otherwise, dmenu is fantastic. It won’t replace my right-click root menu, but it is a great addition to any window manager.

Update: As Tami points out in a comment below, you can get around this problem if you use the following command to launch dmenu:

`dmenu_path | dmenu -b -nb '#E0E9D0' -sb '#a6b38d' -sf '#070806'` && eval “exec $exe”

In Pekwm, you can use this command in the Keys file, but in Openbox you’ll still have to use a script to launch dmenu (as explained above).

This should also work in other window managers, such as Fluxbox, or desktop environments like Xfce, though I haven’t tried it.