Skip to content

Instantly share code, notes, and snippets.

@kawaz
Last active Feb 22, 2022
Embed
What would you like to do?
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