How to make brightness keys work with second monitor?

I only had a 13-inch MacBook Pro a few months back, but then got an external monitor. At that time, I had AppleScripts to completely dim my screen or bring back the brightness to around 80%. Here are the two AppleScripts I was using:

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

(16 times if brightness at 100%)

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

However, with a secondary monitor, the scripts don’t work anymore. These scripts were really useful and I dread having to press the increase/decrease brightness for 5 or so seconds every time.

Could someone please help me out to either fix the scripts or find another way to have similar scripts?

The first thing to do is to bring up System Preferences → Displays and see if you can adjust the brightness of the display from there.

The answer is probably “no” for an external display, but it is at least theoretically possible and I think a few Thunderbolt displays implement things like that. But if your monitor is connected by DVI or HDMI, the answer is almost certainly “no.”

If there’s no brightness adjustment for the display in the System Preference, then the only way to adjust it is probably with the buttons on the display, in which case we’ll have a hard time Applescripting it.

If there is an adjustment for it in System Preferences, let us know and someone here might be able to take a crack at it. However, I wouldn’t be too hopeful, because very few external monitors can have their settings changed via software, so the scripters here probably won’t have a reference to target for the script.

There are ways to sort of “hack” this. You can make an icc profile that effectively dims the display by just making
the image data it sends to the display darker. This isn’t ideal because it wastes electricity and reduces your contrast ratio. The display hardware (backlight) is still set to bright, but it just makes your computer send darker images to it. But if you want to go that route, it is possible to Applescript changing the display profile.

Hi t.spoon. Thanks for the help. Yeah, there is no Brightness setting in System Preferences. An ICC colour profile could have been useful, but I don’t want to play with the monitor too much as it was quite expensive due to needing a 99%-100% AdobeRGB monitor.

But, what I would really like to script would be the screen of my MacBook Pro, since I only occasionally use it. Turning on or dimming the screen with a keyboard shortcut, instead of pressing the brighgtness buttons for a few seconds would be so much better.

Ah ha! I did some testing and it turns out that the issue is not the secondary monitor, but rather the secondary keyboard.

Since my laptop is now to the left of my larger display, I had to get an external Apple keyboard as well. I tested the AppleScripts without the Apple keyboard plugged in and everything works”with the keyboard plugged in (and pressing ‘Play’ in the Script Editor of the scripts), the scripts play, but they don’t work and instead produce an error beep.

Oh, it’s still the laptop display you’re trying to control.

When you say “with a secondary monitor, the scripts don’t work anymore,” I thought you were trying to get the scripts to brighten and dim the secondary monitor.

In that case, there are at least two ways you should be able to fix this with the keyboard plugged in.

You should be able to use UI scripting to change the setting for the built-in display in System Preferences. I don’t have a built-in display, so I can’t write that script, I have nothing to test against. Here’s a thread with people discussing that:

But if you’re willing to spend a few bucks, the best solution is probably SwitchResX:

Which can save “display sets” of display settings, and has an Applescript dictionary so you can directly address it to change to the settings you want. You can just do a script that toggles the setting back and forth between the two brightnesses you like and put it on any key command you like.

I made it work!
(From here:

Apparently, with an external keyboard, the brightness key codes change from 107 and 113 to 144 and 145.

Soooo much better now.

Another - keyboard-independed - way to do it is using a little command line utility which you then in turn can call from applescript. Look at the following tool: (Can be install via homebrew as well.)