如何使用applescript控制AirPods Pro的降噪?

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

我想实现一个 alfred 工作流程来控制我的 AirPods Pro 在 "Transparency Mode" 和 "ANC Mode" 之间切换。如何编写一个苹果脚本来模拟点击 "audio" 菜单栏来切换降噪。或者有更好的解决方案?

经过尝试,我找到了一个简单的苹果脚本解决方案。

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

your AirPods name 更改为您的 AirPods 名称,并将 noise control mode 更改为您想要的名称(例如 OffNoise CancellationTransparency,或您的语言 关闭,降噪,通透模式(中文)。


anton-uspehov的回答启发。我更新了脚本以在未连接时自动连接 AirPods。

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

或者如果您想在 Noise CancellationTransparency

之间自动切换
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

对于 macos Big Sur (10.14) 用户,使用以下脚本

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

我也遇到了这个问题,所以我就这样解决了(加上AirPods未连接+弹窗的错误处理):

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