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

时间:2013-08-08 08:36:13

标签: macos accessibility osx-mavericks

我知道我可以打开安全&隐私偏好窗格如下:

open /System/Library/PreferencePanes/Security.prefPane

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

1 个答案:

答案 0 :(得分:7)

我从your answer to the other topic看到您已经发现AXProcessIsTrustedWithOptions,这会让用户直接进入隐私辅助功能设置;你可能想要实现自己的用户提示,这种提示比the official alert provided by that function更令人困惑和怀疑。

您可以打开安全&隐私首选项窗格并使用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

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

osascript path/to/applescript.scpt

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


编辑:构建系统偏好设置标头后,以下目标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];

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