2013-08-15 23:29

Thanks to:
http://vickychijwani.github.io/2012/04/15/blazing-fast-application-switching-in-linux/

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
#!/bin/sh
# 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')
MINIMIZE=false
if echo "$WINIDS" | grep -q "$ACTIVEWIN"; then
        MINIMIZE=true
fi

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