osx- 以编程方式打开隐私>辅助功能 window

osx- opening Privacy>Accessibility window programmatically

我正在开发需要从“系统偏好设置”>“安全和隐私”>“隐私”>“辅助功能”中启用的应用程序。

现在,我正在使用以下代码打开下面屏幕截图中显示的 window:

-(IBAction)enableAccessibility
{
NSString *script = @"tell application \"System Preferences\" \n reveal anchor \"Privacy\" of pane id \"com.apple.preference.security\" \n activate \n end tell";

NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource:script];
[scriptObject executeAndReturnError:nil];
}

但不一定会打开 "Accessibility" 选项卡。相反,它会打开之前打开的选项卡。

所以请建议我修改此代码的方法,该代码将专门打开此 window 侧边菜单中的 "Accessibility" 选项卡。

提前致谢。

在搜索解决方案时,我发现根据 this question 中的一些提示生成了以下代码,对我有用。

这就是我想要实现的。

感谢@duskwuff 的评论支持。

NSString *script;
if ([[[[NSProcessInfo processInfo] operatingSystemVersionString] substringToIndex:12] isEqualToString:@"Version 10.7"] || [[[[NSProcessInfo processInfo] operatingSystemVersionString] substringToIndex:12] isEqualToString:@"Version 10.8"])
{ //>> For OSX 10.7 and 10.8
     script = @"tell application \"System Preferences\" \n set the current pane to pane id \"com.apple.preference.universalaccess\" \n activate \n end tell";

     NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource:script];
     [scriptObject executeAndReturnError:nil];

}
else
{ //>> For OSX 10.9 and 10.10
    script = @"tell application \"System Preferences\" \n reveal anchor \"Privacy_Accessibility\" of pane id \"com.apple.preference.security\" \n activate \n end tell";

    NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource:script];
    [scriptObject executeAndReturnError:nil];
}

找到更简单的解决方案:

NSURL *URL = [NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility"];
[[NSWorkspace sharedWorkspace] openURL:URL];

如果您只需要打开 prefPane

NSURL *URL = [NSURL URLWithString:@"/System/Library/PreferencePanes/Security.prefPane"];
[[NSWorkspace sharedWorkspace] openFile:[URL relativePath]];

https://macosxautomation.com/system-prefs-links.html 有一个页面链接到许多,但不是全部,各种首选项面板。通过一些猜测,我能够验证这些调用在 macOS Mojave beta 7 下是否有效。当用户拒绝访问该应用程序无法访问的设备时,我正在使用这些调用将用户引导至正确的窗格t 运行 没有。

// open last Privacy subpane viewed:
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy"]];

// specify a particular subpange
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_Camera"]];
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_Microphone"]];
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_Automation"]];

Swift 4兼容版本,采纳自已接受的答案:

static func openAccessibilityPreferences() {
    let macOS10version = ProcessInfo.processInfo.operatingSystemVersion.minorVersion

    let script = macOS10version < 9
        ? "tell application \"System Preferences\" \n set the current pane to pane id \"com.apple.preference.universalaccess\" \n activate \n end tell"
        : "tell application \"System Preferences\" \n reveal anchor \"Privacy_Accessibility\" of pane id \"com.apple.preference.security\" \n activate \n end tell"

    NSAppleScript(source: script)?.executeAndReturnError(nil)
}

shell命令的一种方式是
/bin/sh -c open -b com.apple.systempreferences /System/Library/PreferencePanes/Security.prefPane

如果您知道如何从 command-line 打开它,您可以自定义您的编程方式