0

An AppleScript I use to auto adjust the brightness for iMacs stopped working when migrated to 10.12 Monterey. Any suggestions for a fix? Here is the original script:

tell application "System Preferences"
    if it is running then
        quit
    end if
end tell
delay 10.0
activate application "System Preferences"
tell application "System Events"
    tell process "System Preferences"
        click button "Displays" of scroll area 1 of window "System Preferences"
        delay 1
        click radio button "Display" of tab group 1 of window "Built-in Retina Display"
        set value of value indicator 1 of slider 1 of tab group 1 of window "Built-in Retina Display" to 0.57
    end tell
    quit application "System Preferences"
end tell

Right off the bat you can see the windows look different from the operating systems. I tried loosing "tab group 1" because I don't see tabs anymore. Also, I changed the name of the window from "Built-in Retina Display" to "Displays".enter image description here

enter image description here

I ended up using "UI Browser" to determine the path to element. However, I still can't get the brightness to adjust. As proof it is navigating properly in that window, I have included a line to click on the radio button "Scaled".

activate application "System Preferences"
tell application "System Events"
    tell process "System Preferences"
        click button "Displays" of scroll area 1 of window "System Preferences"
        delay 2
        # click radio button 2 of radio group 1 of group 1 of window 1
        set value of value indicator 1 of slider 1 of group 1 of window 1 to 0.25
    end tell
end tell

UI Browser

3
  • This sounds a bit X-Y. Why do you need to regularly change (or reset) the brightness of your display?
    – benwiggy
    Commented Dec 3, 2021 at 7:57
  • We have a lot workstations in a production environment that need to be reset daily.
    – cucaracha
    Commented Dec 3, 2021 at 19:25
  • 1
    Forget UI Scripting and just use brightness instead. Commented Dec 3, 2021 at 20:16

4 Answers 4

1
activate application "System Preferences"
tell application "System Events"
    tell process "System Preferences"
        click button "Displays" of scroll area 1 of window "System Preferences"
        delay 2
        set value of value indicator 1 of slider 1 of group 1 of window 1 to 0.25
    end tell
end tell

I went to test this code (after also finding the aforementioned lines from a past OS). And I swear I was crazy because it started working after I had switched group 1 and slider 1 (then compiled and tested with error) and then I switched it back to the original and it started to work.

Luckily I screen recorded this behavior, note I kept forgetting to quit System Preferences but that is a different error.

https://youtu.be/NGVU0vpEGsU

1

I am using "Brightness" with Homebrew through a shell script. This will allow me to run it at login.

https://formulae.brew.sh/formula/brightness

0

You could use the new Shortcuts automation feature, which has an action to Set screen Brightness.

enter image description here

1
  • This would be a viable option if it would auto run at login. At the moment, I am using "Brightness" with Homebrew through a shell script.
    – cucaracha
    Commented Dec 6, 2021 at 23:18
0

On macOS Monterey, I'm using the following script to set my MacBook Pro's built-in display brightness (in a multiple display set-up).

set myBrightness to 0.71

--  # Start with System Preferences closed

if running of application "System Preferences" then
    try
        tell application "System Preferences" to quit
    on error
        do shell script "killall 'System Preferences'"
    end try
end if
repeat while running of application "System Preferences" is true
    delay 0.2
end repeat

--  # Open System Preferences to the target pane

tell application "System Preferences"
    reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays"
    activate
    repeat while name of window 1 is not "Displays"
        delay 0.2
    end repeat
end tell

--  # Change the Brightness

tell application "System Events"
    try
        tell process "System Preferences"
            -- Open the Display Settings sheet
            click button "Display Settings…" of window 1
            repeat until sheet 1 of window 1 exists
                delay 0.2
            end repeat
            -- Select the built-in display
            select row 1 of outline 1 of scroll area 1 of sheet 1 of window "Displays"
            repeat until slider 1 of sheet 1 of window 1 exists
                delay 0.2
            end repeat
            -- Set the built-in display brightness
            set value of slider 1 of sheet 1 of window 1 to myBrightness
            repeat until value of slider 1 of sheet 1 of window 1 > (myBrightness - 0.05) and value of slider 1 of sheet 1 of window 1 < (myBrightness + 0.05)
                delay 0.2
            end repeat
            -- Close the Display Settings sheet
            click button "Done" of sheet 1 of window 1
            repeat while sheet 1 of window 1 exists
                delay 0.2
            end repeat
        end tell
    end try
end tell

--  # Close System Preferences

quit application "System Preferences"

You must log in to answer this question.

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