Single Keyboard Shortcut to Launch/Raise an Application

I have recently shifted from KDE4 to Gnome3. In KDE you can create application specific keyboard shortcuts to raise windows. I typically create one each for Firefox, Thunderbird, my terminal etc. That way switching between windows is lightning quick. Gnome doesn’t seem to have that kind of functionality. So I asked on unix stackexchange, if there is a way to achieve the functionality. The clue was to use wmctrl.

A few Google searches later, I found this tutorial explaining how to use wmictrl to achieve what I want. Now I can Launch or Raise (if the application is already running) my favorite applications using a single keyboard shortcut per application. This is how I did it:

I took the find_app.sh script from the above URL and tweaked it to add support for application arguments. Here’s the result.

#!/bin/bash
# Find_app
# Author: Lucas van Staden (lvs at dedmeet.com / www.dedmeet.com)
# This little script will try and find the application attempting to start
# in the running processes, and if found, focus the application
# if not found, a new instance will start
# usage:
# find_app.sh <application with full path>
# params
# 1 - application to start (full path)
# helper applications
#!/bin/bash
# Find_app
# Author: Lucas van Staden (lvs at dedmeet.com / www.dedmeet.com)
# This little script will try and find the application attempting to start
# in the running processes, and if found, focus the application
# if not found, a new instance will start
# usage:
# find_app.sh <application with full path>
# params
# 1 - application to start (full path)
# helper applications
DOLLARONE=$(echo $1 | sed -e 's/[\t ]*$//') #Delete trailing spaces
WMCTRL=`which wmctrl`;
GREP=`which grep`;
APPLICATION=$(echo $DOLLARONE | cut -d ' ' -f 1)
if [ "x$APPLICATION" != "x$DOLLARONE" ]; then
  APPARGS=$(echo $DOLLARONE | cut -d ' ' -f 2)
fi
BASENAME=`basename $APPLICATION`;
BASENAME=`echo $BASENAME | tr "[:upper:]" "[:lower:]"`
FOUND=0;
function findwindow {
# 1 = BASENAME
# 2 = WMCTRL
# 3 = GREP
        IFS=$'\n';
        for RUNNING in `$2 -l -x`
        do
                if [ `echo $RUNNING | tr "[:upper:]" "[:lower:]" | $3 -c $DOLLARONE` -gt 0 ]
                then
                        HOSTNAME=`hostname`
                        WINDOW=${RUNNING#*${HOSTNAME} }
                        $2 -a $WINDOW
                        FOUND=1;
                fi;
        done
}
if [ "x$APPARGS" = "x" ]; then
  findwindow $BASENAME $WMCTRL $GREP;
  if [ $FOUND -eq 0 ]
  then
          $APPLICATION &
          sleep 2;
          # Try and find the application, after opened
          findwindow $BASENAME $WMCTRL $GREP;
          if [ $FOUND -eq 0 ]
          then
                  # Still not found, wait a bit more, and try again
                  sleep 3;
                  findwindow $BASENAME $WMCTRL $GREP;
          fi
  fi
else
  $APPLICATION $APPARGS &
fi

Added this script to $HOME/bin directory and made it executable. Make sure its in your path.

Next I updated the default desktop entry files for Firefox, Thunderbird and Terminator (my default terminal application).

First of all copy the default desktop entry file to your local apps directory.

cp /usr/share/applications/firefox.desktop ~/.local/share/applications/

Then change the Exec property from

Exec=firefox %u

To

Exec=find_app.sh "firefox %u"

Now assign a keyboard shortcut to your default browser application by going to System Settings => Keyboard => Shortcuts => Launchers => Launch Web Browser

Now restart Gnome Shell – Press Alt r to bring up the run dialog. Input r and press Enter.

Now with the keyboard shortcut assigned above, you should be able to launch as well bring the application to front if its already running.

Change the desktop files for your default mail client and terminal client similarly.

An additional step is required for this to work for your default terminal client.

Find the following snippet in your ~/.bashrc

case "$TERM" in
xterm*|rxvt*)
    PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"'
    ;;
*)
    ;;
esac

and change that to

APPNAME=$(basename $(cat /proc/$PPID/cmdline))
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~} $APPNAME\007"'
    ;;
*)
    ;;
esac

The above change will cause the application name to be always advertised in the title of your default terminal application. This is required for find_app.sh to raise the window of an active application.

Though Gnome3 has a way to have application keyboards for some apps, like your browser, email client etc, I haven’t found a way to assign custom keyboard shortcuts for any general application in Gnome3 yet, any clues?

Leave a Reply