Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env bash
ME=$(basename $0)
MUTEFILE=~/.channels.mute
usage() {
cat <<END
$ME COMMAND [ARGS...]: Volume control / muting
Commands:
-t [CHANNEL] Toggle muting of CHANNEL
(Default input channel)
-l Show current volumes of all channels
-s CHANNEL [VALUE] Set the volume of CHANNEL to VALUE
or show the current volume of CHANNEL
(Default input channel)
-h This help
END
}
osa() {
osascript <<< "$@"
return $?
}
getvol() {
local which="$1"
osa $which volume of \(get volume settings\)
return $?
}
setvol() {
local which="$1"
local volume="$2"
osa set volume "$which" volume "$volume"
return $?
}
show() {
local which="$1"
echo -n "$which: "
getvol "$which"
return $?
}
toggle() {
local which="$1"
local ret=
local current="$(getvol "$which")"
if [ "$current" -gt 0 ]; then
echo "$which $current" > "$MUTEFILE"
setvol "$which" 0
ret=$?
else
setvol "$which" "$(getlast "$which")"
ret=$?
fi
return $ret
}
getlast() {
local which="$1"
local last=
local ret=0
local CHANNEL=
local VOLUME=
local TEMPFILE="$(mktemp mute.XXXX)"
while read CHANNEL VOLUME; do
if [ "$CHANNEL" = "$which" ]; then
last="$VOLUME"
break
else
echo "$CHANNEL $VOLUME" >> "$TEMPFILE"
fi
done < "$MUTEFILE"
if [ -n "$last" ]; then
ret=1
last=64
fi
mv "$TEMPFILE" "$MUTEFILE"
echo "$last"
return $ret
}
main() {
local cmd="$1"
shift
local which="${1:-input}"
[ -n "$1" ] && shift
case $cmd in
-t)
toggle "$which"
show "$which"
;;
-s)
[ -n "$1" ] && setvol "$which" "$1"
show "$which"
;;
-l)
osa get volume settings
;;
*)
usage
exit 1
;;
esac
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment