Search code examples
pythonshellgnome-terminalnautilus

How to execute a Nautilus script written in Python inside a gnome-terminal window that stays open?


Lets say I want to execute a simple Python script from Nautilus, the default file manager of GNOME:

#!/usr/bin/python3
print("Hello")

Of course the aim is to interact with selected files in Nautilus, but I want to keep it simple.

I save the script to the folder ~/.local/share/nautilus/scripts/ and then I can execute it from the right click context menu:

Nautilus script context menu

How can I execute this nautilus script inside gnome-terminal and keep the terminal opened at the end of the script?


Solution

  • I have found that I can achieve what I want to do using two script files.

    Script folder

    1) Hello.sh to open gnome-terminal (and potentially leave it open)

    The first script file ~/.local/share/nautilus/scripts/Hello.sh will appear in Nautilus script context menu and will open gnome-terminal in order to execute .Hello.py:

    #!/bin/bash
    gnome-terminal -- python3 ~/.local/share/nautilus/scripts/.Hello.py
    

    To force the terminal window to stay open after execution (to see output or for debugging purpose if it fails), tweak it as follow to make gnome-terminal execute bash at the end:

    #!/bin/bash
    gnome-terminal -- bash -c "python3 ~/.local/share/nautilus/scripts/.Hello.py; bash"
    

    2) .Hello.py to execute the actual script

    Then, the second script file ~/.local/share/nautilus/scripts/.Hello.py will be executed inside the gnome-terminal windows opened previously but will be hidden from nautilus script context menu.

    #!/usr/bin/python3
    print("Hello")