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

百般思念 提交于 2019-12-13 03:14:49

问题


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:

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


回答1:


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

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")


来源:https://stackoverflow.com/questions/45797220/how-to-execute-a-nautilus-script-written-in-python-inside-a-gnome-terminal-windo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!