Cocoa按钮打开“系统首选项”页面 [英] Cocoa button opens a System Preference page

查看:169
本文介绍了Cocoa按钮打开“系统首选项”页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OSX Cocoa应用程式中,我想要一个可开启「语音」偏好设定窗格的按钮。那可能吗?我只是试图保存他们的时间去系统偏好设置>语音>文本到语音

In a OSX Cocoa app, I would like a button that would open the "speech" preference pane. Is that possible? I'm just trying to save them the time to go System Preferences > Speech > Text to Speech

推荐答案

至少获得系统首选项打开到Speech.prefPane:

The following is a fairly easy (and reliable) way to at least get System Preferences open to the Speech.prefPane:

- (IBAction)openSpeechPrefs:(id)sender {
    [[NSWorkspace sharedWorkspace] openURL:
     [NSURL fileURLWithPath:@"/System/Library/PreferencePanes/Speech.prefPane"]];
}

但是,它不一定会切换到

However, it won't necessarily be switched to the Text to Speech tab, but rather the last tab the user had selected.

可以实际切换到文本到语音选项卡,而不是用户选择的最后一个选项卡。以及,但它有点更多的参与。您可以使用AppleScript将命令发送到系统首选项应用程序,但使用 ScriptingBridge.framework (请参阅脚本桥程序设计指南)速度更快。

It is possible to actually switch to the Text to Speech tab as well, but it's a bit more involved. You can use AppleScript to send commands to the System Preferences application, but using the ScriptingBridge.framework (See Scripting Bridge Programming Guide) is much faster.

需要向您的项目中添加 ScriptingBridge.framework ,然后在Terminal中使用类似以下命令来生成 SBSystemPreferences.h 头文件使用:

You'll need to add the ScriptingBridge.framework to your project, and then use a command like the following in Terminal to generate a SBSystemPreferences.h header file to work with:

sdef/ Applications / System Preferences.app| sdp -fh --basename SBSystemPreferences -o〜/ Desktop / SBSystemPreferences.h

添加 SBSystemPreferences.h 标题到您的项目,然后将 -openSpeechPrefs:更改为以下内容:

Add that SBSystemPreferences.h header to your project, then change -openSpeechPrefs: to the following:

- (IBAction)openSpeechPrefs:(id)sender {
    SBSystemPreferencesApplication *systemPrefs = 
    [SBApplication applicationWithBundleIdentifier:@"com.apple.systempreferences"];

    [systemPrefs activate];

    SBElementArray *panes = [systemPrefs panes];
    SBSystemPreferencesPane *speechPane = nil;

    for (SBSystemPreferencesPane *pane in panes) {
        if ([[pane id] isEqualToString:@"com.apple.preference.speech"]) {
            speechPane = pane;
            break;
        }
    }
    [systemPrefs setCurrentPane:speechPane];

    SBElementArray *anchors = [speechPane anchors];

    for (SBSystemPreferencesAnchor *anchor in anchors) {
        if ([anchor.name isEqualToString:@"TTS"]) {
            [anchor reveal];
        }
    }
}

编辑:

使用ScriptingBridge.framework方法的示例项目:
http://github.com/ NSGod / OpenSystemPrefsTTS

Sample project using the ScriptingBridge.framework method: http://github.com/NSGod/OpenSystemPrefsTTS

这篇关于Cocoa按钮打开“系统首选项”页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆