Created
January 26, 2020 14:03
-
-
Save lamchau/9d5529f99978914192f2de534304400b to your computer and use it in GitHub Desktop.
bash script for setting the volume on macOS
This file contains hidden or 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
#!/usr/bin/env bash | |
MIN_LEVEL=0 | |
MAX_LEVEL=100 | |
if [ "$#" -eq 0 ]; then | |
LEVEL=$MAX_LEVEL | |
else | |
LEVEL=$1 | |
fi | |
if ! [[ "$LEVEL" =~ ^[0-9\.]+$ ]]; then | |
case "$LEVEL" in | |
*min*) | |
LEVEL=$MIN_LEVEL | |
;; | |
*max*) | |
LEVEL=$MAX_LEVEL | |
;; | |
*) | |
LEVEL=$MIN_LEVEL | |
;; | |
esac | |
fi | |
# trunc(value) | |
LEVEL=$(printf "%.0f\n" "$LEVEL") | |
# max($MIN_LEVEL, min($LEVEL, $MAX_LEVEL)) | |
if [ "$LEVEL" -lt $MIN_LEVEL ]; then | |
LEVEL=$MIN_LEVEL | |
elif [ "$LEVEL" -gt $MAX_LEVEL ]; then | |
LEVEL=$MAX_LEVEL | |
fi | |
osascript -e "set volume output volume $LEVEL" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment