Ask before running script in Thunar

Nautilus or PCManFM have an option to ask you before executing files.

Can I have that in Thunar?


I am posting this in order to provide an answer.


Solution 1:

Based on the Zenity script provided in this answer,

see you have zenity installed

sudo apt-get install zenity

create an executable file called editorrun.sh with this content:

    #!/bin/bash

zenity --question --text="What to do with the executable file '$1'?" \
       --ok-label=Run \
       --cancel-label=Edit

case $? in
    0)xfce4-terminal -e "bash $1"
    ;;
    1)gedit $1
    ;;
esac

make it executable, then add it to the list of applications

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

with this content:

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

adding the path to the script in Exec=bash -c 'path/to/the/script/editorrun.sh %f'. Then, make that executable.

Then, make this desktop file the default application for executable files by selecting such a file, going to 'Proprieties - General - Open with' and selecting Edit-or-run.

enter image description here

After that, when you click an executable a dialog appears

enter image description here

(A small glitch: you cannot dismiss the dialog at this point: using close window button, Esc or Alt+F4 equates to the --cancel-label option in the script and will open the file in text editor.)


Edit: regarding the "small glitch": to avoid the file being opened for editing through this zenity script (and given that "open in text editor" is a very accessible option anyway), one could remove the text editor from the script and re-formulate the question:

    #!/bin/bash

zenity --question --text="Do you REALLY want to EXECUTE this file?" \
       --ok-label=YES! \
       --cancel-label=No...

case $? in
    0)0)xfce4-terminal -e "bash $1"
    ;;
    1)
    ;;
esac

Which gives you this:

enter image description here

where "Yes" will execute the script, while all other options do nothing.