Last active
February 14, 2024 11:05
-
-
Save jlp78/1d9334a988eb8ed959580f10bc7e7b56 to your computer and use it in GitHub Desktop.
functions to assist in managing mounted devices on Mac OS X
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
eject () { | |
case "x$1" in | |
x) | |
disk=`osascript -e 'tell application "Finder" to get name of every disk whose ejectable is true' 2>/dev/null | | |
perl -ne 'print((split(", "))[0], "\n");'` | |
;; | |
*) | |
disk="$1" | |
;; | |
esac | |
if [ "$disk" ]; then | |
echo "ejecting $disk" | |
osascript -e "tell application \"Finder\" to eject \"$disk\"" | |
else | |
echo "no ejectable media" | |
fi | |
} | |
ejectall () { | |
osascript -e 'tell application "Finder" to eject (every disk whose ejectable is true)' | |
} | |
_eject () { | |
local cur=${COMP_WORDS[COMP_CWORD]} | |
local IFS=$'\n' | |
local escaped_single_quote="'\''" | |
local i=0 | |
COMPREPLY=( $( cd /Volumes; compgen -o dirnames -- "$cur" ) ) | |
for entry in ${COMPREPLY[*]}; do | |
if [[ "${cur:0:1}" == "'" ]]; then | |
# started with single quote, escaping only other single quotes | |
# [']bla'bla"bla\bla bla --> [']bla'\''bla"bla\bla bla | |
COMPREPLY[$i]="${entry//\'/${escaped_single_quote}}" | |
elif [[ "${cur:0:1}" == "\"" ]]; then | |
# started with double quote, escaping all double quotes and all backslashes | |
# ["]bla'bla"bla\bla bla -> ["]bla'bla\"bla\\bla bla | |
entry="${entry//\\/\\\\}" | |
COMPREPLY[$i]="${entry//\"/\\\"}" | |
else | |
# no quotes in front, escaping _everything_ | |
# [ ]bla'bla"bla\bla bla -> [ ]bla\'bla\"bla\\bla\ blal | |
entry="${entry//\\/\\\\}" | |
entry="${entry//\'/\'}" | |
entry="${entry//\"/\\\"}" | |
COMPREPLY[$i]="${entry// /\\ }" | |
fi | |
(( i++ )) | |
done | |
return 0 | |
} | |
complete -F _eject eject |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment