10

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. Also I do not like Gnome3's window switching scheme (alt-tab).

Hence I was wondering if its possible to raise windows through DBUS? If it is, then it will be possible to write a script and assign a keyboard shortcut to that.

3
  • 1
    If you want to raise and focus the window, wmctrl can do it. May 15, 2011 at 11:19
  • 1
    How "intelligent" is the KDE shortcut method? eg. How does it handle multiple windows of the same application? There are several X tools which working singly or together can do what you want.. But which ones you use can depends on how sophisticated you want to make it.
    – Peter.O
    May 16, 2011 at 12:27
  • @Gilles, thanks for the clue, wmctrl did the trick @fred the KDE shortcut method works only for single windows, but thats good enough for me, coz I only use single windows for firefox, thunderbird etc. May 17, 2011 at 9:59

2 Answers 2

3

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.

2
  • @Gilles Its an open source script, not upto me to release it or not, i thought its just too long a solution to be posted here. Will update my answers with more details May 17, 2011 at 11:27
  • there should be a gnome3 native way of implementing this using libbamf library I think. May 17, 2011 at 11:46
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.