Help with modifying some code pls

Hi,

Can some help me modify the following code to just show the percentage of the battery (ie. 100%)? Much appreciated.

--Original script by Noon Chen, modified by yuuiko to support any bluetooth device and list UI setup.
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 currentModel to "Generic" --default widget icon
tell application "BetterTouchTool" to set AirPodsName to get_string_variable "BluetoothWg_2_Device"

if AirPodsName is "" then
	return jsonData(currentModel, "Tap to Setup", 0, 14)
else if AirPodsName is missing value then
	return jsonData(currentModel, "Tap to Setup", 0, 14)
end if

if devNames contains AirPodsName then
	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
			
			------SPECIAL PRODUCT BEHAVIOUR------
			try
				set ProductID to do shell script "/usr/libexec/PlistBuddy -c \"print :DeviceCache:" & MAC_addr & ":ProductID\" /Library/Preferences/com.apple.Bluetooth.plist"
			on error
				set ProductID to "ERROR"
				set currentModel to "Not Airpods"
			end try
			
			--set icons
			if ProductID = "8206" then
				--AirPods Pro
				set currentModel to "AirPods Pro"
				
			else if ProductID = "8207" or ProductID = "8194" then --AirPods 1 & 2, ProductID = 8207/8194
				set currentModel to "AirPods"
				
			else
				set currentModel to "Not Airpods"
			end if
			
			------SPECIAL PRODUCT BEHAVIOUR------
			
			
			------SET TEXT TO RETURN------
			if connectValue = 1 then
				
				try --scan for "BatteryPercentLeft" and "BatteryPercentRight", used in AirPods and probably beats true wireless.
					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"
					
					if BatteryLeft = "0" and BatteryRight = "0" then
						return jsonData(currentModel, "Not Connected", 0, 15)
					else
						set batteryText to formatValue(BatteryLeft, "🅛") & "\\n" & formatValue(BatteryRight, "🅡") -- Connected Output
					end if
					
				on error --scan for "BatteryPercent", used in probably apple magic mice, trackpad and keyboards.
					try
						set BatteryLeft to do shell script "/usr/libexec/PlistBuddy -c \"print :DeviceCache:" & MAC_addr & ":BatteryPercent\" /Library/Preferences/com.apple.Bluetooth.plist"
						log BatteryLeft
						
						if BatteryLeft = "0" then
							return jsonData(currentModel, AirPodsName & "\\n" & "Not Connected", 0, 11) --SET OUTPUT probably apple magic mice, trackpad and keyboards not connected
						else
							set batteryText to AirPodsName & "\\n" & "Battery: " & (round BatteryLeft * 100 rounding down) & "%" --SET OUTPUT probably apple magic mice, trackpad and keyboards connected
						end if
						
					on error --scan for "BatteryPercentSingle", used in probably beats.
						try
							set BatteryLeft to do shell script "/usr/libexec/PlistBuddy -c \"print :DeviceCache:" & MAC_addr & ":BatteryPercentSingle\" /Library/Preferences/com.apple.Bluetooth.plist"
							log BatteryLeft
							
							if BatteryLeft = "0" then
								return jsonData(currentModel, AirPodsName & "\\n" & "Not Connected", 0, 11) --SET OUTPUT probably beats not connected
							else if BatteryLeft is greater than 1 then
								set batteryText to AirPodsName & "\\n" & "Battery: " & BatteryLeft & "%" --SET OUTPUT probably beats connected
							else
								set batteryText to AirPodsName & "\\n" & "Battery: " & (round BatteryLeft * 100 rounding down) & "%" --SET OUTPUT probably beats connected
							end if
							
						on error --handle any other generic bluetooth device with no battery report
							log "these aren't airpods"
							
							set currentModel to "Not Airpods"
							set batteryText to AirPodsName & "\\n" & "Connected" --SET OUTPUT generic device connected
						end try
					end try
				end try
				
			else
				return jsonData(currentModel, AirPodsName & "\\n" & "Not Connected", 0, 11) --OUTPUT display device but not connected
			end if
			------SET TEXT TO RETURN------
			
			log "break loop"
			return jsonData(currentModel, batteryText, connectValue, 11) --OUTPUT 
			
		end if
	end repeat
else
	log "widget not setup"
	return jsonData(currentModel, "Tap to Setup", 0, 14)
end if

--style battery display
on formatValue(batteryLevel, prefix)
	if batteryLevel = "0" then
		return prefix & " --"
	else
		return prefix & batteryLevel & "%"
	end if
end formatValue

--Format into json data to output to BTT
on jsonData(currentModel, batteryText, connectValue, fontSize)
	
	set airpods_iconData to "iVBORw0KGgoAAAANSUhEUgAAADwAAAA8CAYAAAA6/NlyAAABxklEQVRoge2ZwUoCURSGfwuiZavW+RBugp4jkraCrxG9RgX5BrVo76Lc1NK1uhe0hYZKTtw4Fw6Dd/TM6IwO/wcHx5lzz/n/Ye5hRBBCCCGEEEJInpwDuAcwADBU0QJwadDhcp9jNVzNO+mxF7wAWACIEqK9gdD2mhoL6VUYJwC+14jUMU4QOjbUcT1PizA9MYj08bWizmeKOpO8zX5I4zcAM6PYqqpTNa6dSU93/J6nYb+nYHwcIxlknpZxrd8WfmaYOcpg2jdfyueVBNT3ioQ+fxE4DuXrur5X0jxIJIthv1bf6dBd1+d/1PHUuNYfHxt0boVfdbdHxseyrgRcG9eOZN1SNOTGqwjoAJgbh04cy9CbS89INOSKdVi5aK4Q2ExRJ/UezsKRvPptKvIxodeDoc4w4+zJzJMMn5BAN6QaGzRpSG6ozlTes/eGG9lfPYkugFoKcTVZ6+t0pPZB4B+9MzERois5B09dXgH9YxnCX3e5t7s0Xdlh7VUGQ/0suZkodNoVAQ2XHRouOzRcdmi47NBw2aHhskPDZWfrhqMo+g/5X1czUNfiub11uXuJF6nE9uXHfT9+zZJLCCGEEEIIIfkC4A9P0HeTJCMKAwAAAABJRU5ErkJggg=="
	set airpods_dark_iconData to "iVBORw0KGgoAAAANSUhEUgAAADwAAAA8CAYAAAA6/NlyAAACT0lEQVRoge2Yz0sWQRjHP5tvaZhRULwGmgcjiLKTf0CdOmkieOkQdfGP6hQG3jpUh+jWoW6JCFZkdSgPJh5VDHnzlZFnZZx8Z2dmt5dcng8sszvzzDzPd37sziyKoiiKoiiKoighZ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xZhD8DBiUCXfaVliJthDcBizN2OcOcNubaRF9SF9fwQgteDewBzgAbM3Y9yFwHfjltMnAHQ3pYGjB7fqd8n7vBHZk7P8UeOy1LQzoX3DBg979WmBvhv6Sbm7w2kYC+PWf0IIHgLteAGpX0Y1sbdfkw31uGOgP6aBF0LoH3PQCkIg+XKfPTmCd1/YEuOp9z7QJGgEdHgF/dQ9XBnWxBjQfmdU1XtsDjdrBKRgJrtChS7XWwJa8VVbWFTJg5ZB1tfRR6922GtvHHfCSzqof+IISozz8Aoxq7VtrpmX539B9a0qsengceKnZU6v32R+NxC9iOBIzl/4OTFRpF8GfYzkRa4bnAMf0PMtHlvlq4J0OiikxBM9TsbPrPCOiVwKfdM+bYS14gSYcsybxbFGj+bjlSablHpYAdcQT+959QE88njtNRa24Oq2cshK8BOjxIvJX4EKVZ694J5bi05Yq2VcQLARL3nwQmOm0ybnzaeB3jT4XgdfOfYumpetDO2dRD/d4sUHEntHXTy0ky+rTd3Gl0pIsbFvo49vQgrs9sW9U7GTp18LDpSukg6EFjzrXI7pUs7BIg53Lt5AOhi4P+/TEQvbqUMa+cn61z9v78oo6H9LB0IJl/92fQr9VwC7PnzFdIT8D+md2AJCF+VXESll5tkGgmxLNIPi4F+gkpz5nZcz6xKMap+p89gq4ZGm8GWa4gkT1y9ZGmkXwsJ5QmpPHj2l+sT8USyw5/eVhQosLNKu6lYMPiUQikUgkEonEtAD+AYohf02fXGWtAAAAAElFTkSuQmCC"
	
	if currentModel = "AirPods Pro" then
		if connectValue = 1 then
			set icon to airpods_pro_iconData
		else
			set icon to airpods_pro_dark_iconData
		end if
	else if currentModel = "AirPods" then
		if connectValue = 1 then
			set icon to airpods_iconData
		else
			set icon to airpods_dark_iconData
		end if
	else --if not airpods
		if connectValue = 1 then
			set icon to not_airpods_icon
		else
			set icon to not_airpods_dark_icon
		end if
	end if
	
	return "{\"text\":\"" & batteryText & "\", \"icon_data\": \"" & icon & "\", \"font_size\": \"" & fontSize & "\"}"
end jsonData

Thanks.

Hello,

Your script uses AppleScriptObjC, Bash and a third party component (PlistBuddy). Due to time constraints, I can't adapt it for you directly.
However, I have created a script using pure AppleScriptObjC based on the script that gives you the following values:

ConnectedState (0 or 1)
BatteryPercentLeft (0 or 1-100)
BatteryPercentRight (0 or 1-100)

for the device with the name specified in the "AirPodsName" variable. You would then need to create the touchbar button script yourself.

use framework "Foundation"
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

-- change to your device name! 
set AirPodsName to "AirPods Pro von Dirk" 

set ConnectedState to 0
set BatteryPercentLeft to 0
set BatteryPercentRight to 0

if devNames contains AirPodsName then
	repeat with i from 1 to count devNames
		-- find device
		if (item i of devNames) = AirPodsName then
			set ConnectedState to item i of devStatus
			-- only in case if device is connected
			if ConnectedState = 1 then
				-- load plist data
				set theData to (current application's NSData's dataWithContentsOfFile:("/Library/Preferences/com.apple.Bluetooth.plist"))
				set infoDict to (current application's NSPropertyListSerialization's propertyListWithData:theData options:0 format:(missing value) |error|:(missing value))
				-- get device data
				set devDic to ((infoDict's valueForKey:"DeviceCache")'s valueForKey:(item i of devAddress))
				-- get battery data
				set BatteryPercentLeft to (devDic's valueForKey:"BatteryPercentLeft") as integer
				set BatteryPercentRight to (devDic's valueForKey:"BatteryPercentRight") as integer
			end if
			exit repeat
		end if
	end repeat
end if

-- log result for debug
log ConnectedState
log BatteryPercentLeft
log BatteryPercentRight

-- display notification for demonstration
if ConnectedState = 1 then
	display notification "Left: " & BatteryPercentLeft & "%  " & "Right: " & BatteryPercentRight & "%" with title AirPodsName subtitle "State: connected"
else
	display notification "State: not connected" with title AirPodsName
end if

Tested with AirPods & AirPods Pro on Big Sur

Hey, thanks for your reply. I should've mentioned something. This way I'm wanting to display the percentage of the battery is on the menu bar, so its always being displayed and I can easily look at it.
Also, I'm wanting it to display the percentage of my Magic Trackpad, not headphones. Is this possible?

Thanks for the help!

I don't have a Magic Trackpad, so I can't tell you if this is possible or not. With a Magic Keyboard you can get the battery level with this AppleScript. I think it should be the same with Magic Trackpad:

use framework "Foundation"
use framework "IOBluetooth"
use scripting additions

-- change the name of the BluetoothDevice here:
set BluetoothDeviceName to "Magic Trackpad name"

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 ConnectedState to 0
set BatteryPercent to 0

if devNames contains BluetoothDeviceName then
	repeat with i from 1 to count devNames
		-- find device
		if (item i of devNames) = BluetoothDeviceName then
			set ConnectedState to item i of devStatus
			-- only in case if device is connected
			if ConnectedState = 1 then
				-- load plist data
				set theData to (current application's NSData's dataWithContentsOfFile:("/Library/Preferences/com.apple.Bluetooth.plist"))
				set infoDict to (current application's NSPropertyListSerialization's propertyListWithData:theData options:0 format:(missing value) |error|:(missing value))
				-- get device data
				set devDic to ((infoDict's valueForKey:"DeviceCache")'s valueForKey:(item i of devAddress))
				-- get battery data
				set BatteryPercent to (devDic's valueForKey:"BatteryPercent") as real
				if BatteryPercent > 0 and BatteryPercent < 1 then
					set BatteryPercent to (BatteryPercent * 100)
				end if
				set BatteryPercent to BatteryPercent as integer
			end if
			exit repeat
		end if
	end repeat
end if

log ConnectedState
log BatteryPercent

But! Currently I didn't find a way in BTT to change the title of an entry created with the trigger "Custom Menubar Icon". Maybe Andreas or someone else has an idea. If so, you can use the script in a timer trigger and the BatteryPercent value to update the entry.

1 Like

this returns nothing in script editor

You must enable "Show protocol" (⌘3) or add:

display notification "BatteryPercent " & BatteryPercent with title "ConnectedState: " & ConnectedState
delay 0.1

at the end of the script.

thanks, but this still isn't what I want. Im not wanting a notification to pop up, Im wanting my battery to be always displayed in the menu bar.

@yuuiko, since you wrote the code, would you be able to do this?

isn't there a way to just display a line from the bluetooth dropdown in the menu bar? like cant I just display the line that shows the battery of what I want?

In my opinion it's not possible with a script, because it would have to run endlessly. In BTT it's currently not possible to change a menu item (at least I did not found a way).

Therefore I have packed the script into an applet:

Bluetooth Device Battery Level.zip (173.8 KB)

This creates an icon in the menu bar after starting:

Screen1

Then you can select your Bluetooth device:

Screen2

If the device reports a battery status, then the percentage is displayed to the right of the icon:

Screen3

For AirPods, the level is displayed separately (left / right) if it differs:

Screen4

The update interval is 15 seconds.
If you want to start the applet automatically, you must add it manually (in the Users & Groups Preferences) to Your Login-Items.

Note: I have created the applet just for fun and will not give support for this!

Tested with AirPods, AirPods Pro and Magic Keyboard on Big Sur
Compiled for Intel and Apple Silicon (M1)

Have fun!

1 Like

woah, I highly suggest making a new topic and sharing this preset, its very cool. thanks heaps

how can I select multiple devices to display the battery of?

This is not provided. It is designed to display a single value.

1 Like

Final Release:

The icon can be selected:

Screen1

The separate percentage of the AirPods is only displayed when the difference is more than 2%:

Screen2

Bluetooth Device Battery Level.zip (196.3 KB)

Have fun!

thanks! how exactly do I make it show two percentages? im wanting it to show the trackpad battery and the keyboard battery. also, can I have a seperate icon for the second percentage?

Update for macOS Monterey:
Bluetooth Device Battery Level.zip (196.8 KB)
(only tested with AirPods Pro)

1 Like

Update for macOS Monterey 12.3.1 (21E258):
Bluetooth Device Battery Level.zip (196.8 KB)
(only tested with AirPods Pro)