Applescript for specific System Preferences pane

applescriptsystem-prefs

I'm trying to write an applescript to get me to a specific section of the system preferences – Keyboard > Shortcuts > Services and hopefully to a specific service as well. I've gotten a part of the way with this:

tell application "System Preferences"
    activate
    reveal anchor "shortcutsTab" of pane id "com.apple.preference.keyboard"
end tell

This gets me to the Shortcuts portion of the Keyboard prefs pane but I'd like to drill down further into the correct selection. Is there a way to select a specific service in this prefs pane? Ultimately I'm wanting to direct the end user to change a keyboard shortcut for a service previously installed.

Best Answer

Here's a script I just wrote that'll do it. This will get you to the shortcuts tab of the Keyboard preference pane, and select a row from the left and right columns:

tell application "System Preferences"
    activate
    reveal anchor "shortcutsTab" of pane id "com.apple.preference.keyboard"
end tell
tell application "System Events"
    tell application process "System Preferences"
        repeat until (window 1 exists)
        end repeat
        tell window 1
            #modify these to specify a row in the left column, or the right column, respectively
            
            repeat until (row 3 of table 1 of scroll area 1 of splitter group 1 of tab group 1 exists)
            end repeat

            select row 3 of table 1 of scroll area 1 of splitter group 1 of tab group 1
            
            repeat until (row 1 of outline 1 of scroll area 2 of splitter group 1 of tab group 1 exists)
            end repeat

            select row 1 of outline 1 of scroll area 2 of splitter group 1 of tab group 1
        end tell
    end tell
end tell

Here's the version you can use which identifies the rows by name. Sort of hacky, but it should work well.

tell application "System Preferences"
    activate
    reveal anchor "shortcutsTab" of pane id "com.apple.preference.keyboard"
end tell
tell application "System Events"
    tell application process "System Preferences"
        repeat until (window 1 exists)
        end repeat
        tell window 1
            #modify these to specify a row in the left column, or the right column, respectively
        
            repeat until (rows of table 1 of scroll area 1 of splitter group 1 of tab group 1 exists)
            end repeat
        
            select (first row of table 1 of scroll area 1 of splitter group 1 of tab group 1 whose value of static text 1 is equal to "Services")
        
            repeat until (rows of outline 1 of scroll area 2 of splitter group 1 of tab group 1 exists)
            end repeat
        
            select (first row of outline 1 of scroll area 2 of splitter group 1 of tab group 1 whose name of UI element 2 is equal to "Open URL")
        end tell
    end tell
end tell