I have the following script with attempts to split the window vertically, and tail a log file in the new pane.
#!/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
However, this script splits the window that is currently in focus, and not the window in which the script is running.
Steps to reproduce the issue:
sleep 4
, open another window (say W2) and keep W2 in focus.How to open split the window from W1, the window where the script was called from?
Get the ID of the current session at beginning of the script.
Later in the script, get the session corresponding to this identifier
#!/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)