AppleScript条件语句仅在iTerm2未运行时触发语法错误求助
解决iTerm2未运行时AppleScript触发的语法错误问题
这个问题我之前也碰到过,核心原因是当iTerm2未运行时,AppleScript无法加载它的专有脚本术语,导致编译tell application "iTerm2"块里的代码时,识别不了current window、create tab这类属于iTerm2的类和命令,从而抛出语法错误。
修复后的脚本
把你的脚本修改成下面这样,就能解决问题:
on run if application "iTerm2" is running then using terms from application "iTerm2" tell application "iTerm2" tell current window create tab with default profile end tell end tell end using terms from end if end run
为什么这样有效?
using terms from application "iTerm2"语句会提前导入iTerm2的脚本术语集,让AppleScript在编译阶段就能识别所有iTerm2专属的类、命令和属性,哪怕此时iTerm2并没有在运行。这样就避免了因为应用未运行导致术语无法解析的语法错误。
额外提示
如果你之后需要扩展脚本,比如在iTerm2未运行时也启动它并执行操作,可以去掉if判断,直接用tell application "iTerm2"(它会自动启动应用),再配合using terms from确保术语正常解析:
on run using terms from application "iTerm2" tell application "iTerm2" if not (exists current window) then create window with default profile end if tell current window create tab with default profile end tell end tell end using terms from end run
内容的提问来源于stack exchange,提问作者Daniel Krom
相关产品推荐
相关产品推荐

