Guest User

dmenu window selection

a guest
Jun 29th, 2012
238
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. import os
  3. import re
  4.  
  5. """
  6.    Go to window using dmenu.
  7.    Requires wmctrl, xprop, and something I might have forgotten.
  8.    Tested on Python 2.7.
  9. """
  10.  
  11. """ Returns the window class, give a window ID """
  12. def get_window_class(id):
  13.     xprop_wininfo = os.popen("xprop WM_CLASS -id " + id).read().split('\n')[:-1]
  14.     return xprop_wininfo[0].split('"')[1]
  15.  
  16. window_list = os.popen("wmctrl -l").read().split('\n')[:-1]
  17.  
  18. # remove double spaces
  19. window_list = [x.replace('  ', ' ') for x in window_list]
  20.  
  21. # a list of lists
  22. window_list = [x.split(' ',3) for x in window_list]
  23.  
  24. # dict key: window name, value: window ID
  25. id_dict = { x[3]:x[0] for x in window_list}
  26.  
  27. # get the classes of all windows
  28. class_dict = {x:get_window_class(x) for x in id_dict.values()}
  29.  
  30. # combined dict:  name+class:id
  31. win_dict = {}
  32. for win_name in id_dict.keys():
  33.     id = id_dict[win_name]
  34.     new_name = win_name + " (" + get_window_class(id) + ")"
  35.     new_name = re.sub(r'[\x80-\xff]', '', new_name).lower()
  36.     win_dict[new_name] = id
  37.  
  38. # input for dmenu, newline separated
  39. dmenu_in = '"' + '\n'.join(win_dict.keys()) + '"'
  40.  
  41. selection = os.popen("echo " + dmenu_in + " | dmenu").read()
  42. win_id = win_dict[selection]
  43. os.popen('wmctrl -i -a ' + win_id)
RAW Paste Data