Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top