执行tell application "iTerm2" 脚本时出现command not found错误如何解决
报错原因定位
你脚本中使用的tell、repeat、split vertically等语法是AppleScript专属语法,但你在脚本开头声明的执行解释器是#!/bin/bash,系统会将脚本默认作为Bash脚本执行,Bash环境中不存在对应命令,因此触发批量command not found类报错。
正确实现方案
有两种常用的实现方式,可根据你的使用场景选择:
方案1:直接运行AppleScript脚本
将代码保存为后缀为.scpt的AppleScript专属脚本,修改首行解释器声明即可:
- 新建
split_iterm.scpt文件,内容如下:
#!/usr/bin/osascript tell application "iTerm2" tell current session of current tab of current window write text "ssh host" repeat 1 times split vertically with default profile end repeat end tell tell second session of current tab of current window write text "ssh host" end tell end tell
- 给脚本添加执行权限:
chmod +x split_iterm.scpt - 直接运行脚本即可:
./split_iterm.scpt
方案2:在Bash脚本中嵌入AppleScript调用
如果你需要把逻辑整合到已有的Bash脚本中,可以通过osascript命令包裹AppleScript代码,示例如下:
- 新建
iterm_split.sh文件,内容如下:
#!/bin/bash # 调用osascript执行AppleScript逻辑 osascript <<EOF tell application "iTerm2" tell current session of current tab of current window write text "ssh host" repeat 1 times split vertically with default profile end repeat end tell tell second session of current tab of current window write text "ssh host" end tell end tell EOF
- 给脚本添加执行权限:
chmod +x iterm_split.sh - 运行脚本即可:
./iterm_split.sh
注意:运行脚本前请确保iTerm2处于打开状态,且存在处于激活状态的窗口与标签页,避免脚本找不到目标会话报错。
内容的提问来源于stack exchange,提问作者Maskiin
相关产品推荐
相关产品推荐

