Saturday, January 7, 2012

AppleScript to Toggle usage of bluetooth speakers

Here is a script I wrote that works around a Mac OS bug on the Mac Book Pro. The issue I was seeing, was that after opening the lid to my laptop, a previously connected bluetooth speaker could no longer be connected to.

Workaround:
I found that if I simply ran "Bluetooth Setup Assistant" before selecting "Use as Audio Device" then I could reliably connect to the bluetooth speakers. I am using the Creative Labs D200 speakers, which work well.

Script:

-- Start "Bluetooth Setup Assistant to trigger bluetooth mode

-- For some reason this reliably leads to connecting to the bluetooth speaker

tell application "Bluetooth Setup Assistant"

activate

delay 3

quit

end tell

-- Toggle usage of Bluetooth speakers

tell application "System Events" to tell the front menu bar of process "SystemUIServer"

activate

delay 1

tell menu bar item 1

click

-- Un comment next line for diagnostic mode

-- set menustuff to entire contents of front menu

-- Specify your bluetooth speaker name here in place of "Creative D200"

tell menu item "Creative D200" of menu 1

click

tell menu item 1 of menu "Creative D200"

click

end tell

end tell

end tell

-- Workaround below: Only when running this script as an application

-- For some reason running this script as an application doesn't perform the final click above, so I added an additional menu click below which cause the previous click to take effect.

-- repeat to add another action

tell menu bar item 1

click

tell menu item 1 of menu 1

click

end tell

end tell

end tell

-- Un comment next line for diagnostic mode

--return menustuff

No comments:

Post a Comment