DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Swap All Windows Between Two Workspaces In XFCE4

06.18.2009
| 3457 views |
  • submit to reddit
        
#!/usr/bin/python
"""
Swap all windows between two workspaces in XFCE4.
Depends: pygtk, pyxfce4
Author: Arnau Sanchez <tokland@gmail.com>

Example:
  
>>> import swap_workspaces as sw
>>> sw.swap_workspaces(1, 3)

or 

$ swap_workspaces 1 3
""" 
import xfce4.netk
import gtk
import sys

def refresh_gui():
    """Process all GTK pending events without blocking.""" 
    while gtk.events_pending():
        gtk.main_iteration_do(block=False)

def get_windows_for_workspace(screen, number):
    """Get windows for a given workspace number."""    
    for window in screen.get_windows():
        workspace = window.get_workspace()
        if workspace and workspace.get_number() == number:
            yield window

def swap_workspaces(wsn1, wsn2):            
    """Swap all windows between two workspaces."""
    screen = xfce4.netk.screen_get_default()
    workspaces = screen.get_workspaces()
    assert 0 <= wsn1 < len(workspaces)
    assert 0 <= wsn2 < len(workspaces)
    windows1 = list(get_windows_for_workspace(screen, wsn1))
    windows2 = list(get_windows_for_workspace(screen, wsn2))
    for window in windows1:
        window.move_to_workspace(workspaces[wsn2])
    for window in windows2:
        window.move_to_workspace(workspaces[wsn1])
    return len(windows1), len(windows2)

def main(args):
    """Swap workspaces windows. Workspaces index 0..N-1"""
    ws1, ws2 = map(int, args)
    print swap_workspaces(ws1, ws2)
    refresh_gui()
    
    
if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))