Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Last active August 4, 2021 00:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ttscoff/f2110fee3bd97559eb343a39389934c3 to your computer and use it in GitHub Desktop.
Save ttscoff/f2110fee3bd97559eb343a39389934c3 to your computer and use it in GitHub Desktop.
Bash custom completion to afplay System sounds on Mac
play() {
command afplay "/System/Library/Sounds/$1.aiff"
}
_complete_system_sounds ()
{
local cur
local LC_ALL='C'
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
# Turn case-insensitive matching on if needed
local nocasematchWasOff=0
shopt nocasematch >/dev/null || nocasematchWasOff=1
(( nocasematchWasOff )) && shopt -s nocasematch
local IFS="
"
# Store list of sounds
local sounds=( $(command ls /System/Library/Sounds/ 2>/dev/null | xargs -I% basename % .aiff) )
# Custom case-insensitive matching
local s matches=()
for s in ${sounds[@]}; do
if [[ "$s" == "$cur"* ]]; then
matches+=("$s");
fi
done
# Unset 'nocasematch' option if it was unset before
(( nocasematchWasOff )) && shopt -u nocasematch
COMPREPLY=("${matches[@]}")
return 0
}
complete -F _complete_system_sounds play
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment