MacOS – Get Radio Buttons in System Preferences

applescriptmacossystem-prefs

I am trying to click at radio buttons in Displays panel of System Prefernces. This is the code I use to identify radio buttons:

tell application "System Preferences"
    activate
    reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays"
end tell
tell application "System Events"
    tell application process "System Preferences"

        set frontmost to true
        get every radio button of window 0 // returns {} -> empty array

    end tell
end tell

The radio buttons returned are none. Based on what I see, window has zero radio buttons. This leads to a conclusion that radio buttons are part of sub window, namely the Displays subwindow and not the main window. How can I navigate to this "subwindow" and click radiobuttons?

enter image description here

Best Answer

The radio buttons are contained within a radio group, and the radio group is contained within a tab group. So the following code will give you the names of the radio buttons.

You also need to put in a delay between opening the window and trying to talk to it. Even on an SSD equipped Macbook Pro. You might have to increase the delay if you are on an HDD equipped Mac.

tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preference.displays"
end tell
tell application "System Events"
    tell application process "System Preferences"
        set frontmost to true
        delay 1
        tell tab group 1 of window "Built-In Retina Display"
            tell radio group 1
                get every radio button
            end tell
        end tell
    end tell
end tell

Also for the sake of brevity I've swapped the line set current pane to pane "com.apple.preference.displays" for reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays".

If you are trying to work out how a user interface is assembled a very useful tool is "Accessibility Inspector". Just launch it and it will float above your windows. Then point your cursor at whatever you want to understand and it will show you the hierarchy of how the user interface components are assembled.

Screenshot of Accessibility Inspector doing its job