Skip to content

Instantly share code, notes, and snippets.

@coltenkrauter
Created February 19, 2025 03:44
Show Gist options
  • Save coltenkrauter/4d9fc8d0824dad8bc5776dc251998b33 to your computer and use it in GitHub Desktop.
Save coltenkrauter/4d9fc8d0824dad8bc5776dc251998b33 to your computer and use it in GitHub Desktop.
Automatically sets macOS screen brightness to 100% when plugged into power. Uses a LaunchDaemon and pmset to listen for power state changes in real-time. Event-driven, no polling.

Real-Time Brightness Adjustment When Charging (macOS)

This solution automatically sets your macOS display brightness to 100% when the power adapter is connected. It leverages a LaunchDaemon that runs a persistent script listening for power state changes via pmset. This event-driven approach ensures immediate response without unnecessary polling.

Requirements

  • macOS (tested on recent versions)
  • brightness CLI (install directly from GitHub, see below)
  • Administrative privileges

1. Install the brightness CLI

Important: Do NOT install via Homebrew. The Homebrew package is outdated and broken.

Instead, install from GitHub:

git clone https://github.com/nriley/brightness.git
cd brightness
make
sudo cp brightness /usr/local/bin/

2. Create the Power Monitoring Script

This script continuously monitors power events and sets brightness to 100% when AC power is detected.

Open an editor to create the script:

sudo nano /usr/local/bin/brightness-monitor.sh

Paste the following content,

#!/bin/bash

# Absolute path to the brightness command
BRIGHTNESS_CMD="/usr/local/bin/brightness"

# Monitor power events in real time using pmset
pmset -g pslog | while read -r line; do
    if echo "$line" | grep -q "AC Power"; then
        echo "AC Power detected. Setting brightness to 100%."
        $BRIGHTNESS_CMD 1
    fi
done

Save and exit (Ctrl + X, then Y, then Enter).

Make the script executable,

sudo chmod +x /usr/local/bin/brightness-monitor.sh

3. Create the LaunchDaemon

This LaunchDaemon runs the monitoring script at boot and keeps it alive.

Open an editor to create the daemon configuration,

sudo nano /Library/LaunchDaemons/com.user.brightness-monitor.plist

Insert the following XML content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>com.user.brightness-monitor</string>
        <key>ProgramArguments</key>
        <array>
            <string>/usr/local/bin/brightness-monitor.sh</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>KeepAlive</key>
        <true/>
    </dict>
</plist>

Save and exit.

4. Load the LaunchDaemon

Set proper permissions and load the daemon,

sudo chmod 644 /Library/LaunchDaemons/com.user.brightness-monitor.plist
sudo chown root:wheel /Library/LaunchDaemons/com.user.brightness-monitor.plist
sudo launchctl unload /Library/LaunchDaemons/com.user.brightness-monitor.plist 2>/dev/null
sudo launchctl load /Library/LaunchDaemons/com.user.brightness-monitor.plist

Verification & Troubleshooting

  • Manual Test
    Run the script manually to confirm functionality:
sudo /usr/local/bin/brightness-monitor.sh

You should see a message indicating that AC power was detected and brightness is being set to 100%.

  • Logs
    If the daemon does not trigger as expected, inspect system logs for hints:
log show --predicate 'process == "launchd"' --last 5m | grep brightness

How It Works

  • The script runs continuously in the background.
  • It listens for power events using pmset -g pslog.
  • Upon detecting "AC Power", it invokes the brightness command (via its absolute path) to set the display brightness to 100%.
  • The LaunchDaemon ensures the script is started at boot and remains running.

Happy coding and enjoy your automatically optimized brightness!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment