Forked from brandon1024/BatteryStatusNotification.scpt
Created
October 5, 2024 04:07
-
-
Save memphisthemau/66d1f3b5e589d1f6acf1863b39a925e1 to your computer and use it in GitHub Desktop.
Revisions
-
brandon1024 revised this gist
Mar 15, 2018 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,9 +10,9 @@ I'll be the first to admit, I am no electrochemist. I might be (and likely am) a I wanted a way to get a notification when my battery reaches 40% and 80%. That's my target range. So I wrote a script to do it (I have no life). It is written in AppleScript, and uses a fancy-pants notification card to display a message when it's time to charge or unplug the charge cable. The script relies on Apple's task scheduler `launchd`, and by following these instructions, the script will run automatically after login. ## Installation First, you will need to download the two files below `BatteryStatusNotification.scpt` and `battery.monitor.plist`. **Make sure you edit the `plist` file to point to the correct script file.** If you want to run the script once, just run `osascript BatteryStatusNotification.scpt`. This is fine, but won't automatically run the script on login. To do this, copy the `plist` file `battery.monitor.plist` to `~/Library/LaunchAgents/`. I recommend placing the script under `~/Applications`. Test this out and let me know what you think! You can also easily adjust the boundaries, or modify the script to suit your needs. -
brandon1024 created this gist
Mar 15, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ 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 ≤ 40 then display notification "Time to plug me in :)" with title "Battery Charge Boundary" else if chargeState contains "AC Power" and percentLeft ≥ 80 then display notification "Time to unplug me :)" with title "Battery Charge Boundary" end if end considering delay 60 end repeat This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ # Battery Percentage Boundary Notification Background Script for macOS ## Preface I'm weird. We all have our weird habits and quirks. Luckily for me, mine only involves my the battery in my macbook computer. Are you worried about keeping your devices' batteries healthy and keeping a charge? With every device I have owned, battery health has degraded noticeably over time, likely due to my poor charging habits. I'll be the first to admit, I am no electrochemist. I might be (and likely am) askew, and what I am about to show you may have absolutely no effect on battery performance. But, I like to believe it does :) I wanted a way to get a notification when my battery reaches 40% and 80%. That's my target range. So I wrote a script to do it (I have no life). It is written in AppleScript, and uses a fancy-pants notification card to display a message when it's time to charge or unplug the charge cable. The script relies on Apple's task scheduler `launchd`, and by following these instructions, the script will run automatically after login. ## Installation First, you will need to download the two files below `BatteryStatusNotification.scpt` and `battery.monitor.plist`. If you want to run the script once, just run `osascript BatteryStatusNotification.scpt`. This is fine, but won't automatically run the script on login. To do this, copy the `plist` file `battery.monitor.plist` to `~/Library/LaunchAgents/`. Make sure you edit the `plist` file to point to the correct script file. I recommend placing the script under `~/Applications`. Test this out and let me know what you think! You can also easily adjust the boundaries, or modify the script to suit your needs. ## Further Reading [Stack Exchange - How to run custom AppleScript in Background](https://apple.stackexchange.com/questions/192600/how-to-run-custom-applescript-in-the-background-at-all-times) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ <?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>PATH/TO/YOUR/SCRIPT/FILE/BatteryStatusNotification.scpt</string> </array> <key>RunAtLoad</key> <true/> </dict> </plist>