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

I just got a secondary monitor to use with my MacBook Pro (13" Retina Early-2015). I previously had AppleScripts for dimming and for increasing the brightness of the MBP screen—either to no bars or to about 75%—but now they aren't working for some reason (w/ or w/o the external monitor plugged in). I also tried using Shades but both screens keep flashing every 2-3 seconds (I'd rather use an AppleScript as well).

Basically, my external monitor is my main screen and I want to be able to dim my MBP screen using an AppleScript to either no bars or about 75% (of which the scripts will then be redirected to keyboard shortcuts with an application).

What AppleScript code would be needed to do this?

EDIT: Here is the old that worked really well before, but for some reason stopped (with out without an external monitor plugged in).

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

and

tell application "System Events"
    repeat 11 times
        key code 113
    end repeat
end tell

Solution 1:

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.

Solution 2:

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"

Solution 3:

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.