How to get list opened windows in PyGTK or GTK in Ubuntu?

gtk, pygtk, python, ubuntu, window-management

Solution

You probably want to use libwnck:

http://library.gnome.org/devel/libwnck/stable/

I believe there are python bindings in python-gnome or some similar package.

Once you have the GTK+ mainloop running, you can do the following:

import wnck
window_list = wnck.screen_get_default().get_windows()

Some interesting methods on a window from that list are get_name() and activate().

This will print the names of windows to the console when you click the button. But for some reason I had to click the button twice. This is my first time using libwnck so I'm probably missing something. :-)

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

class WindowLister:
    def on_btn_click(self, widget, data=None):
        window_list = wnck.screen_get_default().get_windows()
        if len(window_list) == 0:
            print "No Windows Found"
        for win in window_list:
            print win.get_name()

    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

        self.button = gtk.Button("List Windows")
        self.button.connect("clicked", self.on_btn_click, None)

        self.window.add(self.button)
        self.window.show_all()

    def main(self):
        gtk.main()

if __name__ == "__main__":
    lister = WindowLister()
    lister.main()

Problem

How to get list opened windows in PyGTK or GTK or other programming language? in Ubuntu? edit: i want get list paths opened directories on desktop!

Original source