Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is it possible to focus an opened app instance/window using a command?

I'd like to assign a keyboard shortcut as to quickly switch to a specific app.

I want shortcuts for:

  • chromium (now it opens a new window with blank tab)
  • terminator (now it opens a new window)

It works fine for these (running the default command, eg. thunderbird):

  • thunderbird
  • sublime text 2

I am using gnome-shell @ 12.04 (shouldn't matter)

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

The wmctrl package offers access and manipulation of application windows and would do what you want. Install this little package first:

sudo apt-get install wmctrl

Here is the man page for wmctrl: http://manpages.ubuntu.com/manpages/precise/en/man1/wmctrl.1.html

When you look at the command line switches, you will see that -a will raise and focus a window. In order to know which window to focus, make sure you have a Chromium window open, and then run

wmctrl -l

This will give you a list of all open windows and their titles. Chromium will most likely always have "Chromium" in the end of it's window title, so you can use that.

With this information at hand, you can construct your command for the shortcut. Open the Keyboard preferences, go to Shortcuts, click on Custom Shortcuts, click the + sign and enter:

Name: Focus Chromium Shortcut
Command: wmctrl -a 'Chromium'

I am using Unity and just tried this with Google Chrome, it did the trick. It should work in Gnome-Shell as well.

Pretty cool idea :)

DrTebi

share|improve this answer
    
Thanks a ton. Works as expected. As for the terminal, one can use username@pc_name. –  mreq Dec 30 '12 at 9:07
    
I am glad it worked out. You could, if you wanted to, take it a step further and put the wmctrl command into a script. This script could then do further things, e.g. check if there is a window with that name at all, and if not, start the application. This way you could technically setup all your favorite applications to shortcuts, which will have two functions--either start the app if not started yet, or bring it into focus if it is already started... just as an idea if you feel like playing around a bit more. –  DrTebi Dec 30 '12 at 10:11
    
Yes, that's exactly what I am going to do once I get some time ;) Too bad you can't modify apps' titles. You can't for instance target Clementine with wmctrl as it always has the title of what's playing. –  mreq Dec 30 '12 at 10:34
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.