burning broccoli

python-wnck

I just found out about a nifty litlle library called libwnck and it’s python bindings are in the ubuntu repositories under the name python-wnck. Its all about window management. It’s meant to be used for applications like window lists and workspace switchers etc. You can maximize windows, change current workspace etc.

So the first thing I did (naturally) was to write a little script that can change to previously activated window, which is nice to have when jumping frequently between two windows (like eclipse and chromium). To bind a global key it uses a library from Project Hamster which in turn uses a lib from Tomboy, don’t ask me why… Anyway, this means that you also need to install the package hamster-applet to try out this script. It also has some other problems, activating a window on a different workspace doesn’t work. I’ll fix that later.

Installing and running

Just copy the script into a text editor, save it as focus_last.py or and run it with python focus_last.py

The keycode for focusing the last window is <Super>P

#!/usr/bin/python2.6
#   Copyright David Jensen 2009
#   This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <>.

import gtk
import gobject
import wnck
import hamster.keybinder as keybinder
import sys

import time
KEY_COMBINATION = '<Super>P'

class FocusShifter(object):
    def __init__(self):
        self.last_window = None       
        try:
            print 'Binding shortcut %s to focus last window' % KEY_COMBINATION
            keybinder.tomboy_keybinder_bind(KEY_COMBINATION, self.focus_last) 
        except KeyError:
            # if the requested keybinding conflicts with an existing one, a KeyError will be thrown
            print 'Key binding conflict, exiting'
            sys.exit(1)
        self.screen = wnck.screen_get_default();
        
    def focus_last(self):
        timestamp = int(time.time())
        win = self.screen.get_previously_active_window()
        #print "Prev win",win
        if win is not None:
            win.activate(timestamp) #not sure what timestamp should be, but this seems to work
            
f = FocusShifter()
gobject.MainLoop().run()
 
comments    Add a comment    Posted 2 years ago
blog comments powered by Disqus