How To

Note: As of Update 4 of this post, I’ve rewritten this section!! The script will automatically grab the MAC Address of your AirPods, and you can even skip downloading the script and copy/paste in the code 😀

Put the .sh file somewhere nice, I put mine in /Users/duck/Scripts/AirPodsPower.sh
For the rest of this article, wherever you see /Users/duck/Scripts/AirPodsPower.sh, replace it with wherever you put the file.

Open up your favourite text editor (I like Sublime Text), you’ll want to change the line at the top that says: MACADDR to be the MAC address of your AirPods.

You can get this by holding option down, clicking on the Bluetooth Icon in your menu bar, then going to your AirPods (when they’re connected).

Open up a Terminal window and type:

chmod +x /Users/duck/Scripts/AirPodsPower.sh

(Change this to wherever you put the script)

You can test the script by typing: /Users/duck/Scripts/AirPodsPower.sh

Now, to the Better Touch Tool part of it:

Go to the TouchBar section, click the “+ Widget” button.

Under Select Touch Bar Widget, go to “Run AppleScript and Show Return Value”

In here enter the following:

return do shell script "/Users/duck/Scripts/AirPodsPower.sh"

Click Compile/Check Syntax, then Run Script to make sure it runs okay. Hit Save and you’re good!

Extra Notes

While I was working this out, I worked out a bunch of extra bits that I couldn’t easily google. These pretty much *only* apply to AirPods in their current form, but can be modified to read all sorts of other bits.

First up, here’s how to see if a Bluetooth device is connected via the command line (Replace $MACADDR with the MAC Address of the device):

Using system_profiler, you can get the state of a whole bunch of stuff on the system in either XML (using the -xml switch) or indented form. I just run that command showing me all the Bluetooth Devices, grep for the MAC Address of my AirPods, grab the next 6 lines of text (this includes the line saying “Connected: Yes”, then replaces the words “Connected: Yes” with a 1 to make it neater for working with the shell script.

This was built just for AirPods, so for other devices you may need to change the pcregrep bit to check for more/less lines after the MAC address is found.

Secondly, getting the data for the battery levels from a device via Command Line (again, replace $MACADDR with the MAC Address of the device).

defaults read /Library/Preferences/com.apple.Bluetooth will return all the info about bluetooth in json form, which seems to include every bluetooth device that has farted anywhere near the machine, so the next part will pcregrep for the MAC Address and include everything until the closing tag.

Then the next line grabs just the BatteryPercentCase part.

Also note it’s only really reading a cached value rather than a “live” value. So it’ll be a little bit behind (little bit being like 10-30seconds or so, good enough for what I want to do).

Hopefully this helped you! If you like this article, leave a comment and say hi 🙂

Update 4 – Skipping the .sh file part

In Better Touch Tool, when you get to the step to paste in the AppleScript, paste the following:

Thanks for the great post! I’ve used your code as a base to really make the Touchbar useful with my Airpods.

I found the case battery was always 0% so I left that out and reduced space so just says something like “L50 R50”, changed “Not Connected” to simply N/C and have an icon of AirPods added. Also have a match on return so background colour/icon is changed if not connected (dimmed)