Compact AirPods widget with User-Friendly Setup

I get the battery script working, but the 1.2 version will not keep the AirPods name - always asks again for it

EDIT:
I could edit the catalina.json script and it connects and disconnects - but I don't get the icon. It shows shortly and than ist gone :-(( Please help

@Caliguvara?

(i feel pretty helpless without airpods.....)

Sorry for some reason I missed this post!

I have honestly no clue why this doesn't work out for @tosbsas! I just downloaded the 1.2 version again and it works fine here!

I changed the script a little bit with that version, maybe try to copy-paste the attached script and replace the one of your widget (the widget script itself, not the associated action):

return checkStatus()

on checkStatus()
	--GET STORED NAME
	tell application "BetterTouchTool"
		--attempt to get variable
		try
			set LclAirPodsName to get_string_variable "AirPodsWg_DevceName"
		end try
	end tell
	--END GET STORED NAME
	
	--set myString to button returned of (display dialog "Connected?" buttons {"Yes", "No"})
	--set myString to "name: DR-BT101 Connected: " & myString
	
	(*uncomment line below when done testing*)
	set myString to do shell script "system_profiler SPBluetoothDataType"
	
	--initial check if it's not even there
	if myString does not contain LclAirPodsName then
		return " "
	else
		
		--find out if connected/disconnected
		set AppleScript's text item delimiters to "name:"
		set myList to the text items of myString --each item of mylist is now one of the devices
		
		set numberOfDevices to count of myList
		set counter to 1
		repeat numberOfDevices times --loop through each devices checking for Connected string
			if item counter of myList contains LclAirPodsName then
				if item counter of myList contains "Connected: Yes" then
					return checkBattery()
				else if item counter of myList contains "Connected: No" then
					return " "
				else
					display dialog "Error Parsing" --this shouldn't happen
				end if
			end if
			set counter to counter + 1
		end repeat
	end if
end checkStatus

on checkBattery()
	set OUTPUT1 to do shell script "
OUTPUT1='';
BLUETOOTH_DEFAULTS=$(defaults read /Library/Preferences/com.apple.Bluetooth);
SYSTEM_PROFILER=$(system_profiler SPBluetoothDataType); 
MAC_ADDR=$(grep -b2 \"Minor Type: Headphones\"<<<\"${SYSTEM_PROFILER}\"|awk '/Address/{print $3}'); 
CONNECTED=$(grep -ia6 \"${MAC_ADDR}\"<<<\"${SYSTEM_PROFILER}\"|awk '/Connected: Yes/{print 1}'); 
BLUETOOTH_DATA=$(grep -ia6 '\"'\"${MAC_ADDR}\"'\"'<<<\"${BLUETOOTH_DEFAULTS}\"); 
BATTERY_LEVELS=(\"BatteryPercentCombined\" \"HeadsetBattery\" \"BatteryPercentSingle\" \"BatteryPercentRight\"); 

if [[ \"${CONNECTED}\" ]];
 then 
 for I in \"${BATTERY_LEVELS[@]}\";
  do 
  declare -x \"${I}\"=\"$(awk -v pat=\"${I}\" '$0~pat{gsub (\";\",\"\"); print $3}'<<<\"${BLUETOOTH_DATA}\")\";
   [[ ! -z \"${!I}\" ]] && 
   OUTPUT1=\"${OUTPUT}$(awk '/BatteryPercent/{print substr($0)}'<<<\"${I}\")${!I}%\";
    done;
    printf \"%s\" \"${OUTPUT1}\";
fi"
	
	set OUTPUT2 to do shell script "
OUTPUT2='';
BLUETOOTH_DEFAULTS=$(defaults read /Library/Preferences/com.apple.Bluetooth);
SYSTEM_PROFILER=$(system_profiler SPBluetoothDataType); 
MAC_ADDR=$(grep -b2 \"Minor Type: Headphones\"<<<\"${SYSTEM_PROFILER}\"|awk '/Address/{print $3}'); 
CONNECTED=$(grep -ia6 \"${MAC_ADDR}\"<<<\"${SYSTEM_PROFILER}\"|awk '/Connected: Yes/{print 1}'); 
BLUETOOTH_DATA=$(grep -ia6 '\"'\"${MAC_ADDR}\"'\"'<<<\"${BLUETOOTH_DEFAULTS}\"); 
BATTERY_LEVELS=(\"BatteryPercentCombined\" \"HeadsetBattery\" \"BatteryPercentSingle\" \"BatteryPercentLeft\"); 

if [[ \"${CONNECTED}\" ]];
 then 
 for I in \"${BATTERY_LEVELS[@]}\";
  do 
  declare -x \"${I}\"=\"$(awk -v pat=\"${I}\" '$0~pat{gsub (\";\",\"\"); print $3}'<<<\"${BLUETOOTH_DATA}\")\";
   [[ ! -z \"${!I}\" ]] && 
   OUTPUT2=\"${OUTPUT}$(awk '/BatteryPercent/{print substr($0)}'<<<\"${I}\")${!I}%\";
    done;
    printf \"%s\" \"${OUTPUT2}\";
fi"
	if OUTPUT2 is " " then
		return " "
	else
		set BatteryLevel to word 2 of OUTPUT2 & "%" & "
" & word 2 of OUTPUT1 & "%"
	end if
	if BatteryLevel is "" & "
" & "" then
		return " "
	else
		return BatteryLevel
	end if
	
end checkBattery

--thanks to @yyuuiko for the base of the code!
--thanks to @GoldenChaos for the great problemsolving idea for Catalina!

EDIT: I understand from the other thread that you use AirPods Pro. Not sure regarding compatibility. Maybe try this code for AirPods Pro, idk if it still works though that's why I didn't publish it yet (I wrote it months ago when I could get hands on a pair of AirPods Pro for an afternoon).

return checkStatus()

on checkStatus()
	--GET STORED NAME
	tell application "BetterTouchTool"
		--attempt to get variable
		try
			set LclAirPodsName to get_string_variable "AirPodsWg_DevceName"
		end try
	end tell
	--END GET STORED NAME
	
	--set myString to button returned of (display dialog "Connected?" buttons {"Yes", "No"})
	--set myString to "name: DR-BT101 Connected: " & myString
	
	(*uncomment line below when done testing*)
	set myString to do shell script "system_profiler SPBluetoothDataType"
	
	--initial check if it's not even there
	if myString does not contain LclAirPodsName then
		return " "
	else
		
		--find out if connected/disconnected
		set AppleScript's text item delimiters to "name:"
		set myList to the text items of myString --each item of mylist is now one of the devices
		
		set numberOfDevices to count of myList
		set counter to 1
		repeat numberOfDevices times --loop through each devices checking for Connected string
			if item counter of myList contains LclAirPodsName then
				if item counter of myList contains "Connected: Yes" then
					return checkBattery()
				else if item counter of myList contains "Connected: No" then
					return " "
				else
					display dialog "Error Parsing" --this shouldn't happen
				end if
			end if
			set counter to counter + 1
		end repeat
	end if
end checkStatus

on checkBattery()
	set OUTPUT1 to do shell script "
OUTPUT1='';
BLUETOOTH_DEFAULTS=$(defaults read /Library/Preferences/com.apple.Bluetooth);
SYSTEM_PROFILER=$(system_profiler SPBluetoothDataType); 
MAC_ADDR=$(grep -b2 \"Minor Type: Headphones\"<<<\"${SYSTEM_PROFILER}\"|awk '/Address/{print $3}'); 
CONNECTED=$(grep -ia6 \"${MAC_ADDR}\"<<<\"${SYSTEM_PROFILER}\"|awk '/Connected: Yes/{print 1}'); 
BLUETOOTH_DATA=$(grep -ia6 '\"'\"${MAC_ADDR}\"'\"'<<<\"${BLUETOOTH_DEFAULTS}\"); 
BATTERY_LEVELS=(\"BatteryPercentCombined\" \"HeadsetBattery\" \"BatteryPercentSingle\" \"BatteryPercentRight\"); 

if [[ \"${CONNECTED}\" ]];
 then 
 for I in \"${BATTERY_LEVELS[@]}\";
  do 
  declare -x \"${I}\"=\"$(awk -v pat=\"${I}\" '$0~pat{gsub (\";\",\"\"); print $3}'<<<\"${BLUETOOTH_DATA}\")\";
   [[ ! -z \"${!I}\" ]] && 
   OUTPUT1=\"${OUTPUT}$(awk '/BatteryPercent/{print substr($0)}'<<<\"${I}\")${!I}%\";
    done;
    printf \"%s\" \"${OUTPUT1}\";
fi"
	
	set OUTPUT2 to do shell script "
OUTPUT2='';
BLUETOOTH_DEFAULTS=$(defaults read /Library/Preferences/com.apple.Bluetooth);
SYSTEM_PROFILER=$(system_profiler SPBluetoothDataType); 
MAC_ADDR=$(grep -b2 \"Minor Type: Headphones\"<<<\"${SYSTEM_PROFILER}\"|awk '/Address/{print $3}'); 
CONNECTED=$(grep -ia6 \"${MAC_ADDR}\"<<<\"${SYSTEM_PROFILER}\"|awk '/Connected: Yes/{print 1}'); 
BLUETOOTH_DATA=$(grep -ia6 '\"'\"${MAC_ADDR}\"'\"'<<<\"${BLUETOOTH_DEFAULTS}\"); 
BATTERY_LEVELS=(\"BatteryPercentCombined\" \"HeadsetBattery\" \"BatteryPercentSingle\" \"BatteryPercentLeft\"); 

if [[ \"${CONNECTED}\" ]];
 then 
 for I in \"${BATTERY_LEVELS[@]}\";
  do 
  declare -x \"${I}\"=\"$(awk -v pat=\"${I}\" '$0~pat{gsub (\";\",\"\"); print $3}'<<<\"${BLUETOOTH_DATA}\")\";
   [[ ! -z \"${!I}\" ]] && 
   OUTPUT2=\"${OUTPUT}$(awk '/BatteryPercent/{print substr($0)}'<<<\"${I}\")${!I}%\";
    done;
    printf \"%s\" \"${OUTPUT2}\";
fi"
	
	set BatteryLevel to OUTPUT2 & "
" & OUTPUT1
	
	if BatteryLevel is "" & "
" & "" then
		return " "
	else
		if OUTPUT2 is " " then
			return " "
		else
			return OUTPUT2 & "
" & OUTPUT1
		end if
		
	end if
	
end checkBattery

--thanks to @yyuuiko for the base of the code!
--thanks to @GoldenChaos for the great problemsolving idea for Catalina!
1 Like

you are the man - the second script fixed it :-)) I get the icon and after changing the wording in the script I got it all as it should - yeah

One question remains - I don't see the icon on app specific touchbar settings - any way to accomplish that? Is there copy paste or something like that? (:-)) found that too

1 Like

If I remember correctly that's because the icons provided by @yuuiko are white (white on white has such a high contrast :wink:). Try these
AirPods Icon AirPods Icon_2

thanks

The latest preset (the one created by me) supports both airpods and pro, it can detect the model and change the icon to the corresponding image, if you dont mind, try it out :grin:

did that

@Noon_Chen any idea where apple hide the original icon used feg in iOS?
I like unified design and would love to make that little "adjustment" to your awesome widget since I use both, AirPods 1st Gen and AirPods Pro, but I'd love to use this icon too. Adding it in the script is no problem, but finding a usable icon is :smiley:

Edit: never mind got it using the icons @GoldenChaos posted a long time ago :+1:t4:

Sorry for the delayed response, you can find macOS default Bluetooth device icons in the following location:
/System/Library/Frameworks/IOBluetoothUI.framework/Versions/A/Resources

1 Like

Thank you so much!

Thank to you as well! As I looking thru the plists in that folder, I think I could create a widget that can support all bluetooth headphones from apple, and change the icon accordingly.

1 Like

This would be nice indeed.
RN I struggle with the fact that I use several W1/H1 headphones (AirPods 1st Gen, AirPods Pro, Beats). I’d like to use something like “short tap”VS“long tap” to connect one device or another but the widget itself a checks only for one device at this time. Have to check for it later.

Great idea! I would definitely think how to accomplish it.

I would also need some help from Beats(X), Solo, Powerbeats users, as the battery key in bluetooth.plist may be different for various products.

I could help out with as I said AirPods 1st Gen, AirPods Pro and Beats Solo :+1:t4:
I found some hack/workaround to check for the battery by looking all the time for every device - but that appears like shooting a fly with a cannon bullet :smiley:

Great! Can you run defaults read /Library/Preferences/com.apple.Bluetooth in the terminal and use ⌘+F to search the name of your Beats Solo, what is the key of the battery info?

This is the battery key of my beatsX, I want to make sure is it the same for all non-airpods model.

Bad news.

FD5AFFA0-47DE-4775-8C7E-B6B56421CD4A" =         {
            BatteryPercentSingle = 79;

Thanks! It's actually good news, there could be only two sets of battery key, one for airpods and one for others, I think the preset would be ready today, I'll inform you when it's done.

1 Like

Thanks, your code worked ! Such a nice little icon to connect my AirPods Pro, really glad !:slight_smile:

@Noon_Chen @Caliguvara

Just want to check if the top post has your latest versions!