8

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

3
  • 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).
    – Wilco
    Commented Oct 1, 2013 at 23:15
  • 1
    Please check this.
    – mu3
    Commented Oct 15, 2017 at 16:35
  • @mu3 can you add that as an actual answer? I almost missed that there's a solution to this question.
    – Jared
    Commented Mar 28, 2018 at 19:50

1 Answer 1

6

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 :)

2
  • Worked great, and it integrates with no changes into an Alfred Workflow. Many thanks 🙌
    – Gabriel R.
    Commented Feb 4, 2020 at 9:00
  • 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. Commented May 29, 2020 at 22:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .