Skip to content

Instantly share code, notes, and snippets.

@ariefrahmansyah
Last active June 28, 2022 09:37
Show Gist options
  • Select an option

  • Save ariefrahmansyah/32b493ed4f6b1dc4679896a2d81abf55 to your computer and use it in GitHub Desktop.

Select an option

Save ariefrahmansyah/32b493ed4f6b1dc4679896a2d81abf55 to your computer and use it in GitHub Desktop.
Mac's Battery Status Notification
  1. Add battery.monitor.plist to ~/Library/LaunchAgents
  2. Modify ~/Library/LaunchAgents to point to path of BatteryStatusNotification.scpt
<?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>battery-status-monitor.job</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>BatteryStatusNotification.scpt</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
repeat
set chargeState to do shell script "pmset -g batt | awk '{printf \"%s %s\\n\", $4,$5;exit}'"
set percentLeft to do shell script "pmset -g batt | egrep -ow '([0-9]{1,3})[%]' | egrep -ow '[0-9]{1,3}'"
considering numeric strings
if chargeState contains "Battery Power" and percentLeft ≤ 20 then
display notification "Time to plug me in 🔌" with title "Battery Charge Boundary" sound name "Ping"
#else if chargeState contains "AC Power" and percentLeft ≥ 80 then
# display notification "Time to unplug me 🔋" with title "Battery Charge Boundary" sound name "Ping"
end if
end considering
delay 300
end repeat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment