2

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.

1
  • My script wasn't working, fixed it, although it's a bit uglier. :( – William T Froggard Nov 27 '15 at 1:12
3

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
3
  • thanks! This did exactly what I wanted. I've been messing around with UI scripting unsuccessfully for awhile. It's nice to get a good example of this. – masterninja01 Dec 9 '15 at 14:43
  • Is there way to select a row from it's name and not it's number? I want this to work on machines I've never used (a part of an installer). I can't get it to work but I've tried select row "Open URL"… IT doesn't seem to want to take the name. Any tips? – masterninja01 Dec 9 '15 at 15:17
  • I have made the requested changes. It's sort of hacky, as GUI scripting tends to be, but it gets the job done. I can't verify if this will work across operating systems without modification, however (I'm running 10.11). This certainly can be done on every version of OS X I'm familiar with (back to 10.5), but the syntax may be slightly different, and you'd have to check for that. With any luck, this may "just work". – William T Froggard Dec 11 '15 at 6:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .