'Pivot monitor with apple script. Error : 'Can't get button'

enter image description hereenter image description here

tell application "System Preferences"activateend tell

tell application "System Events"

set _0 to "Standard"
set _90 to "90°"
set _180 to "180°"
set _270 to "270°"

set preference to application process "System Preferences"
click UI element "Displays" of scroll area 1 of window "System Preferences" of preference

delay 1

click button "Display Settings..." of window "Displays" of preference


set monitor to window "LG 4K HDR" of preference
click monitor
set options to pop up button 1 of tab group 1 of monitor
set portrait to value of options is _270

click options
if portrait then
    --switch to landscape mode
    click menu item _0 of menu 1 of options
else
    --switch to portrait mode
    click menu item _270 of menu 1 of options
    delay 5
    click UI element "Confirm" of sheet 1 of monitor
end if

end tell

tell application "System Preferences"quitend tell

Result: error "System Events got an error: Can’t get button "Display Settings..." of window "Displays" of application process "System Preferences"." number -1728 from the button "Display Settings..." of window "Displays" of application process "System Preferences"

The above code is an automator script that pivots my monitor works until Mojave. After I update my mac, it doesn't work. I actually don't know much about Apple Script. I got an error from 'click button "Display Settings..." of window "Displays" of preference'. I was trying to change 'button' as 'UI element', index, and system language. It doesn't work.



Solution 1:[1]

I think the error is because the button name is an ellipsis character and not triple dots ...

Can you try replacing that line with this instead (Monterey):

click (first button of window "Displays" of preference whose title starts with "Display Settings")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user3579815