Search code examples
applescript

How to use applescript to control the noise canceling of AirPods Pro?


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?


Solution

  • 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