我可以自动将用户导航到系统偏好设置➞隐私吗?

zou*_*oul 7 macos accessibility osx-mavericks

我知道我可以打开安全和隐私偏好设置面板,如下所示:

open /System/Library/PreferencePanes/Security.prefPane
Run Code Online (Sandbox Code Playgroud)

是否可以以编程方式导航到"隐私"选项卡?我想让用户轻松找到合适的屏幕.请注意,此时禁用了辅助功能API,这就是我要在"隐私"选项卡上启用的功能.(这是10.9中新功能.)

Alu*_*tor 7

我从对已经发现的其他主题的回答中看到AXProcessIsTrustedWithOptions,这将使用户直接进入隐私可访问性设置; 你可能想要实现你自己的用户提示,这个提示比那个函数提供的官方警报更令人困惑和怀疑.

您可以打开"安全和隐私"偏好设置面板,然后使用Applescript直接导航到"辅助功能"部分:

tell application "System Preferences"
    --get a reference to the Security & Privacy preferences pane
    set securityPane to pane id "com.apple.preference.security"

    --tell that pane to navigate to its "Accessibility" section under its Privacy tab
    --(the anchor name is arbitrary and does not imply a meaningful hierarchy.)
    tell securityPane to reveal anchor "Privacy_Accessibility"

    --open the preferences window and make it frontmost
    activate
end tell
Run Code Online (Sandbox Code Playgroud)

一种选择是使用Applescript Editor将其保存到applescript文件并直接执行:

osascript path/to/applescript.scpt
Run Code Online (Sandbox Code Playgroud)

您还可以通过Scripting Bridge从Objective C应用程序代码执行等效命令.这需要更多一点,您需要构建一个Objective C标头(使用Apple的命令行工具),它将System Preferences脚本API公开为可编写脚本的对象.(有关如何构建标头的详细信息,请参阅Apple的Scripting Bridge文档.)


编辑:一旦您构建了系统首选项标头,以下Objective C代码将执行与上面的Applescript相同的工作:

//Get a reference we can use to send scripting messages to System Preferences.
//This will not launch the application or establish a connection to it until we start sending it commands.
SystemPreferencesApplication *prefsApp = [SBApplication applicationWithBundleIdentifier: @"com.apple.systempreferences"];

//Tell the scripting bridge wrapper not to block this thread while waiting for replies from the other process.
//(The commands we'll be sending it don't have return values that we care about.)
prefsApp.sendMode = kAENoReply;

//Get a reference to the accessibility anchor within the Security & Privacy pane.
//If the pane or the anchor don't exist (e.g. they get renamed in a future OS X version),
//we'll still get objects for them but any commands sent to those objects will silently fail.
SystemPreferencesPane *securityPane = [prefsApp.panes objectWithID: @"com.apple.preference.security"];
SystemPreferencesAnchor *accessibilityAnchor = [securityPane.anchors objectWithName: @"Privacy_Accessibility"];

//Open the System Preferences application and bring its window to the foreground.
[prefsApp activate];

//Show the accessibility anchor, if it exists.
[accessibilityAnchor reveal];
Run Code Online (Sandbox Code Playgroud)

但请注意(最后我检查过,至少)沙盒应用程序无法使用脚本桥.