在我的首选项窗格中定义锚点名称

Defining an anchor name within my preference pane

我想启用 AppleScript 以显示我正在开发的自定义首选项窗格中的特定选项卡,这样就可以了:

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

我无法在任何地方找到我的首选项窗格如何声明或指定 "Foo" 锚点或将其与任何特定选项卡视图项目相关联。

首先,您必须确保您搜索的是锚点,而不是其他类型的对象。 然后使用下面的脚本获取每个锚点名称:(在此示例中,获取声音首选项的锚点将给出 "output"、"input"、..)

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

对于任何遇到这个问题而感到沮丧的人,我在这里记录了能够为您的自定义设置锚点的步骤 NSPreferencePane

最大的提示来自@geowar:

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

我第一次遇到这个问题时忽略了这个评论,第二次我查看了 GPGPreferences.m 文件,更具体地说是 revealElementForKey 方法,但无法弄清楚该函数是如何工作的实际上被调用了。

关键字是searchTerms

当我终于找到这篇文章时才意识到这一点 SearchablePreferencePanes

步骤:

  1. NSPrefPaneSearchParameters 键添加到您的首选项窗格包的 Info.plist 中,其值类似于 MyPreferencePane.
  2. 在您的 Xcode 项目中创建一个名为 MyPreferencePane.searchTerms 的文件。该文件的内容应该是一个 属性 列表,其中包含一个类似于下面的字典(取自 Sound 首选项窗格)。这不仅允许用户在搜索指定术语时获得您的首选项窗格的命中,而且还将生成首选项窗格的锚点
  3. 在继承自 NSPreferencePane 的 class 中实现 revealElementForKey(key: String) 方法。当用户在搜索术语后打开窗格时以及调用 ScriptingBridge 锚 class.
  4. reveal() 方法时都会调用此方法
  5. 轰!

享受生活。

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