Applescript to connect to bluetooth device

6,111

I've manage to do it thanks to this link provided by @mu3 in the comments. Here is the Apple Script :

activate application "SystemUIServer"
tell application "System Events"
    tell process "SystemUIServer"
        -- Working CONNECT Script.  Goes through the following:
        -- Clicks on Bluetooth Menu (OSX Top Menu Bar)
        --    => Clicks on device Item
        --      => Clicks on Connect Item
        set btMenu to (menu bar item 1 of menu bar 1 whose description contains "bluetooth")
        tell btMenu
            click
            tell (menu item "Beats Solo³ de Anthonin" of menu 1)
                click
                if exists menu item "Connect" of menu 1 then
                    click menu item "Connect" of menu 1
                    return "Connecting..."
                else
                    key code 53 -- Close main BT drop down if Connect wasn't present
                    return "Connect menu was not found, are you already connected?"
                end if
            end tell
        end tell
    end tell
end tell

All you have to do is replacing "Beats Solo³ de Anthonin" with your device name, and if your computer isn't in English, replace "Connect" with its translation in your language.

Hope this helps :)

Share:
6,111

Related videos on Youtube

mhorgan
Author by

mhorgan

Updated on September 18, 2022

Comments

  • mhorgan
    mhorgan over 1 year

    I'm trying to create an applescript to allow me to connect to a bluetooth device by it's Bluetooth ID.

    So far, i've managed to get an applescript to turn on bluetooth if it's off. Here's the code:

    # This is only necessary, if AppleScripts are not yet allowed to change checkboxes
    tell application "System Events" to set UI elements enabled to true
    # Now change the bluetooth status
      tell application "System Preferences"
        set current pane to pane id "com.apple.preferences.bluetooth"
          tell application "System Events"
            tell process "System Preferences"
            # Enabled is checkbox number 2
            if value of checkbox 2 of window "Bluetooth" is 0 then
                click checkbox 2 of window "Bluetooth"
            end if
        end tell
    end tell
    quit
    end tell
    

    Would someone know if and how it's possible to set up a new bluetooth device and if it'd be posible to connect to a device based on it's device name/ it's device bluetooth ID?

    I've also tried to record the action in Automator but for the "set up new device" option, Automator just tells me: "click on "" button". Thanks

    • doubleE
      doubleE over 10 years
      Found your question via Google because I'm hoping to find a solution as well. If I have any luck I'll be sure to post an answer here (though things aren't looking promising).
    • mu3
      mu3 over 6 years
      Please check this.
    • Jared
      Jared about 6 years
      @mu3 can you add that as an actual answer? I almost missed that there's a solution to this question.
  • Gabriel R.
    Gabriel R. over 4 years
    Worked great, and it integrates with no changes into an Alfred Workflow. Many thanks 🙌
  • Oskar Austegard
    Oskar Austegard almost 4 years
    Just FYI - I found this to work GREAT on a 2017 MBP running Mojave, but it did not work on my 2011 iMac running High Sierra - the Connect menu item just isn't present.