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


Hmmm :slightly_frowning_face: :man_shrugging:t4:

Looks like you append my code to yours. Keep only my and, it should works)


:wink: :man_shrugging:t4: :frowning:

Could you share the .json? :wink:

New Airpods Battery Script.json (63.9 KB) :rocket:

1 Like

Damn :frowning:
Yours right (black), mine left (grey)
Running macOS 10.15.1



As the other one it basically returns four values here. Each Headphone twice, once with the % and once without :man_shrugging:t4:

Using a different method, with similar results.

2 Likes

Not stupid, gave me an idea… :thinking:
Let's see if this works :wink:


Edit: Three questions actually @K2DesignLab:

  1. Are you running Catalina?
  2. If so, would you mind sharing the code for your widget? :innocent:
  3. And if I manage to adapt it into my code, might I use it for the Catalina compatible Caliguvara Preset? This would be awesome thanks in advance! :smiley:

Hello,

  1. Yes running on Catalina.
  2. The code is based on some code found here and elsewhere, so im happy to share. Took me days to get the bugs out and find a good process that works 100% of time in Catalina.
  3. There are a few moving parts to what im doing here.

I can imagine :hushed: I'm totally unable to find what part of the code to take out in order to get only the battery percentage as a return, and not the other value (I don't even know what this one is).

Placed it in its own preset. Im sure you'll figure it out. This works in Catalina and Mojave. Obviously, will only get battery levels when AirPod charging case is open or AirPods are connected.

K2-AirPods.bttpreset (209.2 KB)

Enjoy my work?
paypal.me/K2DesignLab

Ok this is funny.
Here is what your script gives me


You imagine the return in the Touch Bar itself… same result than with my code :frowning:
Thanks a lot for your sharing script though! :slightly_smiling_face:

Did you run code without changing the name to Airbeat? Change back to AirPods.

Did so before.
The problem part is actually inside the shell script, and I think there we use quite the same script.

Here from BTT before with no/partially changed names


Not sure what the system is reporting and why its reporting the additional values. I will look at the values before parsed by the script and see if I can find where its getting those values. BTW, What is your MacOS language settings?

English. I have no clue where they come from or what they mean, appeared with Catalina from the first beta on :man_shrugging:t4: There are many screenshots here in the forum too with the same type of result, drives me crazy.

1 Like

yeah all our widgets are based on that shell which isn’t working too well

Interesting, running Catalina, Never installed Beta versions. and I do not have those results in the shell script return values. MacBook Pro 2018, MacOS 10.15.1

A similar result to what @Caliguvara is getting is happening to me to on Catalina with a 2019 MacBook Pro. I have just upgraded from Mojave on a 2017 MacBook Pro (also with touch bar). It's also a pretty new install of BTT.

"L 0
92% R 51
100%"

PROBLEM SOLVED


If you already downloaded the AirPods Widget before, you just need to replace it by the AirPods Widget (Catalina Version). If this is the first time you install the Widget, please download and install the Reset AirPods Widget Trigger as well!

  1. AirPods (Catalina Version).json (67.9 KB)
  2. Reset AirPods Widget.json (1.8 KB)

Solved it indeed just by following @GoldenChaos's hint :+1:t4: The Widget is also integrated into my brand-new PHoeNiX preset :wink:


Important Reminder

  • If you use the "classic" name of the AirPods (like Caliguvara's AirPods) please do not type the name in the Window when asked! Instead, go to System Preferences… → Bluetooth select the device and copy the name there in order to paste it in BTT when asked. The name looks like it contains a simple ' but it seems to be some other character, so your AirPods won't be found if you type it.
  • If your AirPods have any other name (like AirMusic) just go ahead and type their name.

Here is the code of the new widget, for those who wonder

AirPods Battery Level Script
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 word 2 of OUTPUT2 & "%" & "
" & word 2 of OUTPUT1 & "%"

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!

Basically all that changed is the line

set BatteryLevel to word 2 of OUTPUT2 & "%" & "
" & word 2 of OUTPUT1 & "%"

which had been

set BatteryLevel OUTPUT2 & "
" & OUTPUT1

before :wink:

Is it possible to hide the button when the AirPods are not connected? If so how would you achieve that?
My "Touch"Bar is broken and I am using it for info only as I cannot interact with it via touch.