我有以下脚本尝试垂直拆分窗口,并在新窗格中拖尾日志文件。
#!/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
但是,此脚本会拆分当前处于焦点的窗口,而不是运行脚本的窗口。
重现问题的步骤:
sleep 4
时,打开另一个窗口(比如W2)并保持W2对焦。如何从W1打开窗口,从W1调用脚本的窗口?
答案 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)