1

I have a 4K HDR Monitor, and sometimes Macbook causes the colors to be washed, the fix is to Disable and then Re-Enable HDR. I am trying to create an AppleScript to then incorporate that in Automator to do so. I was able to get some traction but not able to identify how to do the actual uncheck and identify the group. Here is what i had so far:

tell application "System Preferences"
activate
delay 2
set the current pane to pane id "com.apple.preference.displays"
delay 2
tell application "System Events"
    click checkbox "High Dynamic Range" of group 2 of window "LG HDR 4K" of application process "System Preferences"
end tell
quit end tell

Here is the error I get:

error "System Events got an error: Can’t get group 2 of window \"LG HDR 4K\" of application process \"System Preferences\". Invalid index." number -1719 from group 2 of the window "LG HDR 4K" of application process "System Preferences"

here is a screenshot of the page I am trying to uncheck and re-check HDR: enter image description here

Any advice would be appreciated, thank you.

avocadoLambda
  • 1,332
  • 7
  • 16
  • 33
  • In addition to what Ted Wrigley mentioned in his answer, you should also use, e.g. `tell application "System Preferences" to reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays"` instead of `set the current pane to pane id "com.apple.preference.displays"` as the latter will reveal the last _tab_ accessed and if it wasn't the **Display** _tab_, well.. you know. :) Also, once you have it working you can eliminate the `activate` _statement_ so you do not need to see the **UI** splash on the screen. – user3439894 Jun 30 '20 at 19:30

2 Answers2

2

I don't have a high-def display, so I don't see this particular option, but if I run the following code, I get a full list of all the subelements of the window:

tell application "System Preferences"
    activate
    delay 2
    set the current pane to pane id "com.apple.preference.displays"
    delay 2
    tell application "System Events"
        tell window 1 of application process "System Preferences"
            entire contents
        end tell
    end tell
end tell

Subelements you are interested in seem to have the following form:

radio button "Scaled" of tab group 1 of window "Built-in Retina Display" of application process "System Preferences" of application "System Events"

Note that it includes a tab group 1 entry (referring to the fact that you are on the 'Display' tab of the four tabs available which is missing from your chain.

Ted Wrigley
  • 2,921
  • 2
  • 7
  • 17
  • Thank you Ted Wrigley for the usefull comment !!! with your suggestion i was able to geto further. i identified the line i needed for my HDR display: 'heckbox "High Dynamic Range" of tab group 1 of window "LG HDR 4K" of application process "System Preferences" of application "System Events"' Now, if i uncheck HDR and run the code it does indeed check the box and enable HDR. I am trying now to identify how to uncheck the box via the applescript, instead of "click checkbox" i want to unckeck the box... – Meir Miyara Jun 30 '20 at 22:33
  • Here is the new code checking the box: tell application "System Preferences" activate set the current pane to pane id "com.apple.preference.displays" tell application "System Events" click checkbox "High Dynamic Range" of tab group 1 of window "LG HDR 4K" of application process "System Preferences" of application "System Events" end tell end tell – Meir Miyara Jun 30 '20 at 22:36
  • Just use `click` again: `click` will toggle the setting on or off, as needed. – Ted Wrigley Jul 01 '20 at 01:45
  • Tried that, however it error out. If the box is already checked then i get an error : ("System Events got an error: Can’t get tab group 1 of window "LG HDR 4K" of application process "System Preferences". Invalid index."). If i manually un-check and re-run the same script i mentioned above it will work without an error and successfully check the box. Which tells me that there is a different call to uncheck. – Meir Miyara Jul 01 '20 at 11:29
  • Ah, it sounds like checking the box changes the GUI element hierarchy. That happens sometimes. Make sure the box is checked, and then run the `entire contents` script again and see what the new path is. – Ted Wrigley Jul 01 '20 at 15:22
  • I thought so too as well, but sadly either with it checked or unchecked the entire contents return the same line output: "checkbox "High Dynamic Range" of tab group 1 of window "LG HDR 4K" of application process "System Preferences" of application "System Events"". By the way, thank you so much for continuously trying to help. – Meir Miyara Jul 01 '20 at 19:30
  • I did a bit more debugging and identified that the error come up ONLY if the system preferences display window is open. So reading back the comment user3439894 made, i removed the line "activate" to have it all running in the background and it seems like it is working now. i need to wait for the mac to mess up the display again and ill run this script to see if it works as i hope it will, if it does i will edit this OP and add this context. Thank you so muhc Ted, really awesome of you Sir !! – Meir Miyara Jul 01 '20 at 19:36
  • 1
    New code: tell application "System Preferences" set the current pane to pane id "com.apple.preference.displays" tell application "System Events" click checkbox "High Dynamic Range" of tab group 1 of window "LG HDR 4K" of application process "System Preferences" of application "System Events" click checkbox "High Dynamic Range" of tab group 1 of window "LG HDR 4K" of application process "System Preferences" of application "System Events" end tell end tell – Meir Miyara Jul 01 '20 at 19:36
0

The Display pane UI layout has changed a bit in macOS Monterey, and now looks like this:

Display Prefs UI Layout

In my case I wanted to enable HDR on my second monitor, so using the following code I was able to get Script Editor to test whether the High Dynamic Range checkbox was true or false, and if false to enable it:

tell application "System Preferences"
    activate
    delay 1
    set the current pane to pane id "com.apple.preference.displays"
    delay 1
    tell application "System Events"
        click button "Display Settings…" of window "Displays" of application process "System Preferences" of application "System Events"
        delay 1
        tell sheet 1 of window 1 of application process "System Preferences"
            select row 2 of outline 1 of scroll area 1
            set theCheckbox to checkbox "High Dynamic Range, Automatically adjust the display to show high dynamic range content."
            tell theCheckbox
                set checkboxStatus to value of theCheckbox as boolean
                if checkboxStatus is false then click theCheckbox
            end tell
        end tell
    end tell
end tell

If I wanted to select a different monitor, I would change the index of "row 2" in this line:

select row 2 of outline 1 of scroll area 1 of sheet 1 of window "Displays" of application process "System Preferences" of application "System Events"