Ubuntu – How to run a script in terminal when doubleclicked

command linegnome-terminalnautilusscripts

Summary: How to set Run in Terminal as the default Nautilus doubleclick action?

I wish to run an interactive script by doubleclicking it. To interact with it, I have to see it run.

If I gsettings set org.gnome.nautilus.preferences executable-text-activation to ask, then it gives me a choice; Run in Terminal does what I want; Run doesn't, since when the script needs to ask me something, it just hangs. However, I don't want to have to click the Run in Terminal every time. I want it to do that by default. I've done it before somehow.

I tried to gsettings set org.gnome.nautilus.preferences executable-text-activation to launch; however, that simply does Run as the default; I don't see a way to make it Run in Terminal by default.

I have also tried inserting a gnome-terminal command. This successfully pops up a terminal window (even when simply Run); however, I then somehow need to send all the commands to that terminal window.

Alternatively, I know how to send (just) the interactive portion of the script to that terminal, which would work for me; however, in that case I need the script to halt until the interactive portion of it finishes, and then resume the execution. Typically, this can be accomplished using the wait command; as is, the gnome-terminal command seems to work as gnome-terminal& instead, in that it relinquishes control to the bash script as soon as it is launched.

I've read through almost 10 duplicates of the thread How do I run executable scripts in Nautilus? (including other sites), and still cannot find my answer.
Any other workarounds will be appreciated.

Best Answer

Not literally what you asked for, still an elegant option I believe:

Add a right-click option to run a selected script in terminal

  1. Create a small script:

    #!/bin/bash
    gnome-terminal -e $1 
    
  2. Save it as run_interminal.sh in ~/.local/share/nautilus/scripts. Create the directory if it doesn't exist.

  3. Make the script executable.
  4. Log out and back in.

That's it. Select a script and choose Scripts --> runinterminal.sh:

enter image description here

Alternatively; drag/drop- run in terminal

Drag an (executable) script over a launcher to have it run in the terminal:

  1. Copy the code below into an empty file and save it on your desktop as run_script.desktop

    [Desktop Entry]
    Name=Run Script
    Type=Application
    Exec=gnome-terminal -e %u
    Terminal=true
    
  2. Make it executable

That's it. Now when dragging an executable script on to the icon, it will run in (gnome-) terminal.

Important note

As mentioned, the script run_interminal.sh needs to be executable to appear in the menu.
As it is, the script to run also needs to be executable. run_interminal.sh can easily be set to automatically make the targeted script executable or call an interpreter. If that is needed, please mention.

Related Question