可通过applescript在0%、30%、60%、100%间循环切换音量,支持静音/恢复联动并记忆上一非零音量,还可绑定快捷键或菜单栏实时显示。
可以通过 applescript 快速在几个常用音量值之间切换(比如 0%、30%、60%、100%),无需打开系统设置或手动拖动控制中心滑块。
基础实现:直接设为指定音量
macOS 的 AppleScript 支持通过 set volume 命令控制输出音量,取值范围是 0–100(整数):
set volume output volume 60
这条命令会立即把系统音量设为 60%,同时保持静音状态不变(若原为静音,仍静音;若原为启用,就启用并设音量)。
循环切换预设音量组
用列表存储常用音量值,配合当前音量查表实现“按一次升一级”,支持回到起点(如 0 → 30 → 60 → 100 → 0):
set presetVolumes to {0, 30, 60, 100}
set currentVol to round (output volume of (get volume settings))
<p>-- 查找当前音量在预设中的位置(从 1 开始)
set currentIndex to 0
repeat with i from 1 to length of presetVolumes
if item i of presetVolumes is currentVol then
set currentIndex to i
exit repeat
end if
end repeat</p><p>-- 计算下一个索引(循环:超尾则回 1)
if currentIndex is 0 then
-- 当前音量不在预设中,设为第一个
set nextVol to item 1 of presetVolumes
else
set nextIndex to (currentIndex mod (length of presetVolumes)) + 1
set nextVol to item nextIndex of presetVolumes
end if</p><p>set volume output volume nextVol</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill6187" title="Apple Silicon Ai"><img
src="https://img.php.cn/upload/skill/000/000/081/179083455666841.jpg" alt="Apple Silicon Ai" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill6187" title="Apple Silicon Ai" class="overflowclass">Apple Silicon Ai</a>
<p class="overflowclass">Apple Silicon AI — 在 Mac Studio、Mac Mini、MacBook Pro 和 Mac Pro 上运行大语言模型(LLM)、图像生成、语音转文字及嵌入(embeddings)。将您的 Apple Silicon 设备…</p>
</div>
<a rel="nofollow" href="/xiazai/skill6187" title="Apple Silicon Ai" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>增强版:带静音/取消静音联动
实际使用中,常希望「0%」代表静音,且再次触发时恢复上次非零音量。可加状态记忆逻辑:
- 读取当前
output volume和mute状态 - 若当前已静音,解除静音并恢复上一次非零音量(需用文件或 UserDefaults 暂存)
- 若当前未静音且音量为 0,视为主动静音,设
set volume with output muted - 否则按预设循环切换
临时方案可用 AppleScript 的 do shell script 调用 defaults 存取上一音量:
-- 读取上次非零音量(默认 50)
try
set lastVol to (do shell script "defaults read com.example.volumePreset lastVolume") as integer
on error
set lastVol to 50
end try
<p>if mute of (get volume settings) then
-- 解除静音,并恢复 lastVol
set volume output volume lastVol
do shell script "defaults write com.example.volumePreset lastVolume -int " & lastVol
else
set currentVol to round (output volume of (get volume settings))
if currentVol = 0 then
-- 主动静音
set volume with output muted
else
-- 记录当前音量,再切换
do shell script "defaults write com.example.volumePreset lastVolume -int " & currentVol
-- 然后执行上面的循环切换逻辑...
end if
end if</p>绑定到快捷键或菜单栏运行
将脚本保存为 .scpt 文件后,可用以下方式调用:
- 快速启动:用 Alfred / Raycast 配置关键词触发
- 全局快捷键:通过 Automator 创建“快速操作”,导出为 App 后用系统设置 → 键盘 → 快捷键 → 服务中分配快捷键
- 菜单栏工具:用 BitBar / SwiftBar 运行脚本并显示当前音量(需脚本返回文本)
例如 SwiftBar 只需脚本末尾加:return "? " & (round (output volume of (get volume settings))) & "%",即可实时显示。










