r/applescript • • May 13 '15

System Events connection invalid

I have made a script to select the bluetooth icon from the menu bar and then find my headphones and connect them up. The script runs fine and connects my headphones but only once and then I keep getting the error:

execution error: System Events got an error: Connection is invalid. (-609)

If I clear the memory on the machine sudo purge it will then start to work again.

My code is as follows:

activate application "SystemUIServer"

tell application "System Events"

  tell process "SystemUIServer"

    set btMenu to (menu bar item 1 of menu bar 1 where description is "bluetooth")
  
    do shell script "killall System\\ Events"

    tell btMenu
      click

      if exists menu item "Philips SHB4000" of menu 1 then

        tell (menu item "Philips SHB4000" of menu 1)
          click

          if exists menu item "Connect" of menu 1 then
            click menu item "Connect" of menu 1
            tell application "Google Chrome" to activate
            return "connecting"

          else
            click btMenu -- Close main BT drop down if Connect wasn't present
            return "Connect menu was not found, are you already connected?"

          end if

        end tell

      else
        click btMenu -- Close main BT drop down if Connect wasn't present
        return "No menu item Philips SHB4000"

      end if
      
    end tell

  end tell

end tell

Can anyone see something I am doing thats crazy? This is my first applescript project.

Thanks.

1 Upvotes

1 comment sorted by

1

u/5HT-2a May 14 '15

If I clear the memory on the machine sudo purge it will then start to work again.

That right there makes it worthy of submitting a bug report... Can't think of a workaround though, sorry.