PDA

View Full Version : Incremental volume adjustments with AppleScript?




wrignj08
Jul 28, 2013, 02:38 AM
Hello All

I would like an AppleScript that would increase volume as if i was hitting Shift+options+F12. Does anyone know how to do this?

Cheers



HexMonkey
Jul 28, 2013, 04:22 AM
The following will approximate it (minus the visual and audio cues that it's changed):

set currentVolume to output volume of (get volume settings) -- in the range 0 to 100 (integer)
set newVolume to round (currentVolume * 64 / 100) -- convert to the range 0 to 64 (float)
if newVolume < 64 then set newVolume to newVolume + 1 -- increment the volume
set volume output volume (newVolume * 100 / 64) -- convert back to the range 0 to 100

The technical details are below, but they're not important if you're not too concerned about details.

Unfortunately it's more complicated than it should be. The actual system volume is an integer from 0 to 100, but the normal range with the up/down volume keys is 0 to 16, and when using option-shift-volume up/down the range is effectively 0 to 64 (as there are four increments for each square). As the actual volume is stored as an integer, numbers don't convert cleanly between ranges, so for example 0.25 squares is volume 1, 0.5 squares is volume 3, 0.75 squares is volume 4, etc. Apple's mapping between the two ranges doesn't seem to follow an obvious formula (a quarter of a square increase can correspond to an actual volume increase of 1, 2, or 3), so the above won't match it perfectly, but will be fairly close.

wrignj08
Jul 28, 2013, 04:27 AM
Thanks for that nice script and explanation, however I was hoping to get the graphical and audial feedback. I know you can do keystrokes with AppleScript however I'm unable to tell applescript to combine Shift+options+F12 successfully. Any help with that?

HexMonkey
Jul 28, 2013, 05:30 AM
Sorry, I misunderstood!

You can literally get Applescript to type shift-option-F12 like this:

tell application "System Events" to key code 111 using {option down, shift down}

Unfortunately, this won't do what you want; 111 corresponds to F12, but the media keys (volume up, volume down, etc) are treated as different keys. In theory there's a separate key code for the "volume up" key (72), but it doesn't work either. I can't seem to find any workaround, sorry. :(

wrignj08
Jul 28, 2013, 05:32 AM
Thanks Mate, that is were I was also stuck, I was thinking if I could tell it to also hold down the function key but i could not find the command for that either. :(

HexMonkey
Jul 28, 2013, 05:41 AM
Yeah, I had a look at that - unfortunately the key down command doesn't have any support for the function key, that I can see.