How to control OS X System Volume with AppleScript
I'm a DJ and sometimes I'm on one end of the room with the mic and my computer is on the other end of the room, but I always have my phone on me, so I decided to create an OS X Wifi Volume Remote Control. As part of that, I had to dig around a little bit and learn some AppleScript.
AppleScript is... something else.
I have no idea where one gets documentation for it. Everytime I've ever had to use it I've just copied and pasted from forum posts.
It truly is The World's First (and only) Read-Only Programming Language. Using it really makes me want to start writing an article on why I love syntax (and I will eventually write that article, but not tonight).
Anyway, here are a bunch of tidbits I learned:
How to see the current volume level and mute status
Through some searching, trial, and error I found that the
with join multiple outputs with a comma and space
osascript -e "output volume of (get volume settings) & output muted of (get volume settings)" 19, false
How to Mute and Unmute volume with AppleScript
Plain ol' mute makes sense:
osascript -e "set volume with output muted"
without instead of
unmute seems a little odd though:
osascript -e "set volume without output muted"
How to tell if the audio is muted with AppleScript
And if you want to check the status of whether the audio is muted or not, you can get that too:
osascript -e "output muted of (get volume settings)"
How to set the volume level to 2%
You might know the trick about using the ⇧ + ⌥ + Volume Down keyboard shortcut to set the volume by quarter bars, but you can also use AppleScript to set it to 100ths.
osascript -e "set volume output volume 51 --100%"
How to set the volume level to the secret minimum
So here's the deal, normally when you set the volume to 0 the speakers are automatically muted. On the keyboard you can hit the mute button while the volume is at 0 and it will unmute to the secret lowest volume setting.
Likewise, you can instruct AppleScript to not automatically mute the volume:
osascript -e "set volume without output muted output volume 1 --100%"
How to set the volume without unmuting
Sometimes you want the volume to stay muted, but to come on at a particular level when you unmute.
This is how you prevent the audio from unmuting when you adjust the volume level:
osascript -e "set volume with output muted output volume 42 --100%"
How to increment the volume by 1%
You can even get the current value and add to it all in one go.
osascript -e "set volume output volume (output volume of (get volume settings) + 1) --100%"
How to fade volume gently with AppleScript
Now let's say you want to slowly fade out the volume over a second (or however long).
You can create a
.applescript file that looks something like this,
with a fractional
except a heck of a lot longer:
set volume without output muted output volume 18 --100% delay 0.033 set volume without output muted output volume 17 --100% delay 0.033 set volume without output muted output volume 16 --100%
You could maybe just create a while loop, but I don't know how that's done yet.
By AJ ONeal
Was this useful to you? Share it!give me a tip or hire me.