-
-
Save gaelicWizard/28346bf0051c363509a0ee112a8f82e0 to your computer and use it in GitHub Desktop.
Bash custom completion to afplay System sounds on Mac
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
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