You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行tell application "iTerm2" 脚本时出现command not found错误如何解决

报错原因定位

你脚本中使用的tell、repeat、split vertically等语法是AppleScript专属语法,但你在脚本开头声明的执行解释器是#!/bin/bash,系统会将脚本默认作为Bash脚本执行,Bash环境中不存在对应命令,因此触发批量command not found类报错。

正确实现方案

有两种常用的实现方式,可根据你的使用场景选择:

方案1:直接运行AppleScript脚本

将代码保存为后缀为.scpt的AppleScript专属脚本,修改首行解释器声明即可:

  1. 新建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
  1. 给脚本添加执行权限:
    chmod +x split_iterm.scpt
  2. 直接运行脚本即可:
    ./split_iterm.scpt

方案2:在Bash脚本中嵌入AppleScript调用

如果你需要把逻辑整合到已有的Bash脚本中,可以通过osascript命令包裹AppleScript代码,示例如下:

  1. 新建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
  1. 给脚本添加执行权限:
    chmod +x iterm_split.sh
  2. 运行脚本即可:
    ./iterm_split.sh

注意:运行脚本前请确保iTerm2处于打开状态,且存在处于激活状态的窗口与标签页,避免脚本找不到目标会话报错。

内容的提问来源于stack exchange,提问作者Maskiin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 08:57:05