How to get the list of open windows from xserver

前端 未结 3 1370
醉话见心
醉话见心 2020-12-04 17:38

Anyone got an idea how to get from an Xserver the list of all open windows?

3条回答
  •  一向
    一向 (楼主)
    2020-12-04 18:16

    Building off of Marten's answer, (assuming your window manager supports Extended Window Manager Hints) you can feed that list of window ids back into xprop to get the _NET_WM_NAME property:

    $ xprop -root _NET_CLIENT_LIST |
        pcregrep -o1 '# (.*)' |
        sed 's/, /\n/g' |
        xargs -I{} -n1 xprop -id {} _NET_WM_NAME
    

    But at the command line, it would just be easier to use wmctrl:

    $ wmctrl -l
    

    Programmatically, with python-xlib, you can do the same with:

    #!/usr/bin/env python
    from Xlib.display import Display
    from Xlib.X import AnyPropertyType
    
    display = Display()
    root = display.screen().root
    
    _NET_CLIENT_LIST = display.get_atom('_NET_CLIENT_LIST')
    _NET_WM_NAME = display.get_atom('_NET_WM_NAME')
    
    client_list = root.get_full_property(
        _NET_CLIENT_LIST,
        property_type=AnyPropertyType,
    ).value
    
    for window_id in client_list:
        window = display.create_resource_object('window', window_id)
        window_name = window.get_full_property(
            _NET_WM_NAME,
            property_type=AnyPropertyType,
        ).value
        print(window_name)
    

    Or, better yet, using the EWMH library:

    #!/usr/bin/env python
    from ewmh import EWMH
    
    window_manager_manager = EWMH()
    client_list = window_manager_manager.getClientList()
    
    for window in client_list:
        print(window_manager_manager.getWmName(window))
    

提交回复
热议问题