我知道我可以像这样打开“安全和隐私”首选项窗格:
open /System/Library/PreferencePanes/Security.prefPane
是否可以以编程方式导航到“隐私”选项卡?我想让用户轻松找到合适的屏幕。请注意,Accessibility API 目前被禁用,这就是我试图在“隐私”选项卡上启用的功能。(这是10.9 中的新功能。)
我知道我可以像这样打开“安全和隐私”首选项窗格:
open /System/Library/PreferencePanes/Security.prefPane
是否可以以编程方式导航到“隐私”选项卡?我想让用户轻松找到合适的屏幕。请注意,Accessibility API 目前被禁用,这就是我试图在“隐私”选项卡上启用的功能。(这是10.9 中的新功能。)
我从您对您已经发现的另一个主题的回答中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
一种选择是使用 Applescript Editor 将其保存到 applescript 文件并直接执行:
osascript path/to/applescript.scpt
您还可以通过脚本桥从您的 Objective C 应用程序代码执行等效命令。这涉及到您需要构建一个 Objective C 标头(使用 Apple 的命令行工具),将 System Preferences 脚本 API 公开为可编写脚本的对象。(有关如何构建标头的详细信息,请参阅Apple 的 Scripting Bridge 文档。)
编辑:一旦你建立了一个系统偏好头,下面的目标 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];
但是请注意(至少我上次检查过)Scripting Bridge 不能被沙盒应用程序使用。