Marco Sulla
Marco Sulla

Reputation: 15930

How to search a window in all workspaces with python-xlib?

I would create a Python script that searches for a window with a certain title and bring it to top. I found how to do it in the current workspace: http://ubuntuforums.org/showthread.php?t=1254376

but I would like to do it in all workspaces. How can I do it?

Upvotes: 3

Views: 2363

Answers (2)

Mohit
Mohit

Reputation: 2110

The code searches for the window in the query tree and gets the window object which can later be used to get query the desired properties:

disp = Display() 
root = disp.screen().root    
children = root.query_tree().children    
for win in children:
    try:
        WinName = getProp(disp,win,'NAME') # or win.get_wm_name()
        PIDs = getProp(disp,win,'PID')
        WinPID = newPIDs[0]

        if WinName and WinName == DESIRED_NAME:
            print("found the window with pid: {} winName: {}  winId: {}".format(pid, newWinName,win.id))
            # Code to bringToTop
        print

    except Xlib.error.BadWindow:
        print("BadWindow error")


def getProp(disp,win, prop):
    p = win.get_full_property(disp.intern_atom('_NET_WM_' + prop), 0)
    return [None] if (p is None) else p.value

Upvotes: 0

MeV
MeV

Reputation: 11

Old question but I faced the same problem. The answer is that to get a list of windows on all workspaces, you should not test if windows are viewable (remove the "attrs.map_state == X.IsViewable" part).

display = Display()
root = display.screen().root
winid_list = root.get_full_property(self.NET_CLIENT_LIST_ATOM,
                                            X.AnyPropertyType).value
for winid in winid_list:
    win = self.display.create_resource_object('window', winid)
    transient_for = win.get_wm_transient_for()
    wmname = win.get_wm_name()
    if transient_for == None:
        if wmname != None and name in wmname:
            break

Upvotes: 1

Related Questions