Power Status Changed Macro -- A Beginner Level Example

How to get a big banner when your laptop has come unplugged

I have been using my laptop a lot in situations where the MagSafe connector has become unplugged without my noticing. It's an older Mac so I don't use it on battery power much because it only runs for about two hours. Recently I had to reboot in the middle of a movie so I decided to set up a notification whenever the Mac got unplugged so that I could take care of the problem right away.

Thankfully, KBM has a Power Status Changed trigger, so it was a simple matter to write a macro that uses it. The simplest possible macro would have been to notify me that the power status had changed and leave it to me to figure out which way, but I wanted a little more hand holding. The "Power Status Changed Trigger" page in the User Manual referred to the BATTERY() function, which was what I needed to tell the difference. That function is 1 (true) when on battery power and 0 (false) when plugged in and charging.

The attached Power Status Changed macro is invoked by the Power Status Changed trigger, It checks the BATTERY() function and if the Mac is on battery power it reports the "Unplugged" status, otherwise it reports the "Plugged In" status. I decided to loop the Unplugged state in case I happened to miss the initial notification. The While... group will repeat as long as the BATTERY() function returns true.

A major enhancement would be to create a way to turn off the notifications for those times that I really do want to be on battery power, and then automatically turn notifications back on after two hours on battery to remind me to get plugged back in. But I'll wait to do that until I need it.

I hope you find this useful. It's my first published KBM macro. Comments and suggestions welcome.

Power Status Changed Macro (v10.0.2)

Power Status Changed.kmmacros (4.2 KB)

Keyboard Maestro Export

4 Likes

Sounds like a good idea, especially for older devices that have limited battery capacity now.

One observation/suggestions: your notifications could stop appearing if this macro were somehow cancelled, for instance via an emergency cancel all macros that many of us have set up.

You could get around this by simply creating another macro that is triggered every 20 seconds, and have this macro enable/disable that macro as needed.

Thanks for that suggestion Chris (@cdthomer).

I'm not immediately visualizing the flow that you are suggesting. Would that every-twenty-seconds macro not also be cancelled by a Kill All emergency cancellation?

This is great! Thanks! I'm using this to automatically position app windows when I disconnect from power and my external display and vice-versa.

1 Like

Hey August, so sorry for the tardy response. Increased personal responsibilities have taken up much of my time I used to use for the forum haha.

But what I was referring to was another macro triggered by the periodic trigger, every 20 seconds. While this macro could indeed be cancelled, it would still trigger again 20 seconds later until disabled. I use a variety of macros that are enabled/disabled via other macros, and have found this method to be quite reliable.

Let me know if you need some more info and i'll be glad to go into more details!

-Chris

Just popping by to say thank you for this. It solved an "on battery power" problem I was trying to solve. I keep forgetting to check whether functions (or trigger values) can help, and this is a useful reminder.

Thanks

1 Like