2

I have a macbook M1 Max 2021, 1tb, 64gb ram, with mac os 13.1 ventura and my HDR always randomly turns off when my computer wakes up from sleep. My monitors are dell s3422dwg and dell s2721dgf.

My externals are 1 and 3 while my internal is monitor 2.

I have to go into the menu and find the monitors and enable the hdr slider.

I am trying to write an apple script that can enable HDR on the external monitors without affecting my internal retina monitor. The internal screen can only enable or disable tru tones not hdr.

Here is what I have so far but I keep running into an end of line error.

tell application "System Preferences"
    activate
    reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays"
    tell anchor "displaysDisplayTab" of pane id "com.apple.preference.displays"
        tell table 1 of scroll area 1
            select (row whose value of static text 1 contains "DELL S3422DWG")
        end tell
        tell table 1 of scroll area 1
            select (row whose value of static text 1 contains "DELL S2721DGF")
        end tell
        click checkbox "High Dynamic Range" of group 1
    end tell
    quit
end tell

Then I also tried to do it another way like this,

tell application "System Settings"
    activate
    set current pane to pane id "com.apple.preference.displays"
end tell


tell application "System Events"
    tell process "System Preferences"
        set frontmost to true
        repeat until exists window "Displays"
        end repeat
        tell scroll area 1 of group 1 of window "Displays"
            click checkbox "High Dynamic Range"
        end tell
    end tell
end tell

tell application "System Settings"
    quit
end tell

But the system preferences app opens up on the appearance tab and I get thrown an error error "System Settings got an error: AppleEvent handler failed." number -10000.

I just started using apple script and I am not really sure what I am doing. Any help would be greatly appreciated. Thanks!

2

2 Answers 2

3

I wanted to avoid UI scripting so I wrote a Swift program to do that using the private MonitorPanel.framework, which is the same method used by Apple when you click that checkbox.

You can find it here along with an already compiled binary: github.com/alin23/mac-utils - ToggleHDR

I also provide it as an Apple Shortcut in 🌕 Lunar for people that want to avoid the command-line.

toggle hdr shortcut

2
  • 1
    Hey man, you're a lifesaver. Thanks for the awesome work :D
    – Raven
    Commented May 19, 2023 at 2:35
  • Good job, thank you Commented Jan 24 at 10:42
1

Stolen from here: https://gist.github.com/skempken/46c184c1a5eac2e88c9c31ce09a38300

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

tell application "System Settings"
    activate
    current application's NSWorkspace's sharedWorkspace()'s openURL:(current application's NSURL's URLWithString:"x-apple.systempreferences:com.apple.Displays-Settings.extension")
    delay 0.5
    
    tell application "System Events"
        tell process "System Settings"
            key code 48 -- One tab to hit left monitor
            delay 0.5

            -- activate hdr on left monitor                         
            set hdr to checkbox 1 of group 3 of scroll area 2 of group 1 of group 2 of splitter group 1 of group 1 of window "Displays" of application process "System Settings" of application "System Events"
            tell hdr
                if value is 0 then click it
             end tell
            
            key code 48 -- Switch to the MBP screen         
            key code 48 -- Another tab to hit the right monitor
            delay 0.5
            -- do it again
            set hdr to checkbox 1 of group 3 of scroll area 2 of group 1 of group 2 of splitter group 1 of group 1 of window "Displays" of application process "System Settings" of application "System Events"
            tell hdr
                if value is 0 then click it
            end tell
            
        end tell
    end tell
    quit
end tell
3
  • it opens up in the display tab, but i get this error error "System Events got an error: Can’t get checkbox 1 of group 3 of scroll area 2 of group 1 of group 2 of splitter group 1 of group 1 of window \"Displays\" of application process \"System Settings\". Invalid index." number -1719 from checkbox 1 of group 3 of scroll area 2 of group 1 of group 2 of splitter group 1 of group 1 of window "Displays" of application process "System Settings"
    – anarchy
    Commented Feb 15, 2023 at 6:21
  • There, tested with an HDR monitor. You get that error above if one of the monitors doesn't have HDR. You can comment out each section so it doesn't try on a monitor that's missing HDR.
    – Alex
    Commented Feb 17, 2023 at 15:19
  • Oh, just read you have three screens, and the middle is your MBP screen. In that case you need to do the one tab to select the left screen, then do the "set HDR" bit, then another tab to the MBP screen, then another tab to the right, and then the "set HDR" bit again.
    – Alex
    Commented Feb 17, 2023 at 15:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .