How to execute a Nautilus script written in Python

2019-08-16 14:43发布

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?

1条回答
smile是对你的礼貌
2楼-- · 2019-08-16 14:55

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")
查看更多
登录 后发表回答