Can I use KM to monitor my trackpad battery?

I use an apple wireless trackpad and for the most part it is pretty good. Its one failing is that when the battery is low I get an alert and with maybe only an hour of battery life left. If I'm facilitating workshop, I may not have enough open usb ports to plug my trackpad in. To avoid this I want to create a KM action that would alert any evening the battery is below X%.

Is this possible? I'm guessing most of the work will be at the applescript level.

Yes, very doable with Keyboard Maestro and an AppleScript. Here is a portion of a macro I use to track battery levels on several devices.

To make it work with your device, make sure to adjust the following line:

set trackpadName to "Trackpad"

Simply put in the name of your Trackpad in the quotations mark where I have simply "Trackpad".

In my full macro, I also have it sent me notifications via Pushover to my phone when my devices drop below 20%, as well as updating my Stream Deck every 15 minutes with their percentages.

Note that I am currently running Big Sur, but this was the same AppleScript I used on Catalina too, so if you're on either one of those OS it should work for you. If it doesn't work for some reason, please let me know and I (or more than likely somebody WAY more knowledgeable in AppleScript than me) can figure out something that will work for you.

EDIT: Just realized that two of my notifications can be combined for simplicity. Right now the notifications for below 20% and below 10% do the same thing...because in my full macro below 20% just gives me a notification on my local machine, whereas below 10% also gives me a Pushover notification. Since not everybody uses Pushover I removed that action to upload it here without realizing it made the two separate portions of the macro redundant. Here's an updated one without the extra notification section

Check Bluetooth Devices Battery Levels (for export).kmmacros (8.4 KB)

1 Like

Hi @cdthomer,

Thanks for sharing.
I tried it. It does not return anything.
Local_MTBatteryLevel is empty from the AppleScript.

I am connected to the Trackpad.
image

You're welcome.

I may know what the issue is. A local variable is only used during the macro's execution...and once it's done running the variable will not return any info. Since this macro is designed to alert you only if the battery is at 20% or lower, if it's above that, there will be no alert, and since the variable is a local, you won't see it after the macro is done running.

If you want to confirm it works, run the version posted here. It's the same thing, except using a global variable (same variable, just without the Local_ which means it will stick around afterwards if you want to see it's contents), and I also added a display window at the end that will show you the battery percentage regardless of whether it hits the 20% threshold or not.

If it still doesn’t work, let me know because it's likely an AppleScript issue.

04)Check Bluetooth Devices Battery Levels (for export).kmmacros (9.9 KB)

I added a debug action (I use often) to display the variable. I was not looking for it in the variable palette.
image

Just to confirm, the way you set it up the display window does not show anything?

Change the AppleScript action from “save results to a variable” to “show results in a window” and then just run the script manually and see what if anything appears.

Also, what OS and version of KM are you using?

Edit: also, did you change the name in the script to match the name of your trackpad exactly?

1 Like

Ah. You caught me!
It's working now!
Thanks!

1 Like

Haha nice! When I have an issue with a macro more often than not it’s a simple little thing like that. Glad it’s working for you!

1 Like

Using a shell script would be more efficient than using AppleScript for this. Although it may not matter with modern processors.

Still, for the sake of information, this will do the same thing as the AppleScript

    ioreg -r -k "BatteryPercent" \
    | egrep '"Product"|"BatteryPercent"' \
    | fgrep -i -A1 'Magic Trackpad' \
    | tr -s '\012' ' ' \
    | awk '{print $NF}'

In the third line, change 'Magic Trackpad' to whatever the name of your Trackpad is.

Be sure to put 'single quotes' around the name.

3 Likes

Thanks for sharing this. Recently I have been wondering about using a shell script to query Bluetooth battery levels but it’s been very far down on my to do list so I have never looked into it.

I tried this script, but I'm having a problem.

My original trackpad name contains a single quote. I changed the name in the Bluetooth setting.

the AppleScript @cdthomer shares is able to get the updated name.
But the Shell script still shows the original name. Maybe I need to restart my computer for the Shell script?

Edit:
After a restart, the device name was indeed updated. However, the command did not output the target information. The line containing the battery level information was cut off.
I changed the script a little bit and was able to get the battery level:

ioreg -r -k "BatteryPercent" \
| egrep '"Product"|"BatteryPercent"' \
| tail -n 1 \
| awk -F '= ' '{print "Trackpad: " $2}'

Sorry, I should have mentioned that if you have a ' in the name, then you can either do this:

     ioreg -r -k "BatteryPercent" \
    | egrep '"Product"|"BatteryPercent"' \
    | fgrep -i -A1 'TJ\'s Magic Trackpad' \
    | tr -s '\012' ' ' \
    | awk '{print $NF}'

or this:

     ioreg -r -k "BatteryPercent" \
    | egrep '"Product"|"BatteryPercent"' \
    | fgrep -i -A1 "TJ's Magic Trackpad" \
    | tr -s '\012' ' ' \
    | awk '{print $NF}'

That's assuming that the name of the Trackpad was named

TJ's Magic Trackpad

1 Like

You're welcome. I already had it for SwiftBar and Stream Deck, so I figured I might as well share it.

I think the main problem is that system_profiler is a huge slow beast.

ioreg can be slow too if you don't limit it to the general area we are looking for, but I think it's generally faster than system_profiler.

Both of them are fairly esoteric to the average Mac user, of course, so it's not a huge surprise that few people know about either of them. I'm sure that I only know them from some StackExchange post,

1 Like

Thanks for that. I've added it to a button on my Stream Deck to get the values to pop up whenever I need to check. Very useful.
Thanks again.

1 Like

Just out of curiosity (because I am not familiar with Shell Scripts), is there a way to query the battery percentage of multiple devices from a single shell script?

I have the same thing. It runs every 15 minutes or whenever I press the button. It shows the percentages of my keyboard, trackpad and mouse, and the battery icon changes colors depending on the percentage level of the devices.