Last active
July 27, 2024 10:34
-
-
Save AprilArcus/10926706 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I've always disliked the "./configure" incantation - it's right here! I | |
# shouldn't need to tell the shell where to look for it. But of course, | |
# putting '.' in your $PATH is terrible -- it's insecure and throws permission | |
# errors if you try to 'execute' regular files. Bash 4's `shopt -s autocd` | |
# solves half the problem; and the ability to override | |
# command_not_found_handle() solves the other. | |
# | |
# This is an OS X-oriented script that attempts to do the most intelligent | |
# thing possible when you enter the name of a file in your $PWD into the bash | |
# prompt. It will: | |
# | |
# 1. Prompt to execute the file, if its executable bit is set | |
# 2. Preview media files in Quick Look | |
# 3. Prompt to open a file in its associated app, if one exists | |
# 4. Open text, XML, and files with no associated app in $EDITOR | |
# | |
# Known bugs/tricky behaviors: | |
# * bash chokes on file names containing parentheses. | |
# * Executables with spaces in their names are not supported. | |
# * If autocd is on, entering a .app bundle's name with navigate into it, | |
# rather than execute it. | |
# * If there is a command in $PATH with the same name as a file in $PWD, the | |
# command will be executed. | |
# | |
# Room for improvement: | |
# * Support for xdg-open, gnome-open and GNOME Sushi | |
command_not_found_handle() { | |
# do not run when inside Midnight Commander or within a Pipe | |
if test -n "$MC_SID" -o ! -t 1 ; then | |
echo $"$1: command not found" | |
return 127 | |
fi | |
if test -x "$1"; then # ∃ an executable matching the first arg? | |
# confirmation prompt | |
read -p "execute $PWD/$*? " -n 1 -r | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
printf "\n" | |
./$* # execute with args | |
else | |
printf "\n" | |
fi | |
elif test -f "$*"; then # a file? | |
local FILE="$*" | |
local MIME_TYPE=$(file --mime-type --brief "$FILE") | |
local MIME_CHARS=[[:alnum:]'!#$&.+-^_'] | |
# open text files in $EDITOR | |
if [[ $MIME_TYPE =~ (text/$MIME_CHARS+|application/($MIME_CHARS+\+)?xml|application/x-empty) ]]; then | |
$EDITOR "$FILE" | |
# view media files in quicklook | |
elif [[ $MIME_TYPE =~ (image|audio|video)/$MIME_CHARS+ ]]; then | |
qlmanage -p "$FILE" &> /dev/null | |
else | |
local DEFAULT_APP=$(osascript -e "on run argv" -e "return POSIX path of default application of (info for item 1 of argv)" -e "end run" "$FILE" 2> /dev/null) | |
if [[ -n $DEFAULT_APP ]]; then | |
printf "open $PWD/$FILE" | |
# 'open ' '/' ' with ' ' ? █' | |
if (( 5 + ${#PWD} + 1 + ${#FILE} + 6 + ${#DEFAULT_APP} + 4 <= $COLUMNS )); then | |
printf " " | |
else | |
printf "\n" | |
fi | |
read -p "with $DEFAULT_APP ? " -n 1 -r | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
open -a "$DEFAULT_APP" "$FILE" | |
fi | |
printf "\n" | |
else | |
$EDITOR "$FILE" | |
fi | |
fi | |
else | |
echo $"$1: command not found" | |
return 127 | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment