This is a little bit hacky, but you can achieve this in AppleScript. If there is a predetermined number of tabs you want, and preset commands you wish to run, this isn't difficult.
tell application "Terminal"
-- Activate it.
activate
set targetWindow to 0
-- Get a window that's not busy.
-- If there are no open windows, open one.
if count of windows is greater than 0 then
repeat with currentWindow in windows
if currentWindow is not busy then
set targetWindow to currentWindow
end if
end repeat
else
do script ""
set targetWindow to window 1
end if
-- Do command 1.
set firstCommand to "cd ~/Desktop; clear"
do script firstCommand in targetWindow
-- Open a new tab.
tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
if count of windows is greater than 0 then
repeat with currentWindow in windows
if currentWindow is not busy then
set targetWindow to currentWindow
end if
end repeat
else
do script ""
set targetWindow to window 1
end if
-- Do command 2.
set secondCommand to "cd /Applications; clear"
do script secondCommand in targetWindow
-- And so on...
end tell
Of course, replace firstCommand
with whichever command you actually want to run, and so on. For whatever reason, Terminal doesn't really have an accessible way to create new tabs through AppleScript, so the long, hacky looking line in the middle just tells Terminal to type ⌘T to open that new tab, and then new commands will execute in it.
You can run this AppleScript as is, or use it in Automator to create a new service, which you can then execute from anywhere using a keyboard shortcut if you'd like.
Extras - If you want to fire some script/command in the newly opened terminal session, you can refer this
iTerm 2 is a powerful replacement for Terminal.app and it has support for AppleScript. You can use AppleScript to tell it to run commands in a new tab or window.
For example, let's make a little AppleScript action that's callable. Enter the following in to the AppleScript Editor:
on run argv
set AppleScript's text item delimiters to {" "}
tell application "iTerm"
make new terminal
tell the current terminal
activate current session
launch session "Default Session"
tell the last session
write text argv as string
end tell
end tell
end tell
end run
and then save it as a Script in ~/Documents/Scripts/run-in-new-iterm-tab.scpt
. We can now call it from the command line like so:
/Users/ian
> osascript ~/Documents/Scripts/run-in-new-iterm-tab.scpt ls ~/Documents/Scripts
/Users/ian
>
and I get a new tab in my iTerm session with:
Last login: Thu Aug 28 22:17:17 on ttys003
ls /Users/ian/Documents/Scripts
/Users/ian
> ls /Users/ian/Documents/Scripts
edit keep_drives_alive.sh run-in-new-iterm-tab.scpt touch-in-order.pl
find-missing-itunes-files.pl keep_drives_alive.sh~ start-utorrent.scpt touch-in-order.pl~
/Users/ian
>
Which is not too bad. There are some refinements you can make to the AppleScript like maintaining the current working directory when you call the command. But that's gets you in the ballpark.
If you use this hint in this question you can turn the whole thing in to a bash-wrapped script that's even easier to execute:
#!/usr/bin/env osascript
on run argv
set AppleScript's text item delimiters to {" "}
tell application "iTerm"
make new terminal
tell the current terminal
activate current session
launch session "Default Session"
tell the last session
write text argv as string
end tell
end tell
end tell
end run
Save that to a spot on your PATH
and make it executable and then you only need to run:
run-in-new-iterm-tab ls ~/Documents/Scripts
to get it to work.
Best Answer
Add this to your
~/.bash_profile
if you use bash or~/.zshrc
if you use zsh, changing the content of theCOMMANDS
array:Now calling
openterms
in a shell will open all the other shells you wanted.