Skip to content

Instantly share code, notes, and snippets.

@ecarreras
Last active December 13, 2015 17:58
Show Gist options
  • Save ecarreras/4952011 to your computer and use it in GitHub Desktop.
Save ecarreras/4952011 to your computer and use it in GitHub Desktop.
Switcht to previous workspace
#!/usr/bin/env python
import gtk
import wnck
import time
import sys
LW_FILE_PATH = '/tmp/lw'
def workspace_active_changed(screen, previous):
if not previous:
return
ws_id = previous.get_number()
set_previous(ws_id)
def get_previous():
ws_id = -1
with open(LW_FILE_PATH, 'r') as lw_file:
ws_id = int(lw_file.read())
print "Previous workspace was %s" % ws_id
return ws_id
def set_previous(ws_id):
print "Setting to previous workspace %s" % ws_id
with open(LW_FILE_PATH, 'w') as lw_file:
lw_file.write(str(ws_id))
def server():
screen = wnck.screen_get_default()
screen.connect('active-workspace-changed', workspace_active_changed)
gtk.main()
def client():
ws_id = get_previous()
screen = wnck.screen_get_default()
screen.force_update()
ws = screen.get_workspace(ws_id)
if ws:
ws_act_id = screen.get_active_workspace().get_number()
ws.activate(int(time.time()))
if ws_act_id != ws_id:
set_previous(ws_act_id)
if __name__ == '__main__':
if len(sys.argv) < 2 or sys.argv[1] == 'client':
client()
elif sys.argv[1] == 'server':
server()
else:
print "Arguments not recognized: %s" % ' '.join(sys.argv[1:])

Switch to previous workspace

  1. Start the server.
 $ python lw.py server
  1. Bind a shortcut to python lw.py client.

That's all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment