4

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
3
  • Since you already have some AppleScript code that at one time did what you wanted, why not debug the code to make it work again? That said, since you do have code that at one time work and apparently doesn't now, why not include the code in your question and the changes in conditions that caused it to no longer work. We might be able to debug it. Did you upgrade to a different major version of OS X or from OS X to mac OS, etc.? Commented Feb 10, 2017 at 20:24
  • Added the old code to the post.
    – samseva
    Commented Feb 11, 2017 at 5:29
  • I found out what was causing the AppleScript to stop working. Since I got an external monitor, I also had to get a seperate Apple keyboard. When the keyboard is plugged in, the script doesn't work (works when it isn't).
    – samseva
    Commented Feb 11, 2017 at 5:37

5 Answers 5

6

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.

3
  • FWIW I use my MacBooK Pro with a Thunderbolt Display, mostly in Clamshell Mode and with an external keyboard, however in either scenario, the MBP's Display opened/closed, if the external keyboard is not attached then 107/113 to lower/raise the brightness works. It's only with the external keyboard attached that I have to use 145 and 144 to lower/raise the brightness. By the way, this only changes the brightness on the external display in any configuration on my system. Commented Jun 6, 2017 at 2:21
  • 2
    @user349894 You changed 75% of my answer, you corrected already correct sentences and added 534 characters to explain obvious things/simple code. You should spend your time more wisely/on other things. :)
    – samseva
    Commented Jun 6, 2017 at 3:55
  • @samseva Can Applescript detect if you use an external or internal keyboard?
    – d-b
    Commented May 3, 2023 at 14:13
5

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"
4
  • I actually just tested the second script with my retina built-in display arranged as my main monitor and again with my airplay display arranged as my main monitor and the script worked both times.
    – wch1zpink
    Commented Feb 10, 2017 at 22:56
  • The 1st code doesn't work and gives me an error. The 2nd code works, but has to launch System Preferences every time.
    – samseva
    Commented Feb 11, 2017 at 5:27
  • 1
    @samseva I added a version of the script which will not bring system preferences to the front
    – wch1zpink
    Commented May 21, 2017 at 23:15
  • Why don't you make that final script the first two as well? No one wants to see System Preferences pop up when using this code
    – minseong
    Commented Mar 28, 2018 at 14:10
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.

4
  • How to dim/raise the brightness?
    – Raining
    Commented Mar 17, 2020 at 7:41
  • Oh this method is too slow...
    – Raining
    Commented Mar 17, 2020 at 8:06
  • For people use this method, add delay 0.01 before line 5:set value of value indicator ...
    – Raining
    Commented Mar 19, 2020 at 4:12
  • 1
    Why add the delay?
    – RandallB
    Commented Mar 26, 2020 at 16:18
2

This AppleScript will instantly put all displays to sleep.

do shell script "pmset displaysleepnow"
  1. the displays will turn back on when you move the mouse/touch the keyboard
  2. works with both internal and external displays
  3. no delay in the screen going dark
2
  • Does pmset also allow to dim monitors (not turn them off completely), as the OP asked?
    – nohillside
    Commented Jul 19, 2018 at 6:25
  • it also stops music playing
    – basil
    Commented Jul 1 at 6:58
0

That's the code I'm using on my multi-display configuration. It is a slightly modified version of the Andrew D. Bond code posted above.

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 "Built-in Retina Display"
    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

You must log in to answer this question.

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