Nautilus extensions - how do you write them now G43?

So I’ve cobbled together a script that adds an entry in Nautilus’s right-click menu that allows me to open folders / files in Sublime Text.

But with Gnome 43 this stopped working (not surprisingly - as GTK version has been bumped) - any ideas how to approach it now? What are the required dependencies as well.

We have a migration guide now: Migrating to Nautilus API 4.0: nautilus-python Reference Manual

2 Likes

do you know of any working nautilus extension like this so I could look at the sample code? I’ve removed “window” argument and bumped “require version” to “4.0”, but this still is not working.

Here’s one that updated recently and has the Gtk 4.0 and Nautilus 4.0 required to work with Files 43 (with a fallback to 3.0 for older Files version—that may require more in the API use to make it work though): nautilus-open-any-terminal/open_any_terminal_extension.py at master · Stunkymonkey/nautilus-open-any-terminal · GitHub

To be clear: I haven’t used or tried that extension. Just did a search for “python nautilus” most recently updated on GitHub and stumbled on this project.

thank you Jake. This is a bit more convoluted example than I hoped.

So - is there a way to see why the extension is not working (a way to debug it) and play a tad with it? I’d love to to some simple things like get a path and print it, and so on - just so I could play and build the plugin step by step.

At this point I really don’t know what I’m doing wrong and how the things should be solved.

edit: ok, so actually it was a bit weird - after I removed old, not working extensions the new one I made started working.

Great you got it working! I have not yet used python-nautilus but started looking at it for a script of mine. There are examples: https://gitlab.gnome.org/GNOME/nautilus-python/-/tree/master/examples

1 Like

nice examples Jake!

It’s definitely not the best day for programming for me today… I’m trying to rewrite the whole thing and I’ve got totally confused… So if anyone can point me how do you get a path to a file/directory as a string?

I’d love to keep the things simple - just join string containing name of the app binary (in my case - “subl”) and a file / folder path then simply launch it using os.system(). I see I can get uri etc , but don’t see actual full path with file / folder name.

edit: nevermind… it is simply

.get_location().get_path()

… as mentioned I’m not in the best state of mind today :smiley:

Don’t use os.system. There’s no sensible reason to use that these days. That’s just inviting in bugs like for filenames with odd characters. You’d have to shlex.quote each filename with that.

You probably want to use subprocess.Popen instead, which you can pass the command as an argument list without the need to make a long string of it, and it will execute it in a new process so no need for the & subshell. That way you don’t need to run a shell or worry about shell special characters in filenames, it handles that safely. Make a list of the files you want to pass to Sublime Text and then do something like this (where SUBL and args are the variables from your above code):

_ = subprocess.Popen(
    [SUBL] + [args] + files_list,
    start_new_session=True,
)

I might prefer using Gio.DesktopAppInfo to get the .desktop file for Sublime Text and Gio.AppInfo.launch that with the list of files to open.

Sure Jake, will look into that, didn’t know os.system can cause issues (any links on the subject? I’ll look anyways :slight_smile: ). Just need to make sure subprocess doesn’t cause other issues. Need to check it out.

I’ve just uploaded current iteration to Github, just to make sure I won’t loose it easily :wink: .

As a trivial example. Create a file that has a shell special character in its name. Let’s put a ; in the filename. Make the file with command:

echo "Hello world!" > 'myfile;test.txt'

Then start python and run:

>>> import os
>>> filename='myfile;test.txt'
>>> os.system('cat' + ' ' + filename)
cat: myfile: No such file or directory
sh: line 1: test.txt: command not found
>>>

You had expected it to print “Hello world!”, the contents of the file we created, but instead cat tells you it can’t find the file myfile and the shell tells you it doesn’t know the command test.txt.

With subprocess it goes as expected:

>>> import subprocess
>>> filename='myfile;test.txt'
>>> _ = subprocess.Popen(['cat', filename])
>>> Hello world!

Thank you, you rock!

I’ll stick to os.system for a while. I couldn’t get Popen to work and I’m bit sick of restarting Gnome for today :wink: . I need to find a better way to troubleshoot things when working on such scripts, lack of cli output or any sort of other info is infuriating.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.