3

I'd like to have a list of open windows and its icons and process it with python. I thought I was close with xprop and wmctrl, but I can't use it for my purpose. I can get a list of open windows with wmctrl -l, but no idea how get an icon/icon path for any of the listed controls.

Please, help :)

xliiv
  • 31
  • 1
  • 2

1 Answers1

3

You can use the module wnck and gtk.

For instance:

import pygtk  
pygtk.require('2.0')  
import gtk  
import wnck  

screen = wnck.screen_get_default()  

while gtk.events_pending():  
    gtk.main_iteration(False)  

for w in screen.get_windows():  
    name = w.get_name()  
    icon = w.get_icon()
    ...

Where name is a string and icon is a GdkPixbuf.

You can read the API documentation for libwnck at http://developer.gnome.org/libwnck/stable/WnckWindow.html, which is for C. However, for Python you only have to remove the prefix wncK_window. If the documentation say wnck_window_get_name(), then in Python would be my_window.get_name().

libwnk is specific to XWindow, hence you can not use it on MS Windows. In that case, you can use other modules that precisely helps on that. Check the answer for Get list of open windows in Python

Community
  • 1
  • 1
gpoo
  • 8,408
  • 3
  • 38
  • 53
  • This is what I want, you are genius. Do you have any idea how to handle that with MS Windows? – xliiv Apr 29 '11 at 11:33
  • @xliiv, I added a reference to a similar question but limited only to MS Windows. It was answered by @reckoner. – gpoo Apr 29 '11 at 18:08
  • sudo apt-get install python-wnck will take care of 'ImportError: No module named wnck' – Luke Stanley May 26 '12 at 18:35