macoscocoaapplescriptsystem-preferencesnspreferencepane

Defining an anchor name within my preference pane


I would like enable AppleScript to reveal a particular tab within a custom preference pane I am developing, so that this works:

tell application "System Preferences" 
  reveal anchor "Foo" of pane id "com.example.preferences.Bar"
end tell

I cannot find anywhere how my preference pane would declare or specify the "Foo" anchor or associate that with any particular tab view item.


Solution

  • For anyone that comes to this question with the amount of frustration I did, I'm documenting here the steps to be able to get anchors for your custom NSPreferencePane.

    The greatest hint was from @geowar:

    Google GPGPreferences.searchTerms dictionary revealElementForKey for some clues… (AFAIK this isn't documented anywhere…)

    I overlooked this comment the first time I came to this question, the second time I looked at the GPGPreferences.m file, more specifically the revealElementForKey method but couldn't figure out how the function actually got called.

    The keyword was searchTerms.

    I only realized this when I finally found this article SearchablePreferencePanes.

    Steps:

    1. Add a NSPrefPaneSearchParameters key to the Info.plist of your preference pane bundle with a value something like MyPreferencePane.
    2. Create a file called MyPreferencePane.searchTerms in your Xcode project. The contents of this file should be a property list containing a dictionary that looks something like the one below (taken from the Sound preference pane). This will not only allow users to get a hit for your preference pane when searching for the specified terms, but it will also generate the anchors for the preference pane.
    3. Implement the revealElementForKey(key: String) method in your class that inherits from NSPreferencePane. This method will be called both when the user opens the pane after searching for a term and also when calling the reveal() method for the ScriptingBridge anchor class.
    4. Boom!

    Enjoy life.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>effects</key>
        <dict>
            <key>localizableStrings</key>
            <array>
                <dict>
                    <key>comments</key>
                    <string>Localizable index words</string>
                    <key>index</key>
                    <string>noises, audio, hear, level, volume, louder, softer, quieter, increase, decrease, raise, lower, speaker, beep, warning, bell, ding, ring, hearing, beeping, dinging, ringing</string>
                    <key>title</key>
                    <string>Alerts and sound effects</string>
                </dict>
                <dict>
                    <key>comments</key>
                    <string>Localizable index words</string>
                    <key>index</key>
                    <string>audio, hear, level, volume, louder, softer, quieter, increase, decrease, raise, lower, music, speakers, hearing</string>
                    <key>title</key>
                    <string>Sound volume</string>
                </dict>
                <dict>
                    <key>comments</key>
                    <string>Localizable index words</string>
                    <key>index</key>
                    <string>audio, hear, level, volume, louder, softer, quieter, increase, decrease, raise, lower, quiet, silent, silence, turn off, hearing, muting</string>
                    <key>title</key>
                    <string>Mute the sound</string>
                </dict>
                <dict>
                    <key>comments</key>
                    <string>Localizable index words</string>
                    <key>index</key>
                    <string>menubar</string>
                    <key>title</key>
                    <string>Show volume in menu bar</string>
                </dict>
            </array>
        </dict>
        <key>input</key>
        <dict>
            <key>localizableStrings</key>
            <array>
                <dict>
                    <key>comments</key>
                    <string>Localizable index words</string>
                    <key>index</key>
                    <string>audio, hear, level, volume, louder, softer, quieter, increase, decrease, raise, lower, devices, sources, microphones, instruments, MIDI, record, line in, hearing, recording</string>
                    <key>title</key>
                    <string>Sound input</string>
                </dict>
            </array>
        </dict>
        <key>output</key>
        <dict>
            <key>localizableStrings</key>
            <array>
                <dict>
                    <key>comments</key>
                    <string>Localizable index words</string>
                    <key>index</key>
                    <string>devices, headphones, headsets, speakers, hear, balance, hearing, head phones, head sets</string>
                    <key>title</key>
                    <string>Sound output</string>
                </dict>
                <dict>
                    <key>comments</key>
                    <string>Localizable index words</string>
                    <key>index</key>
                    <string>airplay, speakers</string>
                    <key>title</key>
                    <string>AirPlay audio streaming</string>
                </dict>
            </array>
        </dict>
    </dict>
    </plist>