升级至macOS Sonoma后,Terminal窗口最小化AppleScript报错问题
问题描述
我工作中常打开多个Terminal窗口登录不同服务器,为此编写了一段AppleScript,用于最小化除最前端窗口外的所有窗口(类似应用程序的“隐藏其他”功能)。升级至macOS Sonoma后,脚本运行报错。
脚本内容:
tell application "Terminal" repeat with thewindow in (get every window where frontmost is false) set miniaturized of thewindow to true end repeat end tell
运行时提示错误:
error "Terminal got an error: AppleEvent handler failed." number -10000
在脚本编辑器中,报错时会高亮every window where frontmost is false)部分。请问该表达式存在什么问题?
问题原因与修复方案
问题出在frontmost属性的使用上——macOS Sonoma更新后,Terminal的AppleScript接口发生了兼容性变化,窗口对象不再直接支持frontmost属性,导致原有的筛选条件无法被Terminal正确解析,触发了AppleEvent处理失败的错误。
可以通过以下方式修复脚本:先获取当前的前端窗口,再遍历所有窗口,排除该窗口后执行最小化操作。修改后的脚本如下:
tell application "Terminal" set frontWindow to front window repeat with theWindow in every window if theWindow is not frontWindow then set miniaturized of theWindow to true end if end repeat end tell
该逻辑通过对比窗口对象本身判断是否为前端窗口,绕开了直接使用frontmost筛选的方式,在macOS Sonoma及更高版本的Terminal中可正常运行。
内容的提问来源于stack exchange,提问作者Barmar
相关产品推荐
相关产品推荐

