burning broccoli

keyboard shortcuts to focus applications using python-wnck

In a previous post I fiddled a bit with python-wnck and made a small script that bound <Super>P key to activating previous focused window, i.e. last app you where looking at. This was kind of useful since I often find myself spending way to much time and effort jumping between applications. Window lists,pagers and good looking compiz effects that does the same thing all have one thing in common, I have to identify a name, image or icon and select it. In short, I have to search for it.

You may think I have very little patience, pressing TAB a couple of times to find the window of an app can’t take that much time, can it? Well, no, not really - but since I’m programmer I tend to work with multiple applications at the same time (email,browser,eclipse,spotify,terminal(s),nautilus etc) and I switch between them a lot in one day. Actually it’s not the speed that bothers me, more that I already know which window/application I want and it’s frustrating to have to search for it every damn time!

So what’s my brilliant solution to this? Well my first thought (obviously) was some kind of direct neural interface so that the computer could just infer what I wanted before I even knew wanted it. But since that is a bit too much to engineer on one lunch break I opted for a simpler solution: creating a couple of shortcuts for my most used apps.

The script below does half of the job, it tries to find a window matching the supplied application name. If it does gives it focus. If not it will try to start it.

The script takes one or two arguments, the first is the application name as it is in the window list. If that is the same as the executable your’re all set. Otherwise the second argument is the executables name.

A couple of examples

$ ./activate.py evolution
$ ./activate.py Terminal gnome-terminal

I use CompizConfig Settings Manager and to set global key bindings, so now whenever I tap <Super>T the gnome-terminal springs to life.

Here’s the script, the only requirement is python-wnck.

#!/usr/bin/python2.6
import wnck
import sys
import time
import subprocess as sub

if len(sys.argv)  []"
    sys.exit(1)

name = sys.argv[1]
timestamp = int(time.time())

screen = wnck.screen_get_default();
screen.force_update()
wins = screen.get_windows()
for win in wins:
    app = win.get_application()
    if app is not None:
        if name == app.get_name():
            geo = win.get_geometry()
            screen.move_viewport(max(geo[0],0),max(geo[1],0)) #bugs a bit, unclear why
            win.activate(timestamp)
            sys.exit(0)
            
#if we get this far we didn't find the program, let's start it
if len(sys.argv) >= 3:
    sub.Popen(sys.argv[2],shell=True)
else:
    sub.Popen(sys.argv[1],shell=True)
comments    Add a comment    Posted 2 years ago
blog comments powered by Disqus