Hide AXPress in AppleScript


I am using an AppleScript as a TouchBar button to change the listening mode of the AirPods Pro. However I could only achieve this by letting the script doing clicks in the UI with AXPress.

Does anyone know, how I can do this without needing the UI clicks? I am an absolute AppleScript noob unfortunately :pensive:


set AIRPODS_PRO_NAME to "Name of AirPods Pro"
set ANC to "Noise Cancellation"
set TRANSPARENCY to "Transparency"

tell application "System Events"
	tell process "SystemUIServer"
		tell (menu bar item 1 of menu bar 1 whose description contains "volume") -- find volume menu bar item
			perform action "AXPress" -- click on it
			tell (menu item 1 of menu 1 whose name contains AIRPODS_PRO_NAME) -- find airpods menu item
				perform action "AXPress" -- click on it
				tell (menu 1) -- open airpods menu
					if (value of attribute "AXMenuItemMarkChar" of menu item ANC) is equal to "✓" then
						perform action "AXPress" of (menu item TRANSPARENCY) -- switch to Transparency
						perform action "AXPress" of (menu item ANC) -- switch to ANC
					end if
				end tell
			end tell
		end tell
	end tell
end tell

What's the problem with AXPress? (This is the Apple intended way to activate them).

I'll soon add a native function to BTT to switch between the listening modes, but on first glance - for an Apple Script solution - yours looks fine.

The way it's written in the script here, makes each click visible (open Bluetooth menu, open AirPods Pro menu, etc.). I'd like to have the clicks like "happening in the background, invisibly" or something..

Cool! I guess I'll just wait for your built-in solution then :slightly_smiling_face:

Can the listening mode be set using System Preferences ? If so, that's a better way to AppleScript this than through the menu bar.