Is it possible to raise a window using a keyboard shortcut in Gnome3?

I found a script on fluxbox wiki that uses wmctrl to find an application and raise its window if it's already running. Otherwise, the script launches the application. I'm using that script with tweaks to support arguments, which I have documented on my blog.

  1. Make sure wmctrl is installed.

    sudo apt-get install wmctrl
    
  2. Add the following script to your path (possibly in $HOME/bin/find_app.sh), and make it executable.

    #!/bin/bash
    # Find_app
    
    # Author: Lucas van Staden (lvs at dedmeet.com / http://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
    
  3. Update the desktop entry files of the applications you want to have a singular shortcut for launching as well as raising, so that the applications are invoked through the above script.

    For example:

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

    Edit firefox.desktop in ~/.local/share/applications/ and change the Exec line to refer to find_app.sh:

    Exec=find_app.sh "firefox %u"
    
  4. Now add a keyboard shortcut for your default browser:

    System Settings | Keyboard | Shortcuts | Launchers | Launch Web Browser

  5. Restart gnome shell: Press Alt r to bring up the run dialog. Type r and press Enter.

You should now be able to launch/raise your browser using a single keyboard shortcut.


There is a similar tool called xdotool. It seems to be very much the same as wmctrl. The main advantage over the latter perhaps, is that it uses X Window IDs rather than strings to handle windows. I don't know if it matters much in your case though. But say you are using Chrome, opened on a website, the title of which has Mozilla, then you may not be able to identify the application from the window's title.

Tags:

Gnome

D Bus