更新:较新的iTerm要求您更改语法,因此如下所示:
tell application "iTerm"
tell current window
create tab with default profile
end tell
tell current tab of current window
set _new_session to last item of sessions
end tell
tell _new_session
select
write text "cd \"$dir\""
end tell
end tell
另请参阅此答案。
对于较早的iTerm版本:
从我在这里的答案中获取脚本,您可以执行以下操作:
launch () {
for dir in ~/folderA{1..5}; do
/usr/bin/osascript <<-EOF
tell application "iTerm"
make new terminal
tell the current terminal
activate current session
launch session "Default Session"
tell the last session
write text "cd \"$dir\""
end tell
end tell
end tell
EOF
done
}
要解释发生了什么:
我们创建了一个名为的shell函数launch,因此您可以将~/.bash_profile其放在启动时要执行的任何位置。
我们遍历了Bash括号扩展的结果~/folderA{1..5},它使您~/folderA1通过~/folderA5。
我们通过调用iTerm2 AppleScript库osascript来创建一个新选项卡,将其激活,启动默认会话,并cd到达指定目录。