Last active
February 22, 2022 07:55
-
-
Save kawaz/5aa9d792798ba49aace14d17b4e68b3e to your computer and use it in GitHub Desktop.
Adjusting the volume on macOS with CLI
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
#!/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