How to run a script in terminal when doubleclicked

6,610

Solution 1

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.

Solution 2

This solution should works for every:

  • file managers that support "configurable personal actions".

  • file managers that is configured to open files with double-click.

  • x terminal emulators that has options for executing: "-e" or "--execute".

  • script that have the permission to be executable.

EDIT

Tested with thunar file manager and after that, i installed nautilus and now double-click execute script for both.

1- Right-click on your script

2- Select "open with"

3- Click "open with other application"

4- Click "open with a command"

5- Check "use this action for this kind of file"

6- Enter the following command in the text area: x-terminal-emulator -e "/bin/bash %f"

7- Click Open,your script is executed in the terminal window.

8- Restart your file manager

Now evey time you double-click on a script,it will be executed.

Share:
6,610

Related videos on Youtube

Alex
Author by

Alex

Updated on September 18, 2022

Comments

  • Alex
    Alex over 1 year

    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.

    • WinEunuuchs2Unix
      WinEunuuchs2Unix about 7 years
      Simply change your script to use zenity or yad to prompt in GUI format. If you are unsure post your script contents in a "how do I"... question and some one will modify it for you in an answer.
    • Alex
      Alex about 7 years
      Can't do that... the interactive portion involves a LaTeX compiler-- so any output errors get output to the command line, and can't be routed to zenity. Don't have yad on my system.
    • Kaz Wolfe
      Kaz Wolfe about 7 years
      @WinEunuuchs2Unix Zenity isn't always the solution. Nor is a GUI. There are sometimes very good reasons to keep things in a console.
    • Alex
      Alex about 7 years
      @KazWolfe At this point, though, I'd be willing to accept even a not-so-good solution (that doesn't involve having multiple script files)-- but haven't found one yet.
    • WinEunuuchs2Unix
      WinEunuuchs2Unix about 7 years
      You can redirect LaTex errors to a file and call gedit to display the file when the compiler is finished.
    • Alex
      Alex about 7 years
      @WinEunuuchs2Unix The point is, LaTeX will stop on errors, and ask me what to do about them-- so redirecting to a file is not an option because that way then the compiler will never finish.
    • WinEunuuchs2Unix
      WinEunuuchs2Unix about 7 years
      @Alex Ahhh... Now I get it. Good thing I didn't post the answer I had coded :) It is probably possible to feed GUI retrieved answer back into LaTex but that is above my current pay-grade.
    • Alex
      Alex about 7 years
      @WinEunuuchs2Unix If you can describe how that might work (not necessarily coded), I would upvote. As is, I don't think that is possible (I might be wrong). In order to feed the GUI-retrieved answer into LaTeX, the GUI has to know the error message to display to the user (from LaTeX). And once LaTeX compiler gets far enough to display the error message, it doesn't terminate (so you can read what it output), but just hangs there until you tell it what to do next. I just don't see how to read LaTeX's output (up to the error message) unless you terminate the compiler (which would be a problem).
    • WinEunuuchs2Unix
      WinEunuuchs2Unix about 7 years
      @Alex I've already setup code to read terminal output from diff into gedit and terminal output from wget into yad progress display bar this week. But I haven't run into the problem of feeding keystrokes into the terminal yet. I wouldn't want to speculate on how to do that as important for me to make sure answers work (on my system at least) before posting. To see what I wrote earlier on another answer please visit: askubuntu.com/questions/112164/…
  • Alex
    Alex about 7 years
    I had tried that before posting... there is no option to click "open with a command"(4). I'd even gone into Nautilus settings-- the option to "Add another application to the list" is grayed out. I suspect I need to do sudo Naulilus in order to add an application to the list-- but this fails with dconf-shm.c:92:dconf_shm_open: assertiion failed: (memory != MAP_FAILED). Any way to go around this? Maybe add this application via command line?
  • user-707
    user-707 about 7 years
    i haven't done anything to nautilus,just double-click,it just works after configuring thunar,some data had been shared,because now nautilus is configured like thunar.Strange but true.
  • user-707
    user-707 about 7 years
    See my edit above.
  • Alex
    Alex about 7 years
    As I said, I'd be willing to accept this answer if it works-- but there is no option for me to add a new command to the existing list of commands: Abiword, Archive Manager, Document Viewer, File, GEdit, Gnash SWF Viewer, GNU IMP, Gnumeric, Image Viewer, Movie Player, Web I don't have Thunar installed. Are there other file managers that I should try? Or other ways to get this in the list, or somehow unblocking Nautlius?
  • user-707
    user-707 about 7 years
    You don't want to install thunar?
  • Alex
    Alex about 7 years
    Yes, I saw your comment. I said that I don't have Thunar installed, and asked if there were other managers I should try. I am currently trying to figure out how I can install Thunar-- I think I need to figure out its repository name, etc. I'm just really bad at installing new programs on Fedora.
  • user-707
    user-707 about 7 years
    Wait a minute ;) you use fedora and created this topic on "askubuntu",anyway it's not difficult to install thunar in fedora neither.
  • Alex
    Alex about 7 years
    EDIT: See comment below.
  • Jacob Vlijm
    Jacob Vlijm about 7 years
    @Alex did you log out/in and is the script run.sh executable? I assume the latter is the issue.
  • Alex
    Alex about 7 years
    Missed the logout/login part of it. Thanks, this worked!
  • Jacob Vlijm
    Jacob Vlijm about 7 years
    @Alex Ah, great :)
  • Alex
    Alex about 7 years
    Oops. I guess I just asked it where most of the other duplicates came from. But-- I'm still having trouble installing it. Again, I will upvote your question if you can help me install Thunar to make this work.
  • user-707
    user-707 about 7 years
    sudo dnf install thunar
  • Alex
    Alex about 7 years
    Fedora uses Yum, not dnf.
  • user-707
    user-707 about 7 years
    Latest fedora use dnf
  • Alex
    Alex about 7 years
    Unfortunately, I have Fedora 14, and that's the latest that is supported by my computer's hardware (in fact the reason I am asking this is, because up until yesterday, it only supported Fedora 13, and now they found a way to get Fedora 14 supported on there; the process of updating erased all my scripts, which performed this function in the past). So yeah, getting it with yum is a bit more complicated... to install LaTeX, I had to know which repository it came from; and same thing for each of its 5 dependencies. Maybe there's an easier way, I don't know, because I don't install things much.
  • WinEunuuchs2Unix
    WinEunuuchs2Unix about 7 years
    You need one more parameter --disable-fedora and then come join us in UbuntuLand :)
  • user-707
    user-707 about 7 years
    I you don't know by heart each repository, you can research and then install the package:sudo yum search thunar xfce.If you can't install thunar via any package manager(CLI or GUI),just install xfce,use my solution,and remove xfce, the config files for thunar will still be present on your system.Or you can install thunar from source.