Skip to content

Instantly share code, notes, and snippets.

@grvgr
Last active May 3, 2019 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grvgr/0dfc4a8bb0f7fb78cb6d to your computer and use it in GitHub Desktop.
Save grvgr/0dfc4a8bb0f7fb78cb6d to your computer and use it in GitHub Desktop.
#!/bin/bash
# -*- mode: shell-script -*-
# requires: brew install brightness
execPath=/usr/local/bin/brightness
brightApps=(iTerm iTerm2 Emacs)
ignoreApps=(Alfred)
logFile=/tmp/auto.brightness.log
logEnable=false
# logEnable=true
high='1.000000'
low='0.500000'
# function current { echo $($execPath -l | tail -1 | awk '{print $NF}'); }
function log { $logEnable && echo $1: $currentApp >> $logFile; }
function bright { log 'bright'; $execPath $high; }
function dim { log 'dim'; $execPath $low; }
function ignore { log 'ignore'; }
while [ true ]; do
sleep 0.2
previousApp=$currentApp
currentApp=$(lsappinfo info `lsappinfo front` | head -1 | awk '{print $1}' | grep -Eo '[^"]+')
if [[ $currentApp == $previousApp ]]; then
continue
elif [[ ${brightApps[*]} == *"$currentApp"* ]]; then
bright
elif [[ ${ignoreApps[*]} == *"$currentApp"* ]]; then
ignore
else
dim
fi
done
#> ~/Library/LaunchAgents/local.auto.brightness.daemon.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>local.auto.brightness.daemon</string>
<key>KeepAlive</key>
<true/>
<key>RunAtLoad</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/Users/gaurav/bin/auto.brightness</string>
</array>
<key>StartInterval</key>
<integer>5</integer>
<key>StandardOutPath</key>
<string>/tmp/auto.brightness.log</string>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment