Thursday, 17 January 2019

Is it possible to get the directory of a specific nautilus window in a script?

I would like to build a Python script that checks if a specific directory is open in nautilus.

So far the best solution I have is to use wmctrl -lxp to list all windows, which gives me output like this:

0x0323d584  0 1006   nautilus.Nautilus     namek Downloads
0x0325083a  0 1006   nautilus.Nautilus     namek test
0x04400003  0 25536  gvim.Gvim             namek yolo_voc.py + (~/code/netharn/netharn/examples) - GVIM4

Then I check if the basename of the directory I'm interested in is in window name of the nautilus.Nautilus windows.

Here is the code for the incomplete solution I just described:

    def is_directory_open(dpath):
        import ubelt as ub  # pip install me! https://github.com/Erotemic/ubelt
        import platform
        from os.path import basename
        import re
        computer_name = platform.node()
        dname = basename(dpath)
        for line in ub.cmd('wmctrl -lxp')['out'].splitlines():
            parts = re.split(' *', line)
            if len(parts) > 3 and parts[3] == 'nautilus.Nautilus':
                if parts[4] == computer_name:
                    # FIXME: Might be a False positive!
                    line_dname = ' '.join(parts[5:])
                    if line_dname == dname:
                        return True
        # Always correctly returns False
        return False

This can definitely determine if it is not open, this only gets me so far, because it might return false positives. If I want to check if /foo/test is open, I can't tell if the second line refers to that directory or some other path, where the final directory is named test. E.g. I can't differentiate /foo/test from /bar/test.

Is there any way to do what I want using builtin or apt-get / pip installable tools on Ubuntu?



from Is it possible to get the directory of a specific nautilus window in a script?

No comments:

Post a Comment