Applescript:iTerm 从 window 脚本中拆分窗格

Applescript: iTerm to split pane from the window the script was called

我有以下脚本,尝试垂直拆分 window,并在新窗格中添加一个日志文件。

#!/bin/bash

COMMAND="tail -f log_file"

# Do something important.
sleep 4

# Split the window, and tail logs.
osascript <<-EOF
tell application "iTerm"
    tell current session of current window
        split vertically with default profile command "$COMMAND"
    end tell
end tell
EOF

但是,此脚本拆分了当前处于焦点的 window,而不是脚本 运行ning 所在的 window。

重现问题的步骤:

  1. 打开一个 iTerm window(比如 W1),然后 运行 这个脚本。
  2. 当脚本正在执行时 sleep 4,打开另一个 window(比如 W2)并保持 W2 为焦点。
  3. 4 秒后,较新的 window(即 W2)将垂直拆分。

如何打开从 W1 中拆分 window,即调用脚本的 window?

在脚本开头获取当前会话的 ID。

稍后在脚本中,获取与此标识符对应的会话

#!/bin/bash
myID=$(osascript -e 'tell application "iTerm" to id of current session of current window')

COMMAND="tail -f log_file"

# Do something important.
sleep 4

# Split the window, and tail logs. myID2 is the id of the new session (the split pane)
myID2=$(osascript <<-EOF
tell application "iTerm"
    repeat with w in windows
        repeat with t in tabs of w
            tell (first session of t whose its id = "$myID")
                if exists then return id of (split vertically with default profile command "$COMMAND")
            end tell
        end repeat
    end repeat
end tell
EOF)