Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

wrignj08

macrumors newbie
Original poster
Jun 13, 2009
28
0
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

Administrator emeritus
Feb 5, 2004
2,240
504
New Zealand
The following will approximate it (minus the visual and audio cues that it's changed):

Code:
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

macrumors newbie
Original poster
Jun 13, 2009
28
0
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

Administrator emeritus
Feb 5, 2004
2,240
504
New Zealand
Sorry, I misunderstood!

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

Code:
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

macrumors newbie
Original poster
Jun 13, 2009
28
0
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

Administrator emeritus
Feb 5, 2004
2,240
504
New Zealand
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.