Skip to content

[Bug] macOS iTerm2 会话启动时窗口卡住/空白 #21

Description

@sortbyiky

问题描述

在 macOS 上使用 Coding-Tool Web UI 点击会话,选择 iTerm2 作为终端工具时,iTerm2 打开后窗口空白卡住,不执行任何命令。

复现步骤

  1. npm install -g coding-tool
  2. ct start 启动服务
  3. 打开 Web UI (http://localhost:10099)
  4. 设置 → 终端工具 → 选择 iTerm2
  5. 点击任意会话
  6. iTerm2 打开,但窗口空白,不执行 claude -r {sessionId} 命令

环境信息

  • 操作系统: macOS Sequoia 15.x
  • coding-tool 版本: 2.2.7
  • iTerm2 版本: Build 3.5.x
  • Node.js 版本: v22.21.1

原因分析

问题出在 src/server/services/terminal-detector.js 第 110 行的 AppleScript 语法:

// 当前代码(有问题)
command: 'osascript -e \'tell application "iTerm" to create window with default profile command "cd {cwd} && claude -r {sessionId}"\''

create window with default profile command "..." 这个一步到位的语法在新版 iTerm2 中不稳定,经常导致窗口创建后不执行命令。

修复方案

将 AppleScript 拆分为三步执行:

  1. 激活 iTerm2: tell application "iTerm" to activate
  2. 创建窗口: tell application "iTerm" to create window with default profile
  3. 写入命令: tell application "iTerm" to tell current session of current window to write text "..."

建议修改

// 修复后的代码
command: 'osascript -e \'tell application "iTerm" to activate\' -e \'tell application "iTerm" to create window with default profile\' -e \'tell application "iTerm" to tell current session of current window to write text "cd \\\'{cwd}\\\' && claude -r {sessionId}"\''

完整 diff

diff --git a/src/server/services/terminal-detector.js b/src/server/services/terminal-detector.js--- a/src/server/services/terminal-detector.js+++ b/src/server/services/terminal-detector.js@@ -107,7 +107,7 @@ function detectMacTerminals() {
id: 'iterm2',
name: 'iTerm2',
available: true,
isDefault: false,
- command: 'osascript -e \'tell application "iTerm" to create window with default profile command "cd {cwd} && claude -r {sessionId}"\''+ command: 'osascript -e \'tell application "iTerm" to activate\' -e \'tell application "iTerm" to create window with default profile\' -e \'tell application "iTerm" to tell current session of current window to write text "cd \\\'{cwd}\\\' && claude -r {sessionId}"\''
});
}

验证

修改后测试命令:

osascript -e 'tell application "iTerm" to activate' \
-e 'tell application "iTerm" to create window with default profile' \
-e 'tell application "iTerm" to tell current session of current window to write text "echo Hello from fixed iTerm2"'

执行后 iTerm2 正常打开并执行命令。

临时解决方案

在官方修复前,用户可以:

  1. 在 Web UI 设置中切换为 Terminal.app(系统终端)
  2. 手动修改 $(npm root -g)/coding-tool/src/server/services/terminal-detector.js 文件

感谢您开发这个优秀的工具!🎉

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions