Mac Underscan adjusment coding problem – AppleScript

I tried to use AppleScript to adjust Display > underscan from Off to 2nd level of the underscan setting bar. Because my Mac Mini automatically turns off the underscan setting every time I turn on the TV, or wakes up from sleep.

I took reference to some coding online, and did make the code to open the “Settings” and “Display” tabs, but I don’t know the next step. Does anyone know what other encodings should be?

My Code:

activate application "System Preferences"
tell application "System Events"
    tell process "System Preferences"
        click button "Displays" of scroll area 1 of window "System Preferences"
        delay 0.5
        
        tell tab group 1 of window "42PFD5519/30"
            click radio button "Default for Display"
            
            -- What code should i use?
            
        end tell
    end tell
    delay 0.5
    quit application "System Preferences"
end tell

And Reference Photo Screenshot

Answer

I do not have a setup such as yours to test, however, this is how I code it:

Example AppleScript code:

--  # Check to see if System Preferences is 
--  # running and if yes, then close it.
--  # 
--  # This is done so the script will not fail 
--  # if it is running and a modal sheet is 
--  # showing, hence the use of 'killall' 
--  # as 'quit' fails when done so, if it is.
--  #
--  # This is also done to allow default behaviors
--  # to be predictable from a clean occurrence.

if running of application "System Preferences" then
    try
        tell application "System Preferences" to quit
    on error
        do shell script "killall 'System Preferences'"
    end try
    delay 0.1
end if

--  # Make sure System Preferences is not running before
--  # opening it again. Otherwise there can be an issue
--  # when trying to reopen it while it's actually closing.

repeat while running of application "System Preferences" is true
    delay 0.1
end repeat

--  # Open System Preferences to the 
--  # Display tab of the Displays pane. 

tell application "System Preferences" to ¬
    reveal anchor "displaysDisplayTab" of ¬
        pane id "com.apple.preference.displays"

tell application "System Events"
    tell application process "System Preferences"
        
        --  # Wait for the UI to be available.

        set i to 0
        repeat until exists ¬
            radio button 1 of ¬
            tab group of window 1
            delay 0.1
            set i to i + 1
            if i ≥ 30 then return
        end repeat

        --  # Click the target radio button
        --  # and adjust the target slider.
        
        tell tab group 1 of window 1
            click radio button "Default for display"
            set value of slider 1 to 0.2
        end tell
        
    end tell
end tell

delay 0.2

tell application "System Preferences" to quit

Notes:

Typically, the value of a slider is between 0.0 and 1.0, so I used 0.2 so it should end up where you want it, however, adjust the value as necessary.

The example AppleScript code, shown above, was tested in Script Editor under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.


Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.