cocoamacospreferences

Cocoa button opens a System Preference page


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


Solution

  • 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.

    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.

    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

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

    EDIT:

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