Skip to content

Instantly share code, notes, and snippets.

@kawaz
Last active February 22, 2022 07:55
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 kawaz/5aa9d792798ba49aace14d17b4e68b3e to your computer and use it in GitHub Desktop.
Save kawaz/5aa9d792798ba49aace14d17b4e68b3e to your computer and use it in GitHub Desktop.
Adjusting the volume on macOS with CLI
#!/bin/bash
# https://gist.github.com/kawaz/5aa9d792798ba49aace14d17b4e68b3e
set -eo pipefail
usage() {
echo "Usage: $0 subcommand [args]"
echo " $0 get [output|input|alert|mute] get volume settings"
echo " $0 set [output|input|alert] 0-100 set volume"
echo " $0 set mute [true|false] set mute status"
echo " $0 help show this help"
exit 1
}
case $1 in
get)
if [[ -z $2 ]]; then
if command -v jq >/dev/null; then
osascript -e "get volume settings" | perl -pe's/\s*([\w\s]+):/"$1":/g;s/^/{/;s/$/}/' | jq .
else
osascript -e "get volume settings"
fi
exit
fi
case $2 in
output | input | alert)
osascript -e "get $2 volume of (get volume settings)"
;;
mute)
osascript -e "get output muted of (get volume settings)"
;;
*)
usage
;;
esac
;;
set)
case $2 in
output | input | alert)
osascript -e "set volume $2 volume $3"
;;
mute)
if [[ "$3" == "true" ]]; then
osascript -e "set volume output muted true"
elif [[ "$3" == "false" ]]; then
osascript -e "set volume output muted false"
else
usage
fi
;;
esac
;;
*)
usage
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment