Mac可通过快捷指令+AppleScript实现在Touch Bar显示关机倒计时:新建快捷指令“TouchBar关机倒计时”,添加“运行AppleScript”操作,脚本计算剩余时间并循环更新状态栏与Touch Bar显示,30分钟倒计时后自动关机。

你想让Mac每天固定时间自动关机,同时在Touch Bar上实时看到倒计时,避免错过保存文档——系统原生不支持Touch Bar直接显示关机倒计时,但可通过快捷指令+脚本+第三方工具组合实现,关键在于用AppleScript驱动状态栏图标与Touch Bar联动。
用快捷指令+AppleScript创建带Touch Bar倒计时的关机自动化
这一步必须用快捷指令触发,因为只有快捷指令能调用AppleScript并绑定到Touch Bar;终端命令和系统设置无法驱动Touch Bar显示。
打开「快捷指令」App → 点击右上角「+」新建快捷指令 → 命名为「TouchBar关机倒计时」。
点击「添加操作」→ 搜索并添加「运行 AppleScript」→ 在脚本框中粘贴以下代码:
tell application "System Events"
set currentTime to (current date)
set shutdownTime to (currentTime + 1800) -- 默认30分钟后关机,单位为秒
repeat while (currentTime set remainingSeconds to (shutdownTime - currentTime) as integer
set minutesLeft to (remainingSeconds div 60) as integer
set secondsLeft to (remainingSeconds mod 60) as integer
set displayText to "⏳ " & minutesLeft & "′" & secondsLeft & "″"
set frontmostApplication to name of first application process whose frontmost is true
tell application frontmostApplication to set title of window 1 to displayText
delay 1
set currentTime to (current date)
end repeat
shut down
end tell
⚠️ 这段脚本会强制覆盖当前前台应用窗口标题栏文字,仅适用于你正在使用的应用支持标题动态更新(如Safari、TextEdit);若用VS Code或Chrome,可能无效。真正稳定显示需配合第三方工具(见下一节)。
用BetterTouchTool在Touch Bar显示精确关机倒计时
BetterTouchTool是唯一能原生接管Touch Bar并读取系统关机计划的第三方工具,免费版功能已足够使用。
下载安装BetterTouchTool(官网btt.bettertouchtool.net)→ 启动后点左下角「Add New Trigger」→ 类型选「Touch Bar Button」。
利用 macOS 原生能力实现本地语音识别与合成。通过 yap (Apple Speech.framework) 进行语音转文字,通过 say + ffmpeg 进行文字转语音。完全离线,无需 API 密钥。具备音质检测与智能选声功能。
点击右侧「Add New Action」→ 选择「Apple Script (blocking)」→ 粘贴以下脚本:
set now to current date
try
set schedList to do shell script "pmset -g sched | grep shutdown"
set timeStr to word 3 of schedList
set shutdownTime to timeStr & ":00"
set shutdownDate to date (shutdownTime)
if shutdownDate return "⚠️ 已过期"
else
set diff to (shutdownDate - now) as integer
set mins to diff div 60
set secs to diff mod 60
return "? " & mins & "m " & secs & "s"
end if
on error
return "⏱️ 未设关机"
【必须先用系统设置或pmset命令配置好每日关机计划,否则此脚本无法读取时间】
点击「Save」→ 在Touch Bar预览区拖动按钮至合适位置 → 关闭窗口即生效。
先配置底层关机计划(所有方法的前提)
方法一:系统设置法(新手首选)
点击左上角苹果图标 →「系统设置」→ 左侧选「电池」(MacBook)或「节能器」(iMac)→ 滚动到底部点「定时…」→ 勾选「设定以下日期的睡眠、重新启动或关机」→ 下拉选「关机」→ 输入时间(如23:00)→ 选「每天」→ 点「好」。
方法二:终端命令法(精准可靠)
打开终端 → 先清空旧计划:sudo pmset repeat cancel → 回车输密码 → 再执行:sudo pmset repeat shutdown MTWRFSU 23:00:00 → 回车输密码 → 验证:pmset -g sched,确认输出含shutdown条目。
【两种方法都要求Mac必须连接电源适配器,否则关机计划静默失效】

















