Split window programmatically with iTerm?
Solution 1
You can easily call this from iTerm2 directly to simulate pressing ⌘D:
osascript -e 'tell application "System Events" to key code 2 using command down'
For this to work you want to start the programs in background, since otherwise you cannot run osascript
:
some-command &
osascript -e '…'
From there on you'll land in a new iTerm2 window, so you need to use the write text
option in AppleScript to run further shell commands. See here for more: How do I set up an AppleScript to open a new iTerm2 tab and change the directory?
Solution 2
the answer here is a bit outdated.. here is an example script that does somethign similar:
tell application "iTerm"
tell current window
-- create a tab for background db stuff
create tab with default profile
tell current session
write text "mongod &"
write text "redis-server &"
end tell
close current tab
-- create tab to run aioc server
create tab with default profile
tell current session
write text "title server"
write text "aactivate"
write text "arunserver"
-- split tab vertically to run scheduler
split vertically with default profile
end tell
-- run scheduler
tell last session of last tab
write text "title scheduler"
write text "aactivate"
write text "ascheduler"
-- split tab vertically to run main controller
split vertically with default profile
end tell
-- run main_controller
tell last session of last tab
write text "title main_controller"
write text "aactivate"
write text "amain_controller"
-- split tab vertically to run aggregator
split vertically with default profile
end tell
tell last session of last tab
write text "title aggregator"
write text "aactivate"
write text "aggregator"
end tell
end tell
end tell
Related videos on Youtube
Tallboy
Updated on September 18, 2022Comments
-
Tallboy over 1 year
on my development workstation I often have to start the same commands every morning.
zeus start
,zeus server
(booting rails via zeus),redis-server
, and like 3 others.I know a lot of people would say have them running all the time but I do a lot of work on my own stuff and it wigs me out having all of those running while I'm working on separate rails projects.
Is there some kind of advanced alias I can make which starts all of these from one command, ideally by programmatically splitting the window (like
⌘-D
).I'm using iTerm2 with oh-my-zsh.
I wouldnt mind if they were all in the same window I suppose (somehow running as background processes) however I do need to sometimes look at the output, and work with the output from each command, so I'm not sure how that would work.
Thanks!
-
Tallboy over 10 yearsWow that is great! How would I go about combining this into my script. I tried with && and it didn't work, but I probably wrote it wrong.
-
slhck over 10 yearsI tried running a script with just that command, starting it from iTerm 2 (from within Zsh, same setup as you), and it worked without problems. Make sure that iTerm 2 is active while the command is run. You can also try inserting
tell application "iTerm" to activate;
before the othertell
command just to make sure. -
Tallboy over 10 yearsSorry, I should have clarified. The script itself works perfect, but I suppose I'm not sure how to combine it into one large zsh script that basically does
redis-server -> split -> other command -> split -> etc
-
slhck over 10 yearsAh, I see what you mean. Since you are in a new window (or split panel) after running the first
osascript
, you need to execute further commands withosascript
– I added an example to my answer. -
Nick over 6 yearsdid any of the commands change recently with updates to iterm and High Sierra? Nothing is happening when I try to communicate with the last tab. Iterm Build 3.1.5
-
Nick over 6 yearsActually I need to execute commands from the returned referenced session from
split vertically with default profile
. That way I can interact with the session that got split. -
Nick over 6 yearsthis worked ``` #!/bin/sh tailBuildLog(){ termCMD=$(cat <<-END tell application "iTerm" tell current window local newSession tell current session split vertically with default profile command "tail -f ./buildLogs/$1" end tell end tell end tell END ) osascript -e "$termCMD" #echo "$termCMD" #echo } ```