1

I want to implement a alfred workflow to control my AirPods Pro to switch between "Transparency Mode" and "ANC Mode". How can I write an apple script to simulate click on "audio" menu bar to switch noise-canceling. Or there is a better solution?

Vxider
  • 11
  • 5
  • Hello! I know that this is not accepted. But for a long time I cannot resolve the issue. Could you help me? I would be very grateful. I can't find AirPods Pro in my area. Could you just open up my small xcode project and test it please? https://stackoverflow.com/questions/69851479/audio-files-wont-play-with-airpods-pro-on-ios-15 –  Nov 13 '21 at 19:05

2 Answers2

0

I found a simple apple script solution after trying.

tell application "System Events"
    tell process "SystemUIServer"
        click (menu bar item 1 of menu bar 1 whose description contains "volume")
        click menu item "your AirPods name" of menu 1 of result
        click menu item "noise control mode" of menu 1 of result
    end tell
end tell

Change the your AirPods name to your AirPods name and change the noise control mode to which you want to (like Off, Noise Cancellation, or Transparency, or to your language as 关闭,降噪,通透模式 in Chinese).


Inspired by anton-uspehov's answer. I updated the script to automatic connect AirPods when it is not connected.

tell application "System Events"
    tell process "SystemUIServer"
        click (menu bar item 1 of menu bar 1 whose description contains "volume")
        try
            click menu item "your AirPods name" of menu 1 of result
            click menu item "noise control mode" of menu 1 of result
        on error
            key code 53
            click (menu bar item 1 of menu bar 1 whose description contains "bluetooth")
            click menu item "your AirPods name" of menu 1 of result
            click menu item "Connect" of menu 1 of result
        end try
    end tell
end tell

Or if your want to auto switch between Noise Cancellation and Transparency

tell application "System Events"
    tell process "SystemUIServer"
        click (menu bar item 1 of menu bar 1 whose description contains "volume")
        try
            click menu item "your AirPods name" of menu 1 of result
            if value of attribute "AXMenuItemMarkChar" of menu item "Transparency" of menu 1 of result is "✓" then
                click menu item "Noise Cancellation" of menu 1 of result
            else
                click menu item "Transparency" of menu 1 of result
            end if
        on error
            key code 53
            click (menu bar item 1 of menu bar 1 whose description contains "bluetooth")
            click menu item "your AirPods name" of menu 1 of result
            click menu item "Connect" of menu 1 of result
        end try
    end tell
end tell

For macos Big Sur (10.14) users, use the following script

set AirPodsName to "Your AirPods name"
tell application "System Events"
    tell application process "ControlCenter"
        set volMenu to menu bar item "volume" of menu bar 1
        tell volMenu to click
        set btCheckbox to checkbox 1 of scroll area 1 of group 1 of window "ControlCenter" whose title contains AirPodsName
        set btCheckboxValue to value of btCheckbox
        tell btCheckbox to click
        delay 0.1
        set checkbox_anc to checkbox 1 of scroll area 1 of group 1 of window "ControlCenter" whose title contains "Noise Cancellation"
        if exists checkbox_anc then
            if value of checkbox_anc is 1 then
                set checkbox_transparent to checkbox 1 of scroll area 1 of group 1 of window "ControlCenter" whose title contains "Transparency"
                tell checkbox_transparent to click
            else
                tell checkbox_anc to click
            end if
        end if
        tell volMenu to click
    end tell
end tell
Vxider
  • 11
  • 5
0

I also had this problem, so I solved it in this way (with the error handling if the AirPods are not connected + popups):

tell application "System Events"
    tell process "SystemUIServer"
        click (menu bar item 1 of menu bar 1 whose description contains "volume")
        try
            click menu item "nestim AirPods Pro" of menu 1 of result
            if value of attribute "AXMenuItemMarkChar" of menu item "Transparency" of menu 1 of result is "✓" then
                click menu item "Noise Cancellation" of menu 1 of result
                display notification "Noise Cancellation active" with title "Noise control:"
                return "Noise Cancellation active"
            else
                click menu item "Transparency" of menu 1 of result
                display notification "Transparency mode active" with title "Noise control:"
                return "Transparency mode active"
            end if
        on error
            tell application "System Events"
                key code 53
                display notification "Something went wrong" with title "Noise control:" sound name "Submarine"
                return "Something went wrong"
            end tell
        end try
    end tell
end tell
anton.uspehov
  • 332
  • 3
  • 9