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

1.1k Views Asked by At

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?

1

There are 1 best solutions below

8
On BEST ANSWER

Using @SomeGuyOnAComputer's suggestion:

First, get the nautilus python bindings:

$ sudo apt install python-nautilus

Make a directory to hold your nautilus python extensions:

$ mkdir -p ~/.local/share/nautilus-python/extensions

Apparently, nautilus python just reads extensions that are in that folder and uses them automagically.

Here is a simple extension that puts the file uri into the title bar:

from gi.repository import Nautilus, GObject, Gtk

class ColumnExtension(GObject.GObject, Nautilus.LocationWidgetProvider):
    def __init__(self):
        pass

    def get_widget(self, uri, window):
        window.set_title(uri)

Put this into "extension.py" and dump it into the above created folder. Restart nautilus. As in kill any nautilus processes and restart them. A simple way to do this is to simply reboot your machine.

This puts the file uri into the title bar, which is what your current script scrapes. In other words, you can simply continue doing what you've been doing, and it will now give you the full path.

Note that this doesn't seem to work when Nautilus first starts up. You have to actually navigate somewhere. In other words, if the title bar says "Home", you are in the home folder, and haven't navigated anywhere.