如何将 Ctrl+C 发送到除 AppleScript 中的当前选项卡之外的所有 iTerm2 选项卡?

How to send Ctrl+C to all iTerm2 tabs except the current in AppleScript?

我有这段代码可以向 iTerm2 中的所有选项卡(包括当前选项卡)发送特定命令。但是,有时,某些选项卡被卡住,我想在尝试执行命令之前让它们进入 shell 提示符。但是,将 Ctrl+C 发送到当前选项卡,将停止脚本继续。

我想将终止信号发送到所有其他选项卡,但不是当前选项卡。

osascript \
    -e 'tell application "iTerm"' \
    -e " tell current window" \
    -e '  repeat with aTab in tabs' \
    -e '   tell aTab' \
    -e '    repeat with asession in sessions' \
    -e '     tell asession' \
    -e '      tell application "System Events" to keystroke "c" using {control down}' \
    -e '     end tell' \
    -e '    end repeat' \
    -e '   end tell' \
    -e '  end repeat' \
    -e ' end tell' \
    -e 'end tell'

我尝试用“如果 aTab 不等于当前选项卡”作为条件“告诉 aTab”,但仍然没有用。

以下示例 AppleScript 代码脚本编辑器中测试macOS CatalinaiTerm2 下,Build 3.3.12,并且在当前 window 有多个 选项卡 每个选项卡都有一个 session,除了 tab 执行时是 当前选项卡 ,停止了每个 中的 运行 process选项卡.

tell application "iTerm"
    activate
    tell current window
        set excludeTab to current tab
        select tab 1
        repeat with aTab in tabs
            select aTab
            if current tab is not equal to excludeTab then
                tell current session
                    tell application "System Events" to ¬
                        key code 8 using {control down}
                end tell
            end if
        end repeat
        select excludeTab
    end tell
end tell

备注:

与其在这样的 script 上使用 osascript -e command,我强烈建议创建一个可执行文件 file a #!/usr/bin/osascript shebang 然后复制并粘贴 example AppleScript code 到可执行文件 file 中。这个 IMO 比在 脚本 上使用 osascript -e command 更容易维护和修改,例如这样。


注意:示例 AppleScript code 就是这样,没有包含任何 错误处理 不包含任何额外的 错误处理 可能是适当的。用户有责任根据需要添加任何 错误处理 。查看 try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay 命令 在适当的事件之间可能是必要的,例如delay 0.5,适当设置延迟