Hello @Tom, @ccstone and all the other script experts
I would like to receive a message (Pushover) on my iPhone if the battery level drops below 15 percent on my MacBook Pro.
I'm not a script expert, so I looked on the internet to see if I could find anything. I came across this script:
property lowBattery : 15
idler()
on idler()
set battStatus to do shell script "pmset -g ps"
if battStatus contains "InternalBattery" then
set {TID, text item delimiters} to {text item delimiters, ";"}
set battStatus to text items of battStatus
set battCharging to item 2 of battStatus as string
display notification "(" & battCharging & ")"
set text item delimiters to tab
set battStatus to text 1 thru -2 of last text item of item 1 of battStatus as integer
set text item delimiters to TID
if battCharging = " charging" then
set whatToSay to "A/C attached. " & battStatus & " percent power left"
else
set whatToSay to "Running on battery " & battStatus & " percent power left"
end if
say whatToSay
if battStatus < lowBattery then say "Battery power is lower than " & lowBattery & " percent"
end if
return 60
end idler
I would like to add my Pushover macro to this:
tell application "Keyboard Maestro Engine"
do script "BB740455-13B9-4A0E-947F-AB6B9EC7C2DD"
end tell
and then run the script as an application on the MacBook Pro.
I already tried it in a program (with Show after run handler), but it doesn't work.
It would also be enough for me to get my Pushover notification on the iPhone at 15 percent. I don't need the display notification or the text announcement.
You don't want to run an AppleScript applet with an idle handler – you want to run a Keyboard Maestro macro with a timed-trigger.
You don't need AppleScript either, since pmset is a shell command.
That said here's such an AppleScript:
set battStatus to do shell script "pmset -g ps"
if battStatus starts with "Now drawing from 'Battery Power" then
set AppleScript's text item delimiters to {tab, "%"}
set percentBatteryLeft to text item 2 of battStatus
return percentBatteryLeft
else if battStatus starts with "Now drawing from 'AC Power'" then
return "On AC Power!"
end if
Here's the sort of thing you can do with Keyboard Maestro and a shell script.
Thanks for asking this question @appleianer
And thanks to @ccstone for the macro. Made a second one for removing my power cable if battery is fully charged.