Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How can I make a script run when battery hits certain percent?

I'm trying to set up a specific, audible notification when my battery is low. Is there a way to do it?
I found this from a few years ago: http://www.lifehacker.com.au/2011/02/give-your-mac-a-more-attention-grabbing-low -battery-warning/
But there's some details I don't like. For instance, I don't want it to constantly repeat a message. I'd just like the message to play once.


I'm new to script writing and editing so thank you in advance for your help.


~ Zach Lentino

Posted on May 19, 2015 8:47 PM

Reply
14 replies

Jan 5, 2017 1:27 PM in response to AntigoneRex

The code worked in 2015. It does not work with macOS Sierra, which now returns unreliable, reported battery remaining statistics, and changed the output format of the pmset command.


If the macOS Sierra pmset command in the Terminal returns inconsistent data, wrapping it in AppleScript will not improve it. I was prepared to tweak the 2015 code until I saw those results in the Terminal.

May 20, 2015 3:02 AM in response to Sonic99

Copy/Paste the following AppleScript that I wrote back in 2013 into the AppleScript Editor, and click the hammer toolbar icon. Then run it and listen.


This script can be adapted to your value for a low battery announcement.


Code:


(*

Speakable items addition that recites current battery info.

Open in AppleScript Editor and Save As… script without extension

into login directory/Library/Speech/Speakable Items folder.

Name suggestion: Battery level.


Works exactly as coded. Though intended for visually challenged, it will

output a text banner containing the speech content if audio is off.


Tested on OS X 10.7.5, 10.9.5, and 10.10.3

VikingOSX, December 2013, Apple Support Community

Version 1.2

*)


set theList to {}

set thePct to ""

set theHr to ""

set theMin to ""

set expression to ""


set theList to paragraphs of text of (do shell script "pmset -g batt")

set itemCnt to count of items in theList


if (itemCnt = 1) then

say "Desktop Device not equipped with battery."

return

end if


set thePct to word 3 of theList's item 2

if theList's item 2 contains "finishing charge" then

set theHr to word 6 of theList's item 2

set theMin to word 7 of theList's item 2

else

set theHr to word 5 of theList's item 2

set theMin to word 6 of theList's item 2

end if

--display dialog thePct & return & theHr & return & theMin


--trim leading zero

if (theMin begins with "0") then set theMin to text 2 thru -1 of theMin


if theList's item 1 contains "AC Power" then

set powerStatus to "power cord"

set chargeStatus to "full charge."

else

set powerStatus to "battery power"

set chargeStatus to "battery exhaustion."

end if


if (thePct is greater than or equal to "99") then

set expression to "Currently on " & powerStatus & " at full charge."


else if (theHr is "0") and (theMin is not "0") then

set expression to "Currently on " & powerStatus & ¬

" with the battery at " & thePct & ¬

" percent, and " & theMin & " minutes to " & chargeStatus


else if (theHr is "1") and (theMin is greater than or equal to "0") then

set expression to "Currently on " & powerStatus & ¬

" with the battery at " & thePct & ¬

"percent , and " & theHr & ¬

" hour, and " & theMin & " minutes to " & chargeStatus

else

set expression to "Currently on " & powerStatus & ¬

" with the battery at " & thePct & ¬

"percent , and " & theHr & ¬

" hours, and " & theMin & " minutes to " & chargeStatus

end if


sayexpression

May 20, 2015 12:59 PM in response to Sonic99

What you want is much simpler than what I offered previously. You can achieve a battery level monitoring solution by using the following AppleScript code saved into your home directory, and the monitoring process goes into ~/Library/LaunchAgents/com.mine.batteryAlert.plist. Then all you do is log out, and back in, and the system will run the script every 5 minutes (as I have it configured).


If you are on AC Power, then the script simply notes this and quits. If you are on Battery Power, and your remaining battery percentage is at, or below your threshold percentage, then it will both speak, and pop an alert dialog that will auto-vanish in 15 seconds. Otherwise, it is quiet.


AppleScript


Open a new AppleScript Editor/Script Editor window, and copy/paste the following code into it. Click the hammer icon on the toolbar to compile it. Then save… into your home directory as batteryScript.applescript. Under File Format: choose text. Nothing else is checked. Make certain that the path to this script is identical to the one in the following launch agent.


Code:


-- batteryScript : will run every StartInterval seconds to check remaining battery percentage

-- ~/Library/LaunchAgents/com.mine.batteryAlert.plist will run every StartInterval seconds using

-- this AppleScript.


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})[^%]'"

-- log chargeState

if chargeState contains "AC Power" then return -- skip alert when charging

if chargeState contains "Battery Power" and percentLeft ≤ 95 then

say "The battery is very low at " & percentLeft & " percent. Please connect to Power."

tell application "System Events"

display alert "Battery less than " & percentLeft & "%. Please connect to power now!" giving up after 15

end tell

end if

return


LaunchAgent

Create a new TextEdit document. On the Format menu, select Make Plain Text. After you have done that, copy/paste the following code into TextEdit. Edit the path to your AppleScript document in the following XML code. Finally, save it as yourlogin/Library/LaunchAgents/com.mine.batteryalert.plist. You simply logout and back in and your script will run every StartInterval seconds. Should you want to stop this low battery reporting, simply move the com.mine.batteryalert.plist out of the home/Library/LaunchAgents directory.


Code:


<?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>/Users/you/batteryScript.applescript</string>

</array>

<key>RunAtLoad</key>

<true/>

<key>ServiceDescription</key>

<string>Battery Alert</string>

<key>StartInterval</key>

<!-- run every 5 minutes -->

<integer>300</integer>

</dict>

</plist>

May 22, 2015 4:35 AM in response to Sonic99

The code as I wrote it will continue to warn you every five minutes after the battery percentage drops below a certain configured value. Thus, once you are below 95 percent, it will repeat the warning every five minutes. If you plug in a charger, the warning will stop. I used 95 percent, because I didn't want to wait all day to see if my code worked. You would only use ≤ 5 in your final production adjustment.


To satisfy yourself that it will work, change the 95 to 65, and set about draining your battery. I set the frequency to five minutes in the plist.

May 22, 2015 9:20 AM in response to Sonic99

My bad. This will work. Treats text strings as integers.


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})[^%]'"

logchargeState

if chargeState contains "AC Power" then return -- skip alert when charging

considering numeric strings

if chargeState contains "Battery Power" and percentLeft ≤ 95 then


say "The battery is very low at " & percentLeft & " percent. Please connect to Power."

tell application "System Events"

display alert "Battery less than " & percentLeft & "%. Please connect to power now!" giving up after 15

end tell


end if

end considering

return

May 26, 2015 6:53 PM in response to Sonic99

Well It was working, Now the whole thing just stopped out of nowhere.
Here's my script:



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})[^%]'"

log chargeState

if chargeState contains "AC Power" then return -- skip alert when charging

considering numeric strings

if chargeState contains "Battery Power" and percentLeft ≤ 10 and percentLeft > 5 then

say (say "sorry to interrupt you sir, but I seem to be low on power.")

end if

if chargeState contains "Battery Power" and percentLeft ≤ 5 then

say (say "sir, I fear that if I am not plugged in soon I will power down.")

end if

end considering

return

anytime I run it it says "the command exited with a non zero status"

May 26, 2015 8:08 PM in response to Sonic99

You forgot and ran this script on an AC-only machine. That is what caused the percentLeft command line to fail. This script is only for machines with a battery.


I have added some additional condition checking to catch the AC-only scenario, and simplified the if logic in the on considering block.


set acOnly to do shell script "pmset -g batt | wc -l"

if acOnly = 1 then return -- machine does not have a battery


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})[^%]'"

-- log chargeState & return & percentLeft


-- on a battery equipped machine that is now charging on AC Power, there is no need

-- to warn about low battery condition, so we quit.

if chargeState contains "AC Power" then return


considering numeric strings


-- Implicitly, we are only on battery power here

if percentLeft > 5 and percentLeft ≤ 10 then

say "sorry to interrupt you sir, but I seem to be low on power."

else if percentLeft ≤ 5 then

say "sir, I fear that if I am not plugged in soon I will power down."

end if


end considering

return

How can I make a script run when battery hits certain percent?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.