change-window~ (Código fuente)

#!/usr/bin/env python

import os
import sys
import commands

# EXAMPLE OF wmctrl OUTPUT

# $ wmctrl -l -x
# 0x01000004 -1 xfce4-panel.Xfce4-panel  victoria xfce4-panel
# 0x0100002a -1 xfce4-panel.Xfce4-panel  victoria xfce4-panel
# 0x0100002d -1 xfce4-panel.Xfce4-panel  victoria xfce4-panel
# 0x01800003 -1 xfdesktop.Xfdesktop   victoria Escritorio
# 0x03800004  0 xfce4-terminal.Xfce4-terminal  victoria Terminal - humitos@victoria: ~
# 0x01600003  0 Thunar.Thunar         victoria humitos - Administrador de archivos
# 0x040000a3  0 emacs.Emacs           victoria emacs-victoria: ~/bin/change-window [-]
# 0x024000ab  0 Navigator.Firefox     victoria Inbox (0) - someone@gmail.com - Gmail - Mozilla Firefox
# 0x04200db8  0 Pidgin.Pidgin         victoria (someone@gmail.com)
# 0x0420006a  0 Pidgin.Pidgin         victoria Lista de amigos
# 0x03600052  0 sun-awt-X11-XFramePeer.org-openstreetmap-josm-Main  victoria Editor Java de OpenStreetMap
# 0x03e00001  0 Google-chrome.Google-chrome  victoria Nueva pestana - Google Chrome

# XFCE4 XML FILE
# .config/xfce4/xfconf/xfce-perchannel-xml/xfce4-keyboard-shortcuts.xml
# [...]
#       <property name="&lt;Super&gt;a" type="string" value="/home/humitos/bin/change-window thunar"/>
#       <property name="&lt;Super&gt;c" type="string" value="/home/humitos/bin/change-window pidgin"/>
#       <property name="&lt;Super&gt;j" type="string" value="/home/humitos/bin/change-window josm-latest"/>
#       <property name="&lt;Super&gt;g" type="string" value="/home/humitos/bin/change-window google-chrome"/>
# [...]

LIST_WINDOWS = 'wmctrl -l -x | grep -i {} | grep -v grep | tail -n {}'
CHANGE_WINDOW = 'wmctrl -i -a {}'
COMMANDS_TO_WMCMD = {
    'pidgin': 'Pidgin.Pidgin',
    'emacs': 'emacs.Emacs',
    'thunar': 'Thunar.Thunar',
    'xfce4-terminal': 'xfce4-terminal.Xfce4-terminal',
    'firefox': 'Navigator.Firefox',
    'josm-latest': 'sun-awt-X11-XFramePeer.org-openstreetmap-josm-Main',
    'google-chrome': 'Google-chrome.Google-chrome',
    'terminator': 'terminator.Terminator',
}

def get_window_id(command):
    # This function is horrible but it's late at night and I just want
    # this shit working :)
    if command == 'pidgin':
        n = 2
    else:
        n = 1
    output = commands.getoutput(LIST_WINDOWS.format(COMMANDS_TO_WMCMD[command], n))
    if n > 1:
        if command == 'pidgin':
            lines = output.splitlines()
            for l in lines:
                if 'Lista de amigos' not in l:
                    output = l
                    break
    print output
    win_id = output.split(' ')[0]
    return win_id


def main():
    program = sys.argv[1]
    win_id = get_window_id(program)
    if win_id != '':
        os.system(CHANGE_WINDOW.format(win_id))
    else:
        os.system(program)

if __name__ == '__main__':
    main()