Monterey ,how to use apple sciprts get AirPods Pro battery ?

how to use apple sciprts get AirPods Pro battery ?

This currently works:

use scripting additions
use framework "Foundation"

getAirPodsProBatteryLevel("Dirks AirPods Pro") -- change with your device name

--> {BatteryCase:"86 %", BatteryLeft:"90 %", BatteryRight:"90 %"}

on getAirPodsProBatteryLevel(deviceName)
	set xmlData to do shell script "system_profiler SPBluetoothDataType -xml"
	set theData to ((current application's NSString's stringWithString:xmlData)'s dataUsingEncoding:(current application's NSUTF8StringEncoding))
	set infoDict to (current application's NSPropertyListSerialization's propertyListWithData:theData options:0 format:(missing value) |error|:(missing value))
	set devlist to ((infoDict's firstObject()'s valueForKey:"_items")'s firstObject()'s valueForKey:"device_connected")
	if devlist ≠ missing value then
		repeat with j from 0 to (count of (devlist as list)) - 1
			if (devlist's objectAtIndex:j)'s allKeys()'s firstObject() as text = deviceName then
				set devDic to ((devlist's objectAtIndex:j)'s valueForKey:deviceName)
				set BatteryCase to (devDic's valueForKey:"device_batteryLevelCase") as string
				set BatteryLeft to (devDic's valueForKey:"device_batteryLevelLeft") as string
				set BatteryRight to (devDic's valueForKey:"device_batteryLevelRight") as string
				return {BatteryCase:BatteryCase, BatteryLeft:BatteryLeft, BatteryRight:BatteryRight}
			end if
		end repeat
	end if
end getAirPodsProBatteryLevel

Thank you for sharing the script
I am getting error, any advise Cheers
Error → Expected “given”, “with”, “without”, other parameter name, etc. but found “:”
Line →
set infoDict to (current application's NSPropertyListSerialization's propertyListWithData:theData options:0 format:(missing value) |error|:(missing value))

Strange. Have you installed the latest macOS (12.3.1) update?
Apple has changed this with version 12.3. It works for me:

What happens if you run "system_profiler SPBluetoothDataType -xml" in the terminal? Are you getting anything back like this (PropertyList-1.0.dtd)?

Edit:
You may need to use the following within BTT, as AppleScriptObjC is used:

BatteryLevel's getAirPodsProBatteryLevel("Dirks AirPods Pro")
--> {BatteryCase:"86 %", BatteryLeft:"90 %", BatteryRight:"90 %"}

script BatteryLevel
	use scripting additions
	use framework "Foundation"
	
	on getAirPodsProBatteryLevel(deviceName)
		set xmlData to do shell script "system_profiler SPBluetoothDataType -xml"
		set theData to ((current application's NSString's stringWithString:xmlData)'s dataUsingEncoding:(current application's NSUTF8StringEncoding))
		set infoDict to (current application's NSPropertyListSerialization's propertyListWithData:theData options:0 format:(missing value) |error|:(missing value))
		set devlist to ((infoDict's firstObject()'s valueForKey:"_items")'s firstObject()'s valueForKey:"device_connected")
		if devlist ≠ missing value then
			repeat with j from 0 to (count of (devlist as list)) - 1
				if (devlist's objectAtIndex:j)'s allKeys()'s firstObject() as text = deviceName then
					set devDic to ((devlist's objectAtIndex:j)'s valueForKey:deviceName)
					set BatteryCase to (devDic's valueForKey:"device_batteryLevelCase") as string
					set BatteryLeft to (devDic's valueForKey:"device_batteryLevelLeft") as string
					set BatteryRight to (devDic's valueForKey:"device_batteryLevelRight") as string
					return {BatteryCase:BatteryCase, BatteryLeft:BatteryLeft, BatteryRight:BatteryRight}
				end if
			end repeat
		end if
	end getAirPodsProBatteryLevel
end script

Hi Dirk
Thank you for replying
I am on 12.2.1
The script fails to compose on the script editor & BTT. it fails to compose at line

set infoDict to (current application's NSPropertyListSerialization's propertyListWithData:theData options:0 format:(missing value) |error|:(missing value))

I tried to modify it, it compiles, reads the xml, but fails to output the battery levels

set infoDict to (current application's NSPropertyListSerialization's propertyListWithData:theData options:0 |format|:(missing value) |error|:(missing value))

Thank you for looking at it

Apple has changed the XML with macOS 12.3. Under 12.2.1, it can therefore not work.

Edit:
For 12.2.1 you can try to change the following line:

set devlist to ((infoDict's firstObject()'s valueForKey:"_items")'s firstObject()'s valueForKey:"device_connected")

to:

set devlist to ((infoDict's valueForKey:"_items")'s firstObject()'s firstObject()'s valueForKey:"devices_list")

Maybe it works. Otherwise I can't help you, since I don't have macOS 12.2.x.

Tx Dirk

updating to 12.3.1, the script works

Thank you