cfischer
cfischer

Reputation: 24892

Turn Bluetooth off and on programmatically

Is there a way to programmatically turn on and off the Bluetooth connection on OSX in a way that will be accepted by the Mac App Store?

From my previous question, I've found about blueutil, but it uses private APIs.

Upvotes: 1

Views: 3149

Answers (2)

Enrique Scherer
Enrique Scherer

Reputation: 21

This worked for me in 10.15.6, I might have over complicated my solution which is running script 1 (turn bluetooth off) and then script 2 (turn bluetooth on).

Script 1. This is for turning bluetooth OFF

tell application "System Events" to tell process "SystemUIServer"
    tell (menu bar item 1 of menu bar 1 where description is "bluetooth")
        click
        click menu item "Turn Bluetooth Off" of menu 1
    end tell
    tell window 1
        click button "Turn Bluetooth Off"
    end tell
end tell

Script 2. This is for turning bluetooth ON

tell application "System Events" to tell process "SystemUIServer"
    tell (menu bar item 1 of menu bar 1 where description is "bluetooth")
        click
        click menu item "Turn Bluetooth On" of menu 1
    end tell
end tell

So I execute one command which will run one script after the other, the sleep is for the UI to update properly.

osascript bluetooth_off.scpt && sleep 3s && osascript bluetooth_on.scpt

You can just save the command in a file and execute it using: (they have to be in the same directory).

~ bash <fileName>

Note: You will need to give access to the terminal on Preferences > Security & Privacy

Upvotes: 0

Rob Napier
Rob Napier

Reputation: 299275

It would be somewhat surprising if Apple approved an app that modified the user's antenna settings. It sounds like the kind of thing they typically frown on, no matter how you do it. But then, sometimes I get surprised.

You can definitely do it through Applescript:

tell application "System Preferences"
    set current pane to pane "com.apple.preferences.Bluetooth"
    tell application "System Events"
        tell process "System Preferences"
            set isOnCheckbox to checkbox "On" of window "Bluetooth"
            if value of isOnCheckbox is 0 then
                click isOnCheckbox
            end if
        end tell
    end tell
    quit
end tell

Note that this will take over System Preferences and at the end close it even if the user was running it. That's not the best user experience, and I definitely wouldn't do it without first warning the user. But of course, I wouldn't recommend modifying the bluetooth settings without warning the user.


EDIT

Because you asked, I'll take a moment to rant here....

Regarding how to learn to read and write the above, first note that it, like most AppleScript I write professionally, was cobbled together from google searches and experimentation. I'm a purist programmer at heart, and I believe in really understanding the technology you use. Even I cobble together things in AppleScript until they "kind of work."

I wish there were a really good document. Of course there's the language guide, but it's kind of like learning Cocoa from the ObjC language definition. My current recommendations are Beginning AppleScript and then AppleScript: The Definitive Guide. Neuburg in particular does not sugarcoat the language or pretend that it makes sense. Applescript, even worse than the original COBOL (ADD X TO Y GIVING Z), is very hard to write because it tries so hard to be easy. I love and respect many languages. AppleScript is crap.

It is, however, the most supported way to interact with most of the Mac system functions, so a good Mac developer needs to at least be able to get by in it. Even if you use the new ScriptingBridge via ObjC (or MacRuby), the underlying object model is still AppleScript based. In my experience, to get ScriptingBridge code to work well, you generally have to write it first in AppleScript and then translate it into Cocoa.

Upvotes: 8

Related Questions