change screen resolution with AppleScript

3.5k Views Asked by At

I am trying to click at radio buttons in Displays panel of System Prefernces, namely to change Screen resolution. 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

        --click button 1 of window 0 of application process "System Preferences" of application "System Events"

        --click radio button "Scaled" of radio group of window "com.apple.preference.displays"
    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

2

There are 2 best solutions below

2
On BEST ANSWER

The radio buttons are part of the radio group. The radio group is part of the tab group.

Here's the script:

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
        tell tab group 1 of window 1
            click radio button 2 of radio group 1 -- "Scaled"
            select row 2 of table 1 of scroll area 1 -- select the second row in the table to change the resolution of the monitor
        end tell
    end tell
end tell
0
On

For Mac OS 10.15, you'll need this instead.

Set 'q' to the display button preference you want (1-4)

set tabNum to q as number

tell application "System Preferences" to reveal pane "com.apple.preference.displays"

tell application "System Events" to tell process "System Preferences"

    set activeWindow to window 1

    repeat until exists activeWindow
    end repeat

    set tabGroup to tab group 1 of activeWindow

    tell tabGroup to click radio button "Scaled"

    set subGroup to group 1 of tabGroup

    set radioGroup to radio group 1 of subGroup

    tell radioGroup to click radio button tabNum

    --log activeWindow
    --delay 0.5

    tell application "System Preferences"
        quit
    end tell

end tell