Apple - Dim screen brightness of MBP, using AppleScript (and while using a secondary monitor)?

Having a secondary monitor was not the issue, it was that I also needed an external keyboard.

When using an external keyboard, the brightness key codes change from 107 and 113 to 145 and 144 respectively, to lower/raise the brightness level.

For anyone interested, the correct key code events that works with an external keyboard (tested/works with an external Apple keyboard) are:

To lower the brightness all the way use:

tell application "System Events"
    repeat 16 times
        key code 145
    end repeat
end tell

The above code will lower the brightness all the way, regardless of where it's currently set. If it's already set at anything below 100%, any extra lower key code events, while still executed, are done so harmlessly and are just ignored when the above code runs.

To raise the brightness to, e.g. 75%, use:

tell application "System Events"
    repeat 12 times
        key code 144
    end repeat
end tell

Note that if you want a smother transition, add a delay 0.02 command to each repeat loop, otherwise the transition can appear quite abrupt.


This script will set the value of my display brightness to 75% on my MacBook Pro running OS Sierra if I only have just my retina display available in my display preferences

enter image description here


tell application "System Preferences"
    if it is running then
        quit
    end if
end tell
delay 0.2
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
        set value of value indicator 1 of slider 1 of group 2 of tab group 1 of window "Built-in Retina Display" to 0.75
    end tell
    delay 1
    quit application "System Preferences"
end tell

This script will set the value of my display brightness to 75% on my MacBook Pro running OS Sierra if I have retina display available and I have airplay enabled and using my airplay device as a second monitor in display preferences

enter image description here

tell application "System Preferences"
    if it is running then
        quit
    end if
end tell
delay 0.2
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 group 1 of tab group 1 of window "Built-in Retina Display" to 0.75
    end tell
    delay 1
    quit application "System Preferences"
end tell

For both of these scripts though, my built in retina display is my main monitor.

Here is a revised version of the script which will set the brightness to 75% without bringing system preferences application to the front. I think you will find this to be more efficient.

tell application "System Preferences"
    reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "Built-in Retina Display"
    set value of value indicator 1 of slider 1 of group 2 of tab group 1 to 0.75
end tell
quit application "System Preferences"

The earlier answer from wch1zpink for Sierra doesn't work later versions of macOS. On macOS 10.15.5 I see 2 different element hierarchies appear at different times, so handling both:

set brightness to 0.75
tell application "System Preferences"
    reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window 1
    set tryUntil to (current date) + 2 -- 2 seconds
    repeat
        try
            tell group 1 to tell tab group 1
                set value of value indicator 1 of slider 1 to brightness
            end tell
            exit repeat
        on error errorMessage
            try
                tell tab group 1
                    set value of value indicator 1 of slider 1 to brightness
                end tell
                exit repeat
            end try
            if ((current date) > tryUntil) then
                error errorMessage
            end if
        end try 
    end repeat
end tell

In my testing, this completes in between 0.02 seconds and 0.7 seconds, depending on whether System Preferences is already open. The repeat code causes retries if the element hierarchy isn't yet ready.