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
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.
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?
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.
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:
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,
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.