iTerm2 v3 supports significantly improved support for AppleScript, so now you can create tabs directly without sending keystrokes:
tab() {
osascript &>/dev/null <<EOF
tell application "iTerm"
activate
tell current window to set tb to create tab with default profile
tell current session of current window to write text "pwd"
end tell
EOF
}
To split the new tab horizontally (as you would by clicking โงโD), add:
tell current session of current window to split horizontally with same profile
To write pwdto a new session created by a section (lower half of a new tab):
tab() {
osascript &>/dev/null <<EOF
tell application "iTerm"
activate
tell current window to set tb to create tab with default profile
tell current session of current window to set newSplit to split horizontally with same profile
tell newSplit
select
write text "pwd"
end tell
end tell
EOF
}
iTMP2 AppleScript Script Editor.app, File > Open Dictionary..., iTerm.app.
ttab CLI, / Terminal.app iTerm2.app ( ).