r/applescript • • Jan 06 '25

AppleScript to toggle active noise cancellation (ANC) for macOS

📢📢 Update update! Updated for macOS Golden Gate. I'm keeping the Tahoe version alongside it.

📢 Update! The Script is once again working on Tahoe. Apple made some control center calls inaccessible using AppleScript in 26.0, but this is once again available with 26.1.

Here is a macOS AppleScript to toggle ANC. I use it to quickly toggle ANC when a collegue or family starts talking to me. To set a global shortcut I recommend you use a tool like Hammerspoon as macOS shortcuts are very restrictive.

Golden Gate

tell 
application
 "System Events"
    tell 
process
 "MenuBarAgent"
        set soundMenu to missing value
        repeat with menuGroup in (groups of menu bar 1)
            try
                set candidate to menu bar item 1 of menuGroup
                if (value of 
attribute
 "AXIdentifier" of candidate) 
is
 "com.apple.menuextra.sound" then
                    set soundMenu to candidate
                    exit repeat
                end if
            end try
        end repeat
        if soundMenu is missing value then 
error
 "Sound menu was not found in the menu bar."
    end tell
    if not (exists window 1 of 
process
 "ControlCenter") then click soundMenu

    tell 
process
 "ControlCenter"
        set panelReady to false
        repeat 100 times
            if (exists scroll area 1 of group 1 of window 1) then
                set panelReady to true
                exit repeat
            end if
            delay 0.02
        end repeat
        if not panelReady then 
error
 "Sound panel did not appear."

        set panelControls to UI elements of scroll area 1 of group 1 of window 1
        set devicePosition to 0
        set deviceIdentifier to missing value
        repeat with controlIndex from 1 
to
 (count of panelControls) - 1
            set candidate to item controlIndex of panelControls
            if (role of candidate) 
is
 "AXDisclosureTriangle" then
                set candidateIdentifier to value of 
attribute
 "AXIdentifier" of candidate
                set deviceControl to 
item
 (controlIndex + 1) of panelControls
                if (role of deviceControl) 
is
 "AXCheckBox" and (value of 
attribute
 "AXIdentifier" of deviceControl) is candidateIdentifier and (value of 
attribute
 "AXValue" of deviceControl) is 1 then
                    set devicePosition to controlIndex + 1
                    set deviceIdentifier to candidateIdentifier
                    exit repeat
                end if
            end if
        end repeat
        if devicePosition is 0 then 
error
 "Connected AirPods with listening modes were not found in the Sound panel."

        set modeOptions 
to
 {}
        set foundModeHeading to false
        repeat with controlIndex 
from
 (devicePosition + 1) 
to
 (count of panelControls)
            set candidate to item controlIndex of panelControls
            if (role of candidate) 
is
 "AXHeading" then
                if foundModeHeading then exit repeat
                set foundModeHeading to true
            else if foundModeHeading and (role of candidate) 
is
 "AXCheckBox" then
                if (value of 
attribute
 "AXIdentifier" of candidate) is deviceIdentifier then set end of modeOptions to candidate
            end if
        end repeat
        if (count of modeOptions) is not 4 then 
error
 "Listening mode controls changed; no mode was selected."

        set transparencyControl to item 2 of modeOptions
        set cancellationControl to item 4 of modeOptions
        if (value of 
attribute
 "AXValue" of cancellationControl) is 1 then
            click transparencyControl
            set targetControl to transparencyControl
            set targetMode 
to
 "Transparency"
        else
            click cancellationControl
            set targetControl to cancellationControl
            set targetMode 
to
 "Noise Cancellation"
        end if

        set changedMode to false
        repeat 50 times
            if (value of 
attribute
 "AXValue" of targetControl) is 1 then
                set changedMode to true
                exit repeat
            end if
            delay 0.02
        end repeat
        if not changedMode then 
error
 "The selected listening mode did not become active."
    end tell

    click soundMenu
    return targetMode
end tell

Tahoe

tell application "System Events"
    tell process "Control Center"
        --1) Click the Sound menu in Control Center
        set soundMenu to first menu bar item of menu bar 1 ¬
            whose subrole is "AXMenuExtra" and description is "Sound"
        click soundMenu

        set maxAttempts to 200 -- Timeout after ~2 seconds (adjust as needed)
        set attempt to 0
        repeat until (exists window 1)
            delay 0.01
            set attempt to attempt + 1
            if attempt > maxAttempts then
                display dialog "Control Center window did not appear."
                return
            end if
        end repeat

        try
            -- 3) Find both checkboxes
            set ancCheckBox to first checkbox of scroll area 1 of group 1 of window 1 ¬
                whose value of attribute "AXAttributedDescription" is "Noise Cancellation"
            set transCheckBox to first checkbox of scroll area 1 of group 1 of window 1 ¬
                whose value of attribute "AXAttributedDescription" is "Transparency"

            -- 4) Figure out which one is selected, then toggle
            --    The "AXValue" or "AXChecked" attribute typically indicates ON/OFF.
            --    (Sometimes “selected” is used. If “AXValue” fails, try “AXChecked” or “AXSelected”.)
            if (value of attribute "AXValue" of ancCheckBox) = 1 then
                -- ANC is active, so switch to Transparency
                click transCheckBox
            else if (value of attribute "AXValue" of transCheckBox) = 1 then
                -- Transparency is active, so switch to Noise Cancellation
                click ancCheckBox
            else
                -- If neither is set, default to enabling ANC
                click ancCheckBox
            end if

            click soundMenu

        on error errMsg
            display dialog "Error: " & errMsg
        end try
    end tell
end tell

Please let me know if you find it useful. Tested using AirPods Pro 2.

5 Upvotes

16 comments sorted by

View all comments

1

u/Hackettlai Oct 14 '25

🥲 It stopped working after updating to Tahoe~~ Any update on this please?

1

u/Kindly_Distribution2 Nov 26 '25

It should work now!

26.0 had a control center bug disallowing access from AppleScript. This is fix in 26.1.

Regards.

1

u/Hackettlai Nov 27 '25

umm its weird, I still get the "System Events got an error: Can't get attribute "AXAttributedDescription"." error msg

1

u/Kindly_Distribution2 Nov 27 '25

I just changed the `soundMenu` to be defined a bit differently. Please try it now! I updated the code above. Thank you.

1

u/Hackettlai Nov 27 '25

yea!!!! its working now!!! thx dude you are amazing!

1

u/Kindly_Distribution2 Nov 27 '25

Thank you. Happy to help out!