3

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
  • 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.? – user3439894 Feb 10 '17 at 20:24
  • Added the old code to the post. – samseva Feb 11 '17 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 Feb 11 '17 at 5:37
4

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"
  • 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 Feb 10 '17 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 Feb 11 '17 at 5:27
  • 1
    @samseva I added a version of the script which will not bring system preferences to the front – wch1zpink May 21 '17 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 – theonlygusti Mar 28 '18 at 14:10
4

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.

  • 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. – user3439894 Jun 6 '17 at 2:21
  • 1
    @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 Jun 6 '17 at 3:55
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
  • Does pmset also allow to dim monitors (not turn them off completely), as the OP asked? – nohillside Jul 19 '18 at 6:25
1

The above answer for Sierra doesn't work in Mojave. Working code in Mojave to set brightness to 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 value of value indicator 1 of slider 1 of group 1 of tab group 1 to 0.75
end tell
quit application "System Preferences"

You must log in to answer this question.

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