Skip to content

Instantly share code, notes, and snippets.

@Tschrock
Created January 28, 2022 13:07
Show Gist options
  • Save Tschrock/2601cdd17c6fd1dfa31de06987064b2a to your computer and use it in GitHub Desktop.
Save Tschrock/2601cdd17c6fd1dfa31de06987064b2a to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# ask2run
# v1.0.0
#
# Provides a graphical prompt for opening or executing a script file, emulating
# GNOME Files (Nautilus)'s old "Do you want to run X?" prompt that has since
# been removed. Depends on `zenity` for graphical prompts.
#
# -------------- Usage ----------------
#
# ask2run "/path/to/script/file.sh"
# ask2run --install [MIMETYPE ...]
#
# -------------- Setup ----------------
#
# To register ask2run with your desktop environment and associate it with
# common script file types, simply run:
#
# ./ask2run --install
#
# You may optionally include a space-separated list of mimetypes to associate
# ask2run with, for example:
#
# ./ask2run --install application/x-shellscript text/x-python
#
# By default, ask2run will associate itself with the following mimetypes:
#
# - text/x-shellscript
# - application/x-sh
# - application/x-shellscript
#
# ------------- Changelog --------------
#
# v1.0.0 (2022-01-28)
# - Initial Release
#
# -------------- License ---------------
#
# MIT License
#
# Copyright (c) 2020 Tyler Schrock
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# --------------------------------------
########################################
# Script Settings
# What to do if the file is missing the executable bit.
# Can be "open" to open the file as text or "warn" to give the user a warning.
NO_EXEC_BIT_ACTION="warn"
# The command to run to open a terminal. The script path is appended to the end.
TERMINAL_PROGRAM="gnome-terminal --"
# The command to run to open an editor. The script path is appended to the end.
EDITOR_PROGRAM="gedit"
# Labels for the different buttons
LABEL_OPEN_TEXT="Open as Text"
LABEL_RUN_TERMINAL="Run in the Terminal"
LABEL_RUN_BACKGROUND="Run in the background"
LABEL_CANCEL="Cancel"
########################################
# Script start
# Check for the initial setup flag
if [ "$1" = "--install" ]; then
shift
# Install the .desktop file
# This writes to a temp file first since we can't pipe to xdg-desktop-menu
TMP_DIR=$(mktemp -d)
DESKTOP_FILE="$TMP_DIR/tschrock-ask2run.desktop"
cat <<EOF > "$DESKTOP_FILE"
[Desktop Entry]
Version=1.0
Type=Application
Name=ask2run
Icon=application-x-shellscript
Exec="ask2run" %f
Comment=Asks to run a script.
Categories=Development;
Terminal=false
EOF
xdg-desktop-menu install "$DESKTOP_FILE"
rm -r "$DESKTOP_FILE" "$TMP_DIR"
# Assign mime types
if [ ! -z "$1" ]; then
# Read from arguments
while [ ! -z "$1" ]; do
xdg-mime default tschrock-ask2run.desktop "$1"
shift
done
else
# Use default mimetypes
xdg-mime default tschrock-ask2run.desktop text/x-shellscript application/x-sh application/x-shellscript
fi
exit 0;
fi
# The path to the script file should be the first argument
SCRIPT_PATH="$1"
# Make sure we have a file
if [ ! -f "$SCRIPT_PATH" ]; then
# Might want to show a message graphically for debugging?
echo "Usage:\n ask2run \"/path/to/script/file.sh\"\n ask2run --install [MIMETYPE ...]"
exit 1;
fi
# Make sure we can run zenity
if ! command -v zenity >/dev/null 2>&1; then
# No way to display a graphical error here
echo "Error: ask2run requires zenity for graphical prompts."
exit 1;
fi
# Make sure the file is executable
if [ ! -x "$SCRIPT_PATH" ]; then
if [ "$NO_EXEC_BIT_ACTION" = "warn" ]; then
# WISHLIST: Add buttons to continue opening or cancel
zenity --warning --no-wrap --text "This file could be run as a program, but is not marked as executable.\nIf you want to run this file as a program, please mark it as executable.\n\nFile: $SCRIPT_PATH"
fi
exec $EDITOR_PROGRAM "$SCRIPT_PATH"
fi
# Prompt for an action from the user
FILE_ACTION=$(zenity --info --no-wrap \
--text "This text file can be run as a program.\nHow would you like to open this file?\n\nFile: $SCRIPT_PATH" \
--ok-label "$LABEL_OPEN_TEXT" \
--extra-button "$LABEL_RUN_TERMINAL" \
--extra-button "$LABEL_RUN_BACKGROUND" \
--extra-button "$LABEL_CANCEL")
if [ "$FILE_ACTION" = "$LABEL_CANCEL" ]; then
# Cancel opening the file
exit 0;
elif [ "$FILE_ACTION" = "$LABEL_RUN_BACKGROUND" ]; then
# Run the script without a terminal window
exec "$SCRIPT_PATH"
elif [ "$FILE_ACTION" = "$LABEL_RUN_TERMINAL" ]; then
# Run the script with a terminal window
exec $TERMINAL_PROGRAM "$SCRIPT_PATH"
else # The "Open as Text" action - zenity doesn't output this since it's the default "Ok" action
# Open the script as a text file
exec $EDITOR_PROGRAM "$SCRIPT_PATH"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment