You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用AppleScript设置或切换Mac音频输出至AirPlay/HomePod Mini

用AppleScript管理Mac音频输出设备

1. 设置音频输出为AirPlay音箱(如HomePod Mini)

要稳定实现该功能,推荐使用SwitchAudioSource命令行工具(需先安装),它比直接操作系统UI更可靠。

步骤1:安装工具

打开终端执行:

brew install switchaudio-osx

步骤2:AppleScript脚本

-- 替换为你的AirPlay设备实际名称
set targetAirPlayDevice to "HomePod Mini"
-- 切换到指定输出设备
do shell script "SwitchAudioSource -s '" & targetAirPlayDevice & "' -t output"

执行脚本后,系统会立即将音频输出切换到目标AirPlay设备。

2. 在内置音箱与HomePod Mini之间自动切换

基于SwitchAudioSource工具,脚本会先检测当前输出设备,再自动切换到另一台设备。

AppleScript脚本

-- 根据系统语言调整设备名称:中文系统用"内置扬声器",英文系统用"Built-in Speaker"
set builtInSpeaker to "内置扬声器"
set homePodDevice to "HomePod Mini"

-- 获取当前活跃的音频输出设备
set currentOutputDevice to do shell script "SwitchAudioSource -c -t output"

-- 判断并执行切换
if currentOutputDevice is builtInSpeaker then
    do shell script "SwitchAudioSource -s '" & homePodDevice & "' -t output"
else
    do shell script "SwitchAudioSource -s '" & builtInSpeaker & "' -t output"
end if

注意事项

  • 若Mac系统为英文,将builtInSpeaker的值改为"Built-in Speaker"
  • 确保homePodDevice与你的HomePod Mini实际名称完全一致(可在系统偏好设置>声音>输出中查看)

备选方案:直接操作系统偏好设置(不推荐)

如果不想安装第三方工具,可通过AppleScript操作系统偏好设置,但这种方式易因系统UI更新失效,且需开启辅助功能权限:

set targetDevice to "HomePod Mini"

tell application "System Preferences"
    activate
    set current pane to pane id "com.apple.preference.sound"
end tell

tell application "System Events"
    tell process "System Preferences"
        click radio button "输出" of tab group 1 of window "声音"
        select row 1 of table 1 of scroll area 1 of tab group 1 of window "声音" where value of text field 1 is targetDevice
    end tell
end tell

quit application "System Preferences"

使用前需在「系统偏好设置>安全性与隐私>隐私>辅助功能」中,添加运行脚本的应用(如Script Editor)。

内容的提问来源于stack exchange,提问作者Marceli Wac

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 07:35:22