Skip to content

Instantly share code, notes, and snippets.

@lamchau
Created January 26, 2020 14:03
Show Gist options
  • Save lamchau/9d5529f99978914192f2de534304400b to your computer and use it in GitHub Desktop.
Save lamchau/9d5529f99978914192f2de534304400b to your computer and use it in GitHub Desktop.
bash script for setting the volume on macOS
#!/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