AppleScript在Split View模式下操作Terminal窗口报错的解决方法咨询
Terminal分屏模式下AppleScript主题切换脚本报错解决办法
我编写了一段用于切换Terminal标签页主题的AppleScript脚本:
tell application "Terminal" -- 获取当前标签页 set currentTab to selected tab of first window -- 将当前标签页主题设置为Homebrew set currentTab's current settings to settings set "Homebrew" end tell
该脚本在Terminal普通窗口状态下可正常运行,但在Split View分屏模式下会抛出以下错误:
terminal-color.scpt: execution error: Terminal got an error: AppleEvent handler failed. (-10000)
注:此处所指Split View模式如图所示:
解决方法
方法1:使用front window替代first window
分屏模式下,first window不一定指向当前活跃的分屏窗口,改用front window可以精准定位到当前正在交互的窗口,同时增加存在性检查避免报错:
tell application "Terminal" set currentWindow to front window if currentWindow exists then set currentTab to selected tab of currentWindow set currentTab's current settings to settings set "Homebrew" end if end tell
方法2:遍历所有窗口查找选中标签页
如果分屏窗口的活跃状态检测存在异常,可以遍历所有Terminal窗口,找到包含选中标签页的窗口进行操作:
tell application "Terminal" repeat with win in windows if exists selected tab of win then set currentTab to selected tab of win set currentTab's current settings to settings set "Homebrew" exit repeat end if end repeat end tell
方法3:检查自动化权限
虽然普通窗口能运行脚本,但仍需确认系统权限设置:打开「系统设置」→「隐私与安全性」→「自动化」,确保Terminal已被授予脚本控制权限。
内容的提问来源于stack exchange,提问作者Kraktus
相关产品推荐
相关产品推荐

