dbusgdbus

How to get the open windows of Nemo file manager by Dbus command


As you can see in the image bellow there are (org.Nemo) :

/org/Nemo/window/1
/org/Nemo/window/2
/org/Nemo/window/3
/org/Nemo/window/4

I have four windows open and it shows that.

Nemo Dbus

Each window has a GetMachineID command.


Solution

  • Many D-Bus API's make use of GetManagedObjects but Nemo doesn't seem to be doing this. A way to find the information you want would be to use busctl tree command to find all the object paths and then use them to call the GetMachineId.

    For example:

    $ busctl --user tree org.Nemo --list | grep -P "window/\d+"
    /org/Nemo/window/1
    /org/Nemo/window/2
    $ busctl --user call  org.Nemo /org/Nemo/window/1 org.freedesktop.DBus.Peer  GetMachineId
    s "c55143112d1244248ad60984144b7b53"
    
    

    You can wrap that in shell scripting or you could use Python.

    An example using Python:

    import dbus
    from xml.etree import ElementTree
    
    
    def get_machine_id(obj_path):
        bus = dbus.SessionBus()
        obj = bus.get_object('org.Nemo', obj_path)
        iface = dbus.Interface(obj, 'org.freedesktop.DBus.Peer')
        print('\t', iface.GetMachineId())
    
    
    def nemo_windows():
        window_paths = []
        bus = dbus.SessionBus()
        root = '/org/Nemo/window'
        obj = bus.get_object('org.Nemo', root)
        iface = dbus.Interface(obj, 'org.freedesktop.DBus.Introspectable')
        xml_string = iface.Introspect()
        for child in ElementTree.fromstring(xml_string):
            window_paths.append(root + '/' + child.attrib['name'])
        return window_paths
    
    
    if __name__ == '__main__':
        win_paths = nemo_windows()
        for pth in win_paths:
            print(pth)
            get_machine_id(pth)
    
    

    Gave the following output when I ran it:

    /org/Nemo/window/2
         c55143112d1244248ad60984144b7b53
    /org/Nemo/window/1
         c55143112d1244248ad60984144b7b53