'Change MacBook screen scaling via AppleScript

I am trying to change my MacBook Pro 14"s scale setting via AppleScript.
The setting should toggle two resolution settings.
I found the following script here: https://stackoverflow.com/a/62664159/15705553

on run {input, parameters}
    
    tell application "System Preferences"
        reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
    end tell
    
    set lowResolutionSettingIndex to 4
    set highResolutionSettingIndex to 5
    
    tell application "System Events" to tell process "System Preferences" to tell window "Built-in Retina Display"
        click radio button "Display" of tab group 1
        click radio button "Scaled" of tab group 1
        tell radio group 1 of group 1 of tab group 1
            set isHighResolutionSet to get value of radio button highResolutionSettingIndex
        end tell
        if isHighResolutionSet then
            -- Toggle native resolution
            click radio button lowResolutionSettingIndex of radio group 1 of group 1 of tab group 1
        else
            -- Toggle Default setting - "Retina optimized"
            click radio button highResolutionSettingIndex of radio group 1 of group 1 of tab group 1
        end if
    end tell
    
    quit application "System Preferences"
    
    return input
end run

I changed "Built-in Retina display" to "Built-in Liquid Retina XDR Display" as shown in my System Preferences, but two errors occur:

  • If I execute this script through Automator, I get the following error:
Syntax Error: System Events got an error: Can’t get window "Built-in Liquid Retina XDR Display" of process "System Preferences".
  • If I execute it through shortcuts.app, I get the following error, even though I granted access to accessibility features for Shortcuts in System Preferences
System Events got an error: Shortcuts is not allowed assistive access.


Solution 1:[1]

Here's how I select the first monitor in the list (Macbook Pro Built-in Retina Display) to gain access to the settings such as screen scaling:

tell application "System Preferences"
    activate
    reveal anchor "universalControlTab" of pane id "com.apple.preference.displays"
end tell
tell application "System Events"
    tell application process "System Preferences"
        tell window "Displays"
            select row 1 of outline 1 of scroll area 1 of sheet 1
        end tell
    end tell
end tell

And to select the second monitor in the list if you want to gain access to those settings:

tell application "System Preferences"
    activate
    reveal anchor "universalControlTab" of pane id "com.apple.preference.displays"
end tell
tell application "System Events"
    tell application process "System Preferences"
        tell window "Displays"
            select row 2 of outline 1 of scroll area 1 of sheet 1
        end tell
    end tell
end tell

Cheers.

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