AppleScript可原生控制macOS音量与音频设备:设音量用set volume output volume,静音用muted true/false,切输出设备需ControlCenter权限,Music音量独立设置,提示音通过defaults禁用。
可以直接用 applescript 控制 macos 系统音量和切换音频输出设备,无需安装额外软件,系统原生支持,稳定可靠。
AppleScript 通过 set volume output volume 直接写入 Core Audio 层,所有 macOS 版本(Ventura 至 Sequoia)均兼容。
osascript -e 'set volume output volume 60'
osascript -e 'set volume output muted true'
osascript -e 'set volume output muted false'
osascript -e 'output volume of (get volume settings)'
菜单栏音量图标底层即由 AppleScript 驱动,可通过脚本模拟选择逻辑。需确保目标设备已连接并被系统识别。
osascript -e 'tell application "System Events" to tell process "ControlCenter" to click menu item "AirPods Pro" of menu "Output Device" of menu item "Sound" of menu "Control Center" of menu bar 1'
osascript -e 'tell application "System Events" to tell process "ControlCenter" to click (first menu item whose title is "AirPods Pro") of (first menu whose title is "Output Device")'
AppleScript 可跨应用协同操作,适合构建播放+系统音量联动逻辑。
osascript -e 'set volume output volume 40' -e 'tell application "Music" to set sound volume to 40'
osascript -e 'activate application "Music"' -e 'tell application "Music" to set sound volume to 75'
仅关闭按钮声、警告音等 UI 声音,不影响音乐或视频播放。
defaults write NSGlobalDomain com.apple.sound.uikit.enabled -int 0 && killall -HUP SystemUIServer
defaults write NSGlobalDomain com.apple.sound.uikit.enabled -int 1 && killall -HUP SystemUIServer