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

@otanan, @Caliguvara

I’d think a single quote would work fine though i can see the possibility of it not working

You can escape characters with a backslash e.g. Otonan\’s AirPods.

The script’s condition is also an “if contains”, so that’s why only typing in the name partially should work.

——

Regarding the new UI setup:
A dropdown is not native to applescript dialog boxes so creating one is waay out of my league, and also I’m not sure how that would work while not connected.. I’d need airpods myself to code for that too, right now I don’t have any W1 accessories so It would be hard to code for it.

Also thanks for bringing up the case of illegal characters! I’d need to implelent some sort of rejection or auto-escape into the UI setup now I guess.

——

@Caliguvara
My preset had a beats widget from @GoldenChaos, I never saw the code for it but maybe it could be similar and all it takes to work is just changing the reference name..

Edit: ah on second read I see your bug, its displaying what it’s not meant to right? I’m not sure how I can find the issue out since I don’t have these ‘phones, but maybe take a look again at my or GoldenChaoses beats and airpods widgets and see how they differentiate themselves

Hi @everyone, I've just uploaded improvements and fixes to my take of the AirPods widget.
It has:

  • Improved, Neater Aesthetics
  • Super Sharp and Crispy Sub-Pixel aligned icon!
  • Automatic, No-Code setup
  • Error Detection that lets you reset the name if it can't find your AirPods.
  • And of course, the two line battery display for L and R AirPods

I think it's pretty ready to head out properly, but since I don't have AirPods I'd like anyone to test it and give me some feedback over at the post below.

Download development build d1.1 there and tell me how it behaves! (and what you think, too.)

Hi,

This action is very nice! it works perfectly for me except for the alternate icon... I can't make it working...

The result when not connect is "\n" ("+new line+") but when I write that regex expression it does not works.. And obviously it does not works with an empty feild as well!

Does anybody have it working?

Which Version did you load?

I used the Caliguvara v1.1.bttpreset

Hm, it works for me here.
Try to paste this script and replace the YOUR_AIRPODS_NAMR_HERE with your AirPods name (twice).

Script
return checkStatus()

on checkStatus()
	
	--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 "YOUR_AIRPODS_NAME_HERE" then
		return false
	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 "YOUR_AIRPODS_NAME_HERE" 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"
	
	return OUTPUT2 & "
" & OUTPUT1
end checkBattery

The output for the alternative icon should be space (" ").
If it still doesn't work, could you just copy the JSON of the file to your clipboard (right click on the widget) and send it to me in a private message? So I can take a look into it.

In two or three weeks (just let me pass my exams plz xD) you will also find and update of my Caliguvara C2 Preset, with a way better version of that widget done in collaboration with @yuuiko.
It will also be kinda integrated into an all new AirPlay widget I just finished, more to come with the Update to C3, I don't want to spoil anything :wink:

Hi everyone, just update the script, which not requires your airpods name :slight_smile:
The script returns 2 values - left and right Airpods battery level
New Airpods Battery Script.json (63.9 KB)

1 Like


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