Skip to content

Instantly share code, notes, and snippets.

@jlp78
Last active February 14, 2024 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlp78/1d9334a988eb8ed959580f10bc7e7b56 to your computer and use it in GitHub Desktop.
Save jlp78/1d9334a988eb8ed959580f10bc7e7b56 to your computer and use it in GitHub Desktop.
functions to assist in managing mounted devices on Mac OS X
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