Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I list all open (X11) windows on Gnu/Linux from a Python script?

I'd like to be able to grab a list of all windows that are open on a Linux desktop from a Python script. I suppose this would require working through Xlib or some other x11 or xdisplay library. This would be the Linux equivalent on win32's EnumWindows API call.

Ideally, I'd like to be able to use this to get a list of the title/caption text of every open window along with position/size information.

Is there some function call from Python that will return this info?

like image 461
Al Sweigart Avatar asked Mar 06 '23 08:03

Al Sweigart


1 Answers

Install python-xlib:

pip3 install python-xlib

Try this:

from Xlib import display

d = display.Display()
root = d.screen().root

query = root.query_tree()

for c in query.children:
    # returns window name or None
    name = c.get_wm_name()
    if name: 
        print(name)

I'm not sure about the other properties. query.children is a list of Window objects, so some research on those should turn up something.

Window object docs.

like image 134
Ozzy Walsh Avatar answered Apr 06 '23 05:04

Ozzy Walsh