Skip to content

Instantly share code, notes, and snippets.

@gaelicWizard
Forked from ttscoff/afplay.completion.bash
Created August 4, 2021 00:34
Show Gist options
  • Save gaelicWizard/28346bf0051c363509a0ee112a8f82e0 to your computer and use it in GitHub Desktop.
Save gaelicWizard/28346bf0051c363509a0ee112a8f82e0 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