7

I just switched from Linux Mint (17.3) to elementary OS (0.3.2 Freya).

On Mint, when I double clicked a shell script, I was asked if I wanted to "Run in terminal", "Display", "Cancel" or "Run" the script. Run Script Dialog

Now, I constantly execute scripts by accident because I just double click on them. (At least I think they are being executed but without a terminal.)

Is it somehow possible to get a similar question before running a shell script from Pantheon Files?

5
+100

From what I can tell this dialogue is built into nemo and caja so no, not from pantheon files. you could try another filebrowser (there's lots), or file a bug on the bug tracker (seeing this dialogue every day would be my personal hell though so ask for a gsettings key).

In the mean time you could hack around it with this script:

#!/bin/bash

zenity --question --text="Do you wish to run '$1', or display its contents?" \
       --ok-label=Run \
       --cancel-label=Edit

case $? in
    0)pantheon-terminal -e "$1"
    ;;
    1)scratch-text-editor $1
    ;;
esac

although I'm not sure it works properly outside of the terminal and I'm not sure of the best way to make it the default way to open shell scripts.

  • I just realised that you could use my script as a specimen to add dialogues to your own custom scripts to prevent accidental execution. There's lots more zenity can do, check out the docs. It's a hidden gem present in most linux systems. – Nine Feb 5 '16 at 15:22
  • I have posted an answer on how your script could be used – user170 Jan 25 '17 at 13:48
  • could the script be modified so that the zenity window is dismissed with ESC or close button? as it is both trigger the option to open in text editor – user170 Mar 2 '17 at 14:40
  • what does pressing esc or closing the window do now? if it doesn't work there's a problem with the switch statement, add the line echo $? after zenity, run the script and press esc, and make a new case for this return value. – Nine Mar 4 '17 at 22:19
  • 1
    ok, well in that case zenity returns the same value for closing the window and cancelling the action, and can't be fixed without patching zenity, or writing a new dialog either in files or as a separate project. this is kind of the problem with integrating hacks into your workflow: you can't really get support or bugfixes. – Nine Mar 5 '17 at 23:02
3

At present, the only way to choose is to right-click on the script - the context menu will give on option to "Run" or "Open with ...". The latter should give a list of applications including a text editor.

I could not find a bug raised regarding this at the moment but it seems a reasonable request, so I suggest you raise a bug at https://bugs.launchpad.net/pantheon-files.

1

I'm not sure if it is possible with Pantheon Files, but if you're willing to use the regular Nautilus instead (which is actually very similar to Pantheon Files) there's an easy way to configure it:

  • install Nautilus
  • to make Nautilus visible in Slingshot follow instructions here: Why don't certain GNOME apps show in Slingshot? (don't get confused, both Pantheon Files and Nautilus will be called "Files" in Slingshot. In my system, Nautilus would be the first of the two icons, I guess because it's sorted alphabetically)
  • install the dconf-editor (e.g. via Software Center)

Open dconf and you'll have plenty of settings usually hidden in the system settings. The path is org > gnome > nautilus > preferences. There you'll find an option called "executable-text-activation". Clicking on the value you'll get three options:

  • "launch" will directly launch the shell script. This would be your current setting
  • "display" will always display the script in a text editor
  • "ask" will run the prompt you're familiar to from Mint - this is the option you're looking for
1

The executable scripts will be always executed in Pantheon Files when clicked, no matter the application set as default for that type of files. (bug report: https://bugs.launchpad.net/pantheon-files/+bug/1669996).

A solution for what is asked (avoiding to start scripts when accidentally clicking them), as suggested in the answer on Nautilus, is to use another file manager which has the desired option when manipulating those scripts.

Creating a run_zenity.sh with the script from the answer on Zenity

#!/bin/bash

zenity --question --text="Do you wish to run '$1', or display its contents?" \
       --ok-label=Run \
       --cancel-label=Edit

case $? in
    0)pcmanfm "$1"
    ;;
    1)scratch-text-editor $1
    ;;
esac

and a contract file for that

gedit ~/.local/share/contractor/runoredit-zenity.contract

with a content like

[Contractor Entry]
Name=RUN OR EDIT
Icon=terminal
MimeType=application/x-sh;application/x-executable;
Exec=bash -c '/path/to/the/script/run_zenity.sh %f' 

you will get a new context menu in Pantheon Files; but that is not very useful, as it only provides options that are already provided by the Run and Open with entry.

enter image description here

Anyway, that entry opens the Zenity dialog, which works as expected:

enter image description here

The zenity dialog script can be added to a desktop file (a launcher in ~/.local/share/applications), and the launcher can be set the default 'application' to be used for all executable scripts in Pantheon Files (so that it shows that dialog and avoid the risk of executing the scripts by accident), but no matter that setting Pantheon File will always execute a file if it is an executable file. On the other hand that works ok for .desktop files. (See more on that HERE and below for Thunar).


So, considering other file managers:

PCManFM

Beside Nautilus already mentioned, an even lighter File Manager, PCManFM provides this by default.

enter image description here

'Open' will use the default application.

That can be changed in settings

enter image description here

Thunar

Unlike PCManFM, Thunar file manager doesn't seem to have that dialog for executable files, but in Thunar the Zenity script can be made the default application for scripts as indicated, and then, when clicking a script in Thunar the Zenity dialog will appear. For that, create a desktop file:

gedit ~/.local/share/applications/run-or-edit.desktop

with

[Desktop Entry]
Type=Application
Name=Run or edit
Icon=gedit
Categories=Utility;TextEditor;
MimeType=application/x-sh;application/x-executable;
Exec=bash -c '/path/to/the/script/run_zenity.sh %f' 

Then, no matter the file manager, set the new "application" called Run or edit the default for script files under a file's properties.

As said, in Pantheon Files that changes nothing, but in Thunar it will open the Zenity dialog when clicking to open a script.

To be sure the Run or edit app appears in the list when you try to set is as default, you can add these lines

application/x-sh=run-or-edit.desktop
application/x-executable=run-or-edit.desktop

in ~/.local/share/applications/mimeinfo.cache under [MIME CACHE] and also in ~/.local/share/applications/mimeapps.list under [Default Applications].


There are a few glitches with that Zenity script: after the dialog is displayed, dismissing it with Esc or the window close button (x) will still open the file in the text editor. (As it is a 'yes or no' dialog, 'no' is the text editor.) To improve that behavior look here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.