Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
1  
If you want to raise and focus the window, wmctrl can do it. –  Gilles May 15 '11 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 '11 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. –  freethinker May 17 '11 at 9:59
add comment

2 Answers 2

up vote 1 down vote accepted

I figured out the solution and have posted it on my blog. wmictrl did the trick. I found a script on fluxbox wiki which finds an application if its already running and raises its window or launches it if no instances is running. I'm using that script with certain tweaks to launch applications.

I have documented it here: http://www.humbug.in/2011/single-keyboard-shortcut-to-launchraise-an-application/

I updated the script to support arguments, the result is:

#!/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
#!/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

Add the above script to your path, possibly in $HOME/bin/find_app.sh and make it executable and make sure its in your path.

make sure wmctrl is install

sudo apt-get install wmctrl

Now 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 eg.

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

Edit firefox.desktop in ~/.local/share/applications/ and change Exec to

Exec=find_app.sh "firefox %u"

Now add keyboard shortcut for your default broswer 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 you should be able to launch/raise your browser using a singular keyboard shortcut.

share|improve this answer
    
@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 –  freethinker May 17 '11 at 11:27
    
there should be a gnome3 native way of implementing this using libbamf library I think. –  freethinker May 17 '11 at 11:46
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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