Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing keyboard volume control buttons in Applescript - setting volume inside loop doesn't work

Background

I have a MacAlly IceKey keyboard. This keyboard has volume buttons that require a driver to function. This driver has not been updated since 2006, and I suspect it as the source of a recent spat of recurring kernel panics I've been experiencing under Mac OS X 10.6.1. So, out it goes; but I want my volume keys back!

Using the wonderful ControllerMate, I can program these keys to do anything, including run an applescript script. So, I'm trying to implement that functionality.

The set volume command (part of Standard Additions) allows you to set the volume to any value between 0 and 100. The Apple keyboard volume keys allow a total of 17 volume settings to be selected (including 0). I figure the simplest way to duplicate this behavior is to keep a list of allowed volume settings and grab the next largest (or smallest) one out of that.

The problem

It doesn't work. The following script:

set volumesList to {0, 6, 12, 18, 25, 31, 37, 43, 50, 56, 62, 68, 75, 81, 87, 93, 100}
set sysVolume to get volume settings

repeat with curVolume in volumesList
    if (curVolume > (output volume of sysVolume)) then
        set volume output volume (contents of curVolume)
        exit repeat
    end if
end repeat

get volume settings

...only works if the system volume level happens to be less than 43. The system seems to interpret "50" as "49"; that's as high as the volume will go with my script. If the volume starts higher than 50, my script has no effect. The kicker? If the "exit repeat" statement is removed, the system volume gets set to 100 - just as you'd expect.

(Good grief, AppleScript is weird sometimes.)

Any ideas?

Bonus Points

It would be super awesome to get this to display the volume overlay as well. Does anyone know how to accomplish that? It doesn't even need to be through AppleScript; I'm happy sticking some Cocoa code in a command-line tool if that's what it takes.

like image 586
Ryan Ballantyne Avatar asked Sep 19 '09 21:09

Ryan Ballantyne


2 Answers

I have no idea how to get the translucent overlay to show up, but this at least plays the system beep while turning the volume up:

set currentVolume to output volume of (get volume settings)
set newVolume to (currentVolume + (100 / 17)) as integer
set volume output volume newVolume
beep

Replace the + with a - in your volume-down script.

set volume output seems to be automatically adjust values outside the (0, 100) limit.

Update: You could use Growl's AppleScript support to show some kind of overlay:

tell application "GrowlHelperApp"

    register as application "Volume Change" ¬
        all notifications {"Volume Change"} ¬
        default notifications {"Volume Change"} ¬
        icon of application "Script Editor"

    notify with name "Volume Change" ¬
        title "Volume Up" ¬
        description "Volume is now " & output volume of (get volume settings) ¬
        application name "Volume Change"

end tell
like image 190
a paid nerd Avatar answered Oct 21 '22 13:10

a paid nerd


You can also use the built in notification

set vol to ((output volume of (get volume settings)) + 5)
if (vol > 100) then set vol to 100
set volume output volume (vol)

display notification ((vol) as string)
like image 41
Chris Avatar answered Oct 21 '22 13:10

Chris