AppleScript to fix a Plantronics headset issue in Skype

Jun 19, '09 07:30:01AM

Contributed by: bdobes

I use Skype on my MacBook all day for work, and have always wanted to use a Bluetooth headset with it, but always ran into bad quality. I finally found a headset that worked well, the Plantronics Voyager Pro. However, there was still an issue in that it wouldn't disconnect after hanging up the call, causing the battery on both the MacBook and the earpiece to drain way too quickly.

To remedy this, I wrote this AppleScript, which I run with a keystroke (via FastScripts) to hang up a Skype call and disconnect the bluetooth headset. Here's the script:

tell application "Skype" to activate
menu_click({"Skype", "Call", "Hang Up"})

tell application "System Events"
  tell process "SystemUIServer"
    tell (1st menu bar item of menu bar 1 whose value of attribute "AXDescription" is "bluetooth")
      click
      tell menu item "PROPlantronics" of front menu
        click
        --delay 0.5
        tell menu item "Disconnect" of menu "ProPlantronics" to click
      end tell
    end tell
  end tell
end tell

-- `menu_click`, by Jacob Rus, September 2006
-- 
-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
-- Execute the specified menu item.  In this case, assuming the Finder-- is the active application, arranging the frontmost folder by date.

on menu_click(mList)
  local appName, topMenu, r
  
  -- Validate our input
  if mList's length < 3 then error "Menu list is not long enough"
  
  -- Set these variables for clarity and brevity later on
  set {appName, topMenu} to (items 1 through 2 of mList)
  set r to (items 3 through (mList's length) of mList)
  
  -- This overly-long line calls the menu_recurse function with
  -- two arguments: r, and a reference to the top-level menu
  tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
    (menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click

on menu_click_recurse(mList, parentObject)
  local f, r
  
  -- `f` = first item, `r` = rest of items
  set f to item 1 of mList
  if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
  
  -- either actually click the menu item, or recurse again
  tell application "System Events"
    if mList's length is 1 then
      click parentObject's menu item f
    else
      my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
    end if
  end tell
end menu_click_recurse
With this script, the next time I answer or make a new call, it reconnects with no problems. (Thanks to Jacob Rus for the menu_click function).

Comments (0)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20090618084126955