Skip to content

Instantly share code, notes, and snippets.

@Lessica
Created July 17, 2021 11:52
Show Gist options
  • Save Lessica/dcd7a3beca899a080e5135ce1957772b to your computer and use it in GitHub Desktop.
Save Lessica/dcd7a3beca899a080e5135ce1957772b to your computer and use it in GitHub Desktop.
Make muliple panes in iTerm
#!/bin/zsh
repeatBy() { for i in {1..$(expr $1)}; do echo -n "$2"; done }
if [ "$#" -lt 2 ]; then
echo "usage: $0 v h [command]"
exit 1
fi
CMD="$3"
VER_CNT=$(expr $1 - 1)
VER_SPLIT=$(repeatBy $VER_CNT ' delay 0.3\n keystroke "d" using command down\n')
VER_PREV=$(repeatBy $VER_CNT ' delay 0.1\n keystroke "[" using command down\n')
HOR_CNT=$(expr $2)
HOR_ELEM=" delay 0.3\\\\\\\\n keystroke textToType\\\\\\\\n key code 36\\\\\\\\n"
HOR_ELEMS=$(repeatBy $HOR_CNT "\"$HOR_ELEM\"---")
HOR_SET=$(echo $HOR_ELEMS | sed "s/\"---\"/ keystroke \"D\" using command down\n/g" | sed "s/\\\\n/\\n/g")
HOR_ALL="${HOR_SET: 1:-5}"
ALL_CNT=$(expr $1)
VER_ELEMS=$(repeatBy $ALL_CNT "\"$HOR_ALL\"---")
VER_SET=$(echo $VER_ELEMS | sed "s/\"---\"/\n\n delay 0.3\n keystroke \"]\" using command down\n\n/g" | sed "s/\\\\n/\\n/g")
VER_ALL="${VER_SET: 1:-4}"
/usr/bin/env osascript <<-EOF
tell application "iTerm"
activate
launch session "Panes"
tell application "System Events"
tell process "iTerm"
set textToType to "$CMD"
keystroke "t" using command down
$VER_SPLIT
$VER_PREV
$VER_ALL
end tell
end tell
end tell
EOF
@Lessica
Copy link
Author

Lessica commented Jul 17, 2021

Usage

./make_panes.sh 2 4  # or
./make_panes.sh 3 3  # or
./make_panes.sh 4 3 "echo 1"
# ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment