Applescript:iTerm从调用脚本的窗口中拆分窗格

时间:2016-08-29 21:28:53

标签: bash applescript iterm2 iterm osascript

我有以下脚本尝试垂直拆分窗口,并在新窗格中拖尾日志文件。

#!/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

但是,此脚本会拆分当前处于焦点的窗口,而不是运行脚本的窗口。

重现问题的步骤:

  1. 打开iTerm窗口(比如W1),然后运行此脚本。
  2. 当脚本执行sleep 4时,打开另一个窗口(比如W2)并保持W2对焦。
  3. 4秒后,较新的窗口(即W2)将垂直分割。
  4. 如何从W1打开窗口,从W1调用脚本的窗口?

1 个答案:

答案 0 :(得分:1)

在脚本开头获取当前会话的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)