A better AirPod Connection Widget: less space for the same information

Good! can you share your AirPods Wideget?

1 Like

I just tried the script above with my AirPods pro and its returning a few extra values and new lines.

the output is:

"73

36

73%

71

18

71%"

never mind - figured it out

I somewhere have the script for the AirPods pro. Gonna post it later :ok_hand:t4:

Edit - didn't see you found the solution :ok_hand:t4:

Can you share the Airpods widget? It looks great.

1 Like

could u share this widget to use? thx a lot! it looks so great

1 Like

Ill see what I can do to provide a preset that is available for all. Might have it up in its own thread today...

Link to available preset...

hi there trying to use your code but the battery information isn't displaying properly (some of it is cut off) what setting do I need to change (basically need to wrap text)

Using my code or my preset? Post a screen shot of the output you are seeing. Wraping text output is in the code, maybe you removed the carriage returns.

ive imported the airpod pro preset, haven't touched the code having that issue where there is extra values included. might be an issue using non-pro AirPods?

Hi,

I've installed the button and replaced the three instances of AIRPODS_NAME_HERE with my own AirPods name but it doesn't do anything. The button on the TouchBar has 'false' next to the icon and doesn't do anything when I press it?

Hey, I really don't understand how to install a json file :confused: Could you please do a tutorial?

I didn't do this in years I'm not even sure I remember how to :flushed: when you visualize your presets in BTT, with the button "import" can you select and therefore import a .json?

This widget doesn't seem to work on Big Sur anymore.. Even when my AirPods are connected the widget shows as "not connected". Did anyone else experience this issue? And can someone suggest a solution?

I'm having similar issues - when looking through the script I am using, it specifically looks for bluetooth menu options for connect and disconnect. On Big Sur, those options are no longer there - rather you just click on the headphones you want to connect to.

I'm assuming this is part of what is causing the issue, but unfortunately do not know how to fix it.

Try this widget. It works flawless and needs way less setup :slight_smile:

I did, and it works, but it doesn't let me set my own icons, which is basically why I was using this widget. Any ideas how I can set my own icons for this?

You'll have to encode them in base64 and put them in the script. You know how to do this?

I managed to encode them but I don't know where in the script to put them. Can you help out? :slight_smile:

And is there an option to have an alternative icon based on the connection status, or is it always the same?

Totally forgot about them, sorry!

Try this script. It's a modified version of what @Noon_Chen did, this one just checks for the AirPods Pro.

--Based on the code by Noon Chen, chennoon233@foxmail.com
use framework "IOBluetooth"
use scripting additions

set deviceList to current application's IOBluetoothDevice's pairedDevices()
set devNames to (deviceList's valueForKey:"name") as list
set devStatus to (deviceList's valueForKey:"connected") as list
set devAddress to (deviceList's valueForKey:"addressString") as list

--Set default model and battery info to make the code more tidy
set batteryText to formatValue("0", "πŸ…›") & "\\n" & formatValue("0", "πŸ…‘")
set currentModel to "AirPods"

tell application "BetterTouchTool" to set AirPodsName to get_string_variable "AirPodsWg_DevceName"

if devNames contains AirPodsName then
	--Device is found
	repeat with i from 1 to count devNames
		if (item i of devNames) = AirPodsName then
			--Get index of AirPods in the list and save connection status and MAC address value
			set connectValue to item i of devStatus
			set MAC_addr to item i of devAddress
			
			--set corresponding icon based on product id, I ignored vendorID for simplicity
			set ProductID to do shell script "/usr/libexec/PlistBuddy -c \"print :DeviceCache:" & MAC_addr & ":ProductID\" /Library/Preferences/com.apple.Bluetooth.plist"
			
			if ProductID = "8206" then
				--AirPods Pro
				set currentModel to "AirPods Pro"
				--else if ProductID = "xxxx" then --Add product ID of new model here
				--set currentModelIcon to newModel_iconData
			else
				--Default model: AirPods, ProductID = 8207/8194...
			end if
			
			--Connected, update battery value
			if connectValue = 1 then
				set BatteryLeft to do shell script "/usr/libexec/PlistBuddy -c \"print :DeviceCache:" & MAC_addr & ":BatteryPercentLeft\" /Library/Preferences/com.apple.Bluetooth.plist"
				set BatteryRight to do shell script "/usr/libexec/PlistBuddy -c \"print :DeviceCache:" & MAC_addr & ":BatteryPercentRight\" /Library/Preferences/com.apple.Bluetooth.plist"
				
				set batteryText to formatValue(BatteryLeft, "πŸ…›") & "\\n" & formatValue(BatteryRight, "πŸ…‘")
				
			end if
			
			--return to break repeat loop
			return jsonData(currentModel, batteryText, connectValue)
		end if
	end repeat
else
	--Device not found, return default data
	return jsonData(currentModel, batteryText, 0)
end if

--Adding utf icon and % character
on formatValue(BatteryLevel, prefix)
	if BatteryLevel = "0" then
		--If one of AirPods is in the case, battery would be 0
		return " "
	else
		return prefix & BatteryLevel & "%"
	end if
end formatValue

--Format into json data that BTT can recognize
on jsonData(currentModel, batteryText, connectValue)
	--Add AirPods base64 iconData here, add new line if newer model is released.
	set airpods_pro_iconData to "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAAXNSR0IArs4c6QAAAERlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAQKADAAQAAAABAAAAQAAAAABGUUKwAAAGXElEQVR4AeWZa6hVRRTHrSzLLNHKR2X3ZnUVMzLQjCB7WBhE+YAgKxAhiIKgMCow/BL0pYheIEFGX6MoE3oQcSk/JMWFSKMyEvIBZQiGWmZp9f/Rnss4zJ5Z+9x9uvtsF/zP7JlZs2atNXvWrD1nzJgTnE7qkv3InSZcK1wjLBRmCOOEQ8JW4XPhU2GbcFhoBY2VFauFfcI/FbBJvLOE/51QuCqxugPCEmGBgIxvhDcFVvJKYbJQhW4XM9go3CUcERpJl0ir74TY6h5T+1PCbGF9CU9sXNj2s8aeIzSOlkmjUNmw/rd4Hhdw1PsG/nC8q+/R2NOExhCvtVMuV/4l3qXCfOFAhXGh3Kc1thF0srTgtQwVTNW/Ev9lwlDFcaHMqrFE01UjjMvRVWKYWjDxWp6SG6B+jjz4dhl4UywrUp119FkcwN6HONePCqxSjsaLAQfsTTDOVN+5wlnC6QLO5URhnNPrQT13lSzH4KJCgz9VWoyHHQMAY0IiUObeIo5awHyu1GP95DydktxXdHLMQSiUI94U+CdEGE9Vm5PhygjbME/OWbGx5jaLA9wqWlefyYn+OIBYEBJynCxXxnhcm3O8q9daWhywy5uRFUutmmPdWTz0uQavZAtYCOfsEMqcZJGR5bE4YLCQwqpivOWVxGnEF4JcjDDKAlLjUafp0sBXlhX06+Ez+/8mgSMsxxuODevzJKMR9La0CJUrq78s3suFLRXGxGT9oPGW7Sa27hPn9G4hpqjf9r145ghrDbz+uNgz6Xej6Gxp87UQU5Y2vhL5PF4k/CaU8VnaH9P4RhJB807hJ8E35DPV2a+3Rfp8PsvzG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+z+tU8uI37gFeQmFohKPcHMlffPhuZBZ6fz84OHgfX4UHWd8NLn65n7iHvoW2zot83iJAGbt79+4ZLInf0f6mogWsJSeYj6NvoGsK0XAF8j1BnHaOl3cA1OD8Ke1VbGyLCeVHoQXdIGPsCbMyv1sqlaHqzw0Ap7VywpAdTuFzV3b3EkCpo8iS0T1JToSpGzdu7NcxmfomBABeRrJpbofe6NXnoP0b4OaRi6jEx9Fl95q3TdDOnE+5cOxxWqaj8ybuCZ78VCr424SNAHGFP8+9ln3ifZNbbIpz2RTfNvF59U/YCBBj2eQ+oJK/PNc+bIiv2lx/eSmZ0ACIwcys3EO+6WW8ohEB09kQ21Q7SD3hAcC5FJmdXMhIKq57VrBkFugYvPomPABiNElOD8ei8e+G2S9eluTIy1E/2qQAQIzn0uVFouF5P0cy9BKyzOcAy3pznzQAiIM4Jx9UpsRnETnE0gwgxsoaKaOmPDGQQt9OJLxhGI4gSNUSNWcNfOZbYZOCfPfjlJwI2V+efMYHo8hRuR7z6c+SjQxZzgn0YpklzuO+8RGT2ZMSALmHZMdfYHKOfvns1j6TEgDxiB9g32edb9B6Z0igRHbSAiDGc8skX4xvybvHI39PZLwUndQAsNGl+HF1EY4OuQD4D7fWtQDU4aKPaU5qAMSbhoaGXm6cr+P1OEX+7nAxm2Te/ucp403u579JwfPg+ttDGQAAAABJRU5ErkJggg=="
	
	--determine the color & icon based on model & status
	if currentModel = "AirPods Pro" then
		if connectValue = 1 then
			set icon to airpods_pro_iconData
			return "{\"text\":\"" & batteryText & "\", \"icon_data\": \"" & icon & "\"}"
		else
			set icon to airpods_pro_dark_iconData
			return "{\"font_color\": \"145,145,145,255\", \"text\":\"" & batteryText & "\", \"icon_data\": \"" & icon & "\"}"
		end if
		--else if currentModel = "xxxx" --for futher model
	end if
end jsonData 

Make sure BTT has your AirPods Name saved as string_variable "AirPodsWg_DevceName" (or correct the line in the Script)

1 Like