Script help - MBP battery under 15 percent + Pushover notification

Hello @Tom, @ccstone and all the other script experts :smiley:

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.

I thank you already for every help

1 Like

Hey @appleianer,

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.

Get Battery Status.kmmacros (12 KB)

-Chris

1 Like

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.

1 Like

Thank you very much for your help @ccstone :+1: :clap:
The macro works very well. I added a spoken text (battery level) and the Pushover message.

How did you @Gerrit change the macro for the charged battery level?

@appleianer This is the macro I use. The messages are in dutch to remind me to charge or remove the cable. Get Battery Status - Battery or AC Power.kmmacros (11.4 KB)

Thank you for sharing :+1: I will try it out.

If we are ever at the battery levels, then this is certainly interesting for you @Gerrit...

The macro is from @Tom and shows the current Bluetooth input devices (Mouse, Trackpad & Keyboard):

I have added a voice message and a pushover message for my iOS devices.
Please enter the value you want (green field).

Mouse & Keyboard Battery Charge Level Warning (Newer Devices)2 copy.kmmacros (34,8 KB)

Thanks @appleianer. Unfortunately, I don't have these accessories that are mentioned at the moment. But maybe, in future.

1 Like