Adding Ambient Light Brightness Adjustment to External Monitors
macOS automatically adjusts the brightness of its built-in display according to the ambient light, but if you have an external monitor, its brightness will not be adjusted accordingly. As a result, the brightness difference between the two screens will become more noticeable as you work from day to night.
I’ve looked online for some open-source and commercial solutions, but I wasn’t satisfied with them, mainly because they all occupy a menu bar slot, which is unnecessary for a system that should automatically adjust its brightness. Additionally, some of these solutions come with features that are not really needed, such as color and temperature adjustments, and some even require a subscription just to sync the brightness levels.
This solution is very simple, using only AppleScript
and Shell
, and it has the following advantages:
- Automatically adjusts brightness levels
- Starts up automatically upon boot
- Does not occupy the dock or menu bar items.
Of course, there is still room for improvement due to my limited abilities:
- A more elegant way of obtaining the current brightness level of the system, currently using two regular expressions.
- Listening to system brightness adjustment events instead of monitoring them every 30 seconds with a timer.
Workflow
Prerequisites
- The monitor must support ddc/ci.
- The brightness adjustment tool provided by ddctl is needed:
brew install ddcctl
Let’s begin
Scripting
Open the Script Editor
app, copy the code below, and save it as “AutoBrightness”:
on idle --设置定时任务
set brightness_num to do shell script "ioreg -c AppleBacklightDisplay | grep -oE '(?:\"brightness\").*?(\"value\").*?},' | grep -oE '\"value\"=(\\d{1,5})'|awk -F= '{print $2}'" -- help needed.
set brightness_num to (brightness_num / 65535) * 100 --单位转换
do shell script "/usr/local/bin/ddcctl -d 1 -b " & brightness_num
return 30
end idle
When saving the file, choose “Application” as the file format, otherwise the timer won’t trigger.
Setup Launch at Login
System Preferences -> User & Groups -> (tab)Login Items -> Add AutoBrightness
Hide Application Icon
Right-click on AutoBrightness.app
-> Show Package Contents -> Contents
Open info.plist
Insert the following code in the first <dict></dict>
block, then save and exit.
<key>LSUIElement</key>
<string>1</string>
EOF