Cocoa按钮打开“系统首选项”页面

时间:2011-07-11 15:31:07

标签: cocoa macos preferences

在OSX Cocoa应用程序中,我想要一个可以打开“语音”首选项窗格的按钮。那可能吗?我只是想节省他们去系统偏好设置的时间>演讲>文字转语音

3 个答案:

答案 0 :(得分:18)

以下是一种相当简单(可靠)的方法,至少可以将系统偏好设置打开到Speech.prefPane:

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

但是,它不一定会切换到Text to Speech标签,而是用户选择的最后一个标签。

实际上也可以切换到Text to Speech选项卡,但它更复杂一些。您可以使用AppleScript将命令发送到“系统偏好设置”应用程序,但使用ScriptingBridge.framework(请参阅Scripting Bridge Programming Guide)要快得多。

您需要将ScriptingBridge.framework添加到项目中,然后在终端中使用如下命令生成SBSystemPreferences.h头文件以供使用:

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

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

- (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

答案 1 :(得分:7)

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

积分转到以下网站:https://macosxautomation.com/system-prefs-links.html

供参考的网址列表:

Accessibility Preference Pane
Main    x-apple.systempreferences:com.apple.preference.universalaccess
Display x-apple.systempreferences:com.apple.preference.universalaccess?Seeing_Display
Zoom    x-apple.systempreferences:com.apple.preference.universalaccess?Seeing_Zoom
VoiceOver   x-apple.systempreferences:com.apple.preference.universalaccess?Seeing_VoiceOver
Descriptions    x-apple.systempreferences:com.apple.preference.universalaccess?Media_Descriptions
Captions    x-apple.systempreferences:com.apple.preference.universalaccess?Captioning
Audio   x-apple.systempreferences:com.apple.preference.universalaccess?Hearing
Keyboard    x-apple.systempreferences:com.apple.preference.universalaccess?Keyboard
Mouse & Trackpad    x-apple.systempreferences:com.apple.preference.universalaccess?Mouse
Switch Control  x-apple.systempreferences:com.apple.preference.universalaccess?Switch
Dictation   x-apple.systempreferences:com.apple.preference.universalaccess?SpeakableItems
Security & Privacy Preference Pane
Main    x-apple.systempreferences:com.apple.preference.security
General x-apple.systempreferences:com.apple.preference.security?General
FileVault   x-apple.systempreferences:com.apple.preference.security?FDE
Firewall    x-apple.systempreferences:com.apple.preference.security?Firewall
Advanced    x-apple.systempreferences:com.apple.preference.security?Advanced
Privacy x-apple.systempreferences:com.apple.preference.security?Privacy
Privacy-Camera x-apple.systempreferences:com.apple.preference.security?Privacy_Camera
Privacy-Microphone  x-apple.systempreferences:com.apple.preference.security?Privacy_Microphone
Privacy-Automation  x-apple.systempreferences:com.apple.preference.security?Privacy_Automation
Privacy-AllFiles    x-apple.systempreferences:com.apple.preference.security?Privacy_AllFiles
Privacy-Accessibility   x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility
Privacy-Assistive   x-apple.systempreferences:com.apple.preference.security?Privacy_Assistive
Privacy-Location Services   x-apple.systempreferences:com.apple.preference.security?Privacy_LocationServices
Privacy-SystemServices  x-apple.systempreferences:com.apple.preference.security?Privacy_SystemServices
Privacy-Advertising x-apple.systempreferences:com.apple.preference.security?Privacy_Advertising
Privacy-Contacts    x-apple.systempreferences:com.apple.preference.security?Privacy_Contacts
Privacy-Diagnostics & Usage x-apple.systempreferences:com.apple.preference.security?Privacy_Diagnostics
Privacy-Calendars   x-apple.systempreferences:com.apple.preference.security?Privacy_Calendars
Privacy-Reminders   x-apple.systempreferences:com.apple.preference.security?Privacy_Reminders
Privacy-Facebook    x-apple.systempreferences:com.apple.preference.security?Privacy_Facebook
Privacy-LinkedIn    x-apple.systempreferences:com.apple.preference.security?Privacy_LinkedIn
Privacy-Twitter x-apple.systempreferences:com.apple.preference.security?Privacy_Twitter
Privacy-Weibo   x-apple.systempreferences:com.apple.preference.security?Privacy_Weibo
Privacy-Tencent Weibo   x-apple.systempreferences:com.apple.preference.security?Privacy_TencentWeibo
Dictation & Speech Preference Pane
Dictation   x-apple.systempreferences:com.apple.preference.speech?Dictation
Text to Speech  x-apple.systempreferences:com.apple.preference.speech?TTS
Sharing Preference Pane
Main    x-apple.systempreferences:com.apple.preferences.sharing
Screen Sharing  x-apple.systempreferences:com.apple.preferences.sharing?Services_ScreenSharing
File Sharing    x-apple.systempreferences:com.apple.preferences.sharing?Services_PersonalFileSharing
Printer Sharing x-apple.systempreferences:com.apple.preferences.sharing?Services_PrinterSharing
Remote Login    x-apple.systempreferences:com.apple.preferences.sharing?Services_RemoteLogin
Remote Management   x-apple.systempreferences:com.apple.preferences.sharing?Services_ARDService
Remote Apple Events x-apple.systempreferences:com.apple.preferences.sharing?Services_RemoteAppleEvent
Internet Sharing    x-apple.systempreferences:com.apple.preferences.sharing?Internet
Bluetooth Sharing   x-apple.systempreferences:com.apple.preferences.sharing?Services_BluetoothSharing

所有这一切都可以归功于系统Preferences.app中的preferencePane + CFBundleURLTypes(CFBundleURLSchemes)x-apple.systempreferences(Info.plist)中的Info.plist中的键

  

NSPrefPaneAllowsXAppleSystemPreferencesURLScheme

截至10.14 Mojave,一些隐私密钥不复存在。 Mojave隐私列表:enter image description here

答案 2 :(得分:0)

对于遇到我在评论中提到的同一问题的人,只需转到~/Desktop(我指定此位置),您将看到SBSystemPreferences.h已创建。

但是,在此标头中,缺少某些类声明。所以你必须明确地添加这个声明。

在我的情况下,类“item”未定义。所以加上这个: @class SBSystemPreferencesItem;

然后编译它并查看仍然缺少的内容,需要添加哪个声明。