2013-08-15 23:29

Thanks to:

I made a few improvements on the script. If you want something with more features (but also more complex).

  • Use /bin/sh so that it may use dash instead of bash (lighter)
  • Use exec to start the command so that it replaces the current script (otherwise you get unneeded bash processes in ps)
  • Raise all windows of the same class: useful to raise all terminals for example
  • “toggle mode” : when run a second time, the windows are minimized. For that I had to depend on xdotool, because wmctrl can’t do that (probably coming in a future wmctrl release. the author commited a -Y flag in github for that).

Save it to /home/USER/bin/run-or-raise, then add some keyboard shortcuts in your window manager settings and enjoy!

<Super>f /home/USER/bin/run-or-raise Navigator.Firefox firefox
# syntax: run-or-raise WM_CLASS_name COMMAND
#   WM_CLASS_name : the WM_CALL_name of the window (from wmctrl -lx output)
#   COMMAND : the command to run if nothing to raise

#logfile=/tmp/$(basename $0).log
#exec > $logfile 2>&1

# get windows ids matching WM_CLASS_name
WINIDS=$(wmctrl -lx | awk '{ if ($3 == "'"$1"'") print $1}')

# run if nothing started. exec will end the script
[ -z "$WINIDS" ] && exec "$2"

# if the window is active, we minimize all the windows of the class
ACTIVEWIN=$(wmctrl -a :ACTIVE: -v 2>&1 | sed -n 's/^Using window: \(.*\)/\1/p')
if echo "$WINIDS" | grep -q "$ACTIVEWIN"; then

for ID in $WINIDS; do
        if $MINIMIZE; then
                xdotool windowminimize "$ID"
                wmctrl -i -a "$ID"
2013-08-15 23:29 · Tags: