Mac Sonoma下AppleScript批量关闭失效Terminal窗口失败求助
解决Sonoma系统下Terminal AppleEvent Handler失败问题
Sonoma系统对Terminal的AppleScript交互逻辑做了变更,导致旧清理脚本触发-10000错误,以下是可行的修复方案:
1. 验证自动化权限
- 打开「系统设置」→「隐私与安全性」→「自动化」
- 确认BBEdit和Script Editor.app均已获得控制Terminal.app的权限,若未勾选则手动开启
2. 替换为兼容Sonoma的脚本
旧脚本可能因直接操作窗口索引或未处理异常导致报错,使用以下脚本可稳定清理僵尸窗口:
tell application "Terminal" repeat with targetWindow in windows try -- 仅关闭无运行进程的窗口(判断僵尸窗口的核心逻辑) if (count of processes of targetWindow) is 0 then close targetWindow saving no end if on error -- 跳过无法正常交互的窗口,避免脚本中断 continue repeat end try end repeat end tell
如果需要关闭所有非活跃的Shell窗口(无论是否有进程),可改用:
tell application "Terminal" repeat with targetWindow in windows try if name of targetWindow starts with "Shell" or name of targetWindow starts with "zsh" or name of targetWindow starts with "bash" then close targetWindow saving no end if on error continue repeat end try end repeat end tell
3. 额外排查步骤
- 重启Terminal.app以及运行脚本的工具(BBEdit/Script Editor)
- 重置Terminal偏好:删除
~/Library/Preferences/com.apple.Terminal.plist后重启Terminal,可解决部分权限或缓存问题
内容的提问来源于stack exchange,提问作者Daniel Stranathan
相关产品推荐
相关产品推荐

