I am using a MacBook and the battery is unreliable. I am trying to find a script command that will enable the laptop to play a beep when the battery level is lower than a threshold. After some search and tests, I found a quick solution below:
1) I first create a file called check_battery with the following contents:
ioreg -l | awk '$3~/Capacity/{c[$3]=$5}END{OFMT="%.3f";max=c["\"MaxCapacity\""]; bat= (max>0?100*c["\"CurrentCapacity\""]/max:"?"); printf("%d", bat)}'
2) I then created the following file:
\#!/bin/bash
sh check_battery>battery;
let t=60
read a < battery
if(( "$a" < "$t" ));
then
say "Battery level is lower than 60%!";
say "BEAP BEAP"
fi;
3) Finally, I try to add it to my crontab job, but crontab has been stopped by iOS. Then I found that I have to use launchd, which is detailed below:
https://alvinalexander.com/mac-os-x/mac-osx-startup-crontab-launchd-jobs
It is now working at my MacBook labtop but there may be better solutions. Do share with me if you have other solutions. Many thanks.
I think your script is a good script in its own right, but could end up being quite resource-intensive when run regularly because it invokes a shell process plus two programs to acquire the information you're after.
In this instance, I think AppleScript has a couple of advantages of a shell script, largely thanks to osascript
, which is the only program that needs to be executed to run an AppleScript, and it can do so without creating a shell process.
AppleScript to Retrieve Battery Info
Here's one such script that can retrieve information about your computer's battery, and issue warnings at appropriate times:
property threshold : 20 -- The level below which the script issues warnings
property verbose : false -- Report current battery level on every run
property file : "/tmp/battery.log" -- The path to the log file (from plist)
set eof of my file to 0
tell the battery()
set warning_level to the warningLevel()
if the warning_level = 1 then return check()
warn about (warning_level - 1) * 5 with alert
end tell
# warn
# Sends out a visible and audible warning with information about the
# remaining charge (+level) on the battery. Invoke without alert to have
# the +level reported in the notification centre as a percentage. Invoke
# with alert to have the +level reported in a pop-up alert dialog as an
# estimation of the remaining battery time (in minutes), and which must be
# dismissed by the user.
to warn about level without alert
if not alert then display notification ¬
["Current battery level: ", level, "%"] as text ¬
with title ["⚠️ Battery Warning"] sound name "Basso"
say "Warning: battery low" using "Moira" pitch 127 ¬
speaking rate 180 without waiting until completion
if alert then display alert ["Warning: battery level is very low"] ¬
message ["Estimated time remaining: " & level & " minutes"] ¬
as critical giving up after 0
end warn
# battery()
# Contains a script object that defines a number of convenience handlers that
# retrieve information about the on-board power source
on battery()
script
use framework "IOKit"
use scripting additions
property warninglevels : ["None", "Early", "Final"]
on warningLevel() -- A ten-minute warning indicator
IOPSGetBatteryWarningLevel() of the current application
end warningLevel
on info()
IOPSCopyPowerSourcesInfo() of the current application ¬
as record
end info
to check()
copy [it, |current capacity|, |is charging|] of ¬
info() to [ps_info, percentage, charging]
if the percentage ≤ threshold ¬
and it is not charging ¬
then warn about percentage ¬
without alert
if verbose then return display notification [¬
"Percentage: ", percentage, linefeed, ¬
"Charging: ", charging] as text ¬
with title ["