【问题标题】:Programmatically launch the Mac's System Preferences screen -> Sound以编程方式启动 Mac 的系统偏好设置屏幕 -> 声音
【发布时间】:2013-06-11 14:46:00
【问题描述】:

是否有一种编程方式可以在 Mac 上启动系统设置并将其集中在声音窗格上?

如果是这样,是否可以通过编程方式将其默认显示在“输出”选项卡上?

【问题讨论】:

    标签: macos cocoa operating-system


    【解决方案1】:

    我遇到了一个类似的问题:
    我想在“安全和隐私”系统首选项窗格中打开“隐私”选项卡。

    您可以使用以下命令从终端实现此目的:

    open x-apple.systempreferences:com.apple.preference.security?Privacy
    

    因此,我在我的应用程序中尝试了以下方法,但没有成功:

    // does not work!!!
    [[NSWorkspace sharedWorkspace] openURL:[NSURL fileURLWithPath:@"x-apple.systempreferences:com.apple.preference.security?Privacy"]];
    

    所以我实现了这种 hacky 解决方案:

    NSTask *task = [[NSTask alloc] init];
    task.launchPath = @"/bin/sh";
    task.arguments = @[@"-c" , @"open x-apple.systempreferences:com.apple.preference.security?Privacy"];
    [task launch];
    

    在 macOS 10.14.6 上测试。

    【讨论】:

    • 尝试不使用路径 (fileURLWithPath) 而是使用字符串创建 NSURL: [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString:urlString]];
    【解决方案2】:

    有一个更简单的方法:

    NSURL * url = [NSURL fileURLWithPath:@"/System/Library/PreferencePanes/Speech.prefPane"];
    [[NSWorkspace sharedWorkspace] openURL:url];
    

    【讨论】:

      【解决方案3】:

      你可以用applescript它:

      tell application "System Preferences"
          activate
          set the current pane to pane id "com.apple.preference.sound"
          reveal anchor "Output" of pane id "com.apple.preference.sound"
      end tell
      

      改编自this link

      【讨论】:

        猜你喜欢
        • 2011-11-05
        • 1970-01-01
        • 2011-08-25
        • 2010-09-15
        • 2012-10-19
        • 2016-07-27
        • 1970-01-01
        • 2018-01-17
        相关资源
        最近更新 更多