Skip to content

Instantly share code, notes, and snippets.

@eseceve
Last active January 11, 2016 03:18
Show Gist options
  • Save eseceve/5485752 to your computer and use it in GitHub Desktop.
Save eseceve/5485752 to your computer and use it in GitHub Desktop.
Applescript for show a alert when the battery is above 80% or under 40% load

With Homebrew (and wget)

wget https://gist.github.com/eseceve/5485752/raw/902958d64c4e764660a43a096f05e113e0ed2f6a/batteryAlert.plist
mv batteryAlert.plist ~/Library/LaunchAgents/
wget https://gist.github.com/eseceve/5485752/raw/9f926d66f9f44b13425d183e4e903a90a1c585c8/batteryScript.applescript
mv batteryScript.applescript /etc/
launchctl load ~/Library/LaunchAgents/batteryAlert.plist

Without Homebrew

  • copy batteryScript.applescript to /etc/batteryScript.applescript
  • copy batteryAlert.plist to ~/Library/LaunchAgents/batteryAlert.plist
  • *reset your mac or run launchctl load ~/Library/LaunchAgents/batteryAlert.plist in your terminal
<?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>KeepAlive</key>
<false/>
<key>Label</key>
<string>batteryAlert</string>
<key>LowPriorityIO</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>/etc/batteryScript.applescript</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ServiceDescription</key>
<string>Battery Alert</string>
<key>StartInterval</key>
<integer>30</integer>
</dict>
</plist>
set Cap to (do shell script "ioreg -w0 -l | grep ExternalChargeCapable")
tell Cap to set {wallPower} to {last word of paragraph 1}
set Cap to (do shell script "ioreg -wO -l | grep Capacity")
tell Cap to set {Available, Max} to {last word of paragraph 2, last word of paragraph 1}
set percent to round (100 * Available / Max)
if wallPower = "Yes" then
if percent >= 80 then
# sacar cargador
tell application "Finder"
activate
set dialog_text to "Batería cargada en un " & percent & "%. RETIRAR cargador"
display dialog dialog_text
end tell
end if
else
if percent <= 40 then
# cargar
tell application "Finder"
activate
set dialog_text to "Batería cargada en un " & percent & "%. CONECTAR cargador"
display dialog dialog_text
end tell
end if
end if
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment