【问题标题】:osx- opening Privacy>Accessibility window programmaticallyosx-以编程方式打开隐私>辅助功能窗口
【发布时间】:2015-03-07 12:36:38
【问题描述】:

我正在开发需要从“系统偏好设置”>“安全和隐私”>“隐私”>“可访问性”启用的应用程序。

现在,我正在使用以下代码打开如下屏幕截图所示的窗口:

-(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];
}

但它不一定会打开“辅助功能”选项卡。相反,它会打开以前打开的选项卡。

所以请建议我修改此代码的方法,该代码将从该窗口的侧面菜单中专门打开“辅助功能”选项卡。

提前致谢。

【问题讨论】:

    标签: macos osx-mavericks osx-yosemite


    【解决方案1】:

    在搜索解决方案时,我发现根据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];
    }
    

    【讨论】:

      【解决方案2】:

      找到更简单的解决方案:

      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]];
      

      【讨论】:

      • 接受的答案对我不起作用,但这个答案对我有用。谢谢!
      【解决方案3】:

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

      // 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"]];
      

      【讨论】:

        【解决方案4】:

        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)
        }
        

        【讨论】:

          【解决方案5】:

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

          如果你知道如何从命令行打开它,你可以自定义你的编程方式

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-04-05
            • 2023-04-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多