I am attempting to open System Preferences from a Sandboxed macOS application, so that the user can manually enable a certain preference.
AppleScript
activate
Brings an application to the front, launching it if necessary.
I am running macOS 10.13.6 and Xcode 10.0. The Deployment Target is set to OS X 10.11.
I run the following AppleScript from a sandboxed app:
let script = """
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preferences.extensions"
end tell
"""
let appleScript = NSAppleScript(source: script)
var errorInfo: NSDictionary? = nil
appleScript?.executeAndReturnError(&errorInfo)
I also add a scripting targets Sandbox Entitlement enabling System Preferences to reveal panes:
<key>com.apple.security.scripting-targets</key>
<dict>
<key>com.apple.systempreferences</key>
<array>
<string>preferencepane.reveal</string>
</array>
</dict>
This somewhat works, once I add the Entitlement. The Systems Preferences app launches if closed and opens the correct pane.
The errorInfo
dictionary is nil and presents no error info.
In the console I see a message, which is perhaps unrelated log spam:
AppleEvents: received mach msg which wasn't complex type as expected in getMemoryReference.
However, the app does not activate. The System Preferences window does not come to the front. The app navigates to the correct tab, but does not come to the foreground as the docs for activate
state. It remains behind any other foreground windows.
In the case where System Preferences is closed, the app does launch. But the window is not added to the screen in the foreground or background until you click the dock icon. The window then appears, already navigated to the correct pane.
Why does the app not activate?
Are there any other needed Sandbox Entitlements that will allow for activation of the System Preferences window?