Skip to content

Instantly share code, notes, and snippets.

@nickrw
Created April 19, 2013 12:43
Show Gist options
  • Save nickrw/5420085 to your computer and use it in GitHub Desktop.
Save nickrw/5420085 to your computer and use it in GitHub Desktop.
volsay: Wrapper around OSX's `say' command, which sets the system volume to the requested level, then restores volume / mute state once speech is complete.
#!/bin/bash
function usage() {
echo "usage: $0 <volume (0-100)> <say args...>"
echo
echo "Unmutes and sets the system volume to volume%, passing the remaining"
echo "arguments to the OSX \`say' command, restoring volume / mute setting"
echo "to previous values after the speech has completed."
exit 1
}
# Set volume level (echos current volume level before changing)
function volume() {
osascript -e 'output volume of (get volume settings)'
osascript -e "set volume output volume $1"
}
# Set system mute status (echos current mute state before changing)
function mute() {
mstate=$(osascript -e 'output muted of (get volume settings)')
echo $mstate
if [[ $mstate != $1 ]]; then
osascript -e "set volume output muted $1"
fi
}
if [[ -z $1 || ! $1 =~ ^[0-9]+$ ]]; then
usage
exit 1
fi
oldmute=$(mute false)
oldvol=$(volume $1)
shift
say $*
volume $oldvol > /dev/null
mute $oldmute > /dev/null
@joeheyming
Copy link

fwiw, the man page should include [[volm <%>]] somewhere

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment