Broken again with macOS Sequoia, because the UI element is now:
UI element 1 of group 1 of scroll area 2 of group 1 of list 2 of splitter group 1 of list 1 of window "Displays"
My shortcut no longer toggles between resolutions. Instead I have a shortcut for each resolution, so I posted my latest script (working on macOS 15.1.1) in a new answer below (https://apple.stackexchange.com/a/477031/27373)
Proposed Fix for Zade's change for the Browser UI System Preferences in 13.0.1, the Shortcut didn't work anymore for me in Ventura 13.2 because of an error around the value of attribute "AXAttributedDescription"
part
What I use in Ventura 13.2 now:
do shell script "open x-apple.systempreferences:com.apple.preference.displays"
tell application "System Events"
tell application process "System Settings"
repeat until (exists UI element 1 of group 1 of scroll area 2 of group 1 of group 2 of splitter group 1 of group 1 of window 1)
delay 0.1
end repeat
tell window 1 #"Displays"
set displayOptions to UI element 1 of group 1 of scroll area 2 of group 1 of group 2 of splitter group 1 of group 1
set StandardRes to button 4 of displayOptions
set MoreSpaceRes to button 5 of displayOptions
if focused of StandardRes is false then
click StandardRes
else
click MoreSpaceRes
end if
end tell
end tell
end tell
quit application "System Settings"
This switches between the Standard resolution and the "More Space" one ("Mehr Fläche" in German) and should be system language independent.
Fix for wch1zpink's answer on macOS 13.0.1, thanks to the UI Browser app:
do shell script "open x-apple.systempreferences:com.apple.preference.displays"
tell application "System Events"
tell application process "System Settings"
repeat until (exists UI element 1 of group 1 of scroll area 2 of group 1 of group 2 of splitter group 1 of group 1 of window "Displays")
delay 0.1
end repeat
tell window "Displays"
set displayOptions to UI element 1 of group 1 of scroll area 2 of group 1 of group 2 of splitter group 1 of group 1
set theButton to (first button of displayOptions whose value of attribute "AXAttributedDescription" contains "Selected")
if help of theButton is "2560 × 1440" then
click button 1 of displayOptions
else
click button 3 of displayOptions
end if
end tell
end tell
end tell
quit application "System Settings"
Fix on macOS 12.0.1:
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events"
tell application process "System Preferences"
repeat until (exists radio button "Scaled" of radio group 1 of group 1 of window "Displays")
delay 0.1
end repeat
tell window "Displays"
set isScaled to value of radio button "Scaled" of radio group 1 of group 1
if isScaled = 0 then
click radio button "Scaled" of radio group 1 of group 1
click button 1 of UI element 3 of group 1
else
click radio button "Default for display" of radio group 1 of group 1
end if
end tell
end tell
end tell
quit application "System Preferences"
Fix on macOS 10.15.4:
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "Built-in Retina Display"
set isScaled to value of radio button "Scaled" of tab group 1
if isScaled = 0 then
click radio button "Scaled" of tab group 1
click radio button 1 of radio group 1 of group 1 of tab group 1
else
click radio button "Default for display" of tab group 1
end if
end tell
quit application "System Preferences"