Skip to content

Instantly share code, notes, and snippets.

@jkbrzt
Last active October 24, 2018 15:52
Show Gist options
  • Save jkbrzt/6018903 to your computer and use it in GitHub Desktop.
Save jkbrzt/6018903 to your computer and use it in GitHub Desktop.
#!/usr/bin/osascript
# vim: set syntax=applescript
(*
Launch multiple commands in iTerm2 split views
inspired by github.com/xgdlm/iterm2-cssh.
Usage:
$ iterm-multiple 'ls -la' 'command 2' 'command 3' #...
*)
on run argv
set commands to argv
-- Print help if no commands are given
if (count commands) = 0 then
log ("No commands to run.")
return
end if
set rightArrow to ASCII character 29
set numCommands to count commands
set numHorizontal to (round (numCommands ^ 0.5) rounding up)
set createdPanels to 1
tell application "iTerm"
activate
set myterm to (make new terminal)
tell myterm
launch session "Default Session"
-- create the horizontal panels
repeat numHorizontal - 1 times
tell i term application "System Events" to keystroke "d" using {command down}
set createdPanels to createdPanels + 1
end repeat
set splitColumns to 0
-- split each of the panels vertically
repeat createdPanels times
-- re-evaluate number of vertical panels we need
set numVertical to (round ((numCommands - createdPanels) / (numHorizontal - splitColumns)) rounding up) + 1
tell i term application "System Events" to keystroke rightArrow using {option down, command down}
repeat numVertical - 1 times
if createdPanels is less than numCommands then
log ("open vertical")
tell i term application "System Events" to keystroke "d" using {command down, shift down}
set createdPanels to createdPanels + 1
end if
end repeat
set splitColumns to splitColumns + 1
end repeat
-- run commands
repeat with loopIndex from 1 to numCommands
set currentCommand to item loopIndex of commands
tell item loopIndex of sessions to write text currentCommand
end repeat
-- introducing delay to avoid weirdness
-- delay 1
-- tell i term application "System Events" to keystroke "I" using {command down, shift down}
end tell
end tell
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment