Firstly, a couple of points about your AppleScript just for learning purposes:
- You don’t need to have
tell commands inside a tell block if they both send commands to the same object (i.e. Terminal). The tell block is sufficient; omit the tell statements inside it that address Terminal (but keep the one that addresses System Events), and the commands will be sent to Terminal.
- I wouldn’t System Events keystrokes to create new tabs. For one, shortcuts can change, or the app might lose focus for some reason and miss the keystroke event. In fact, if you don't target your
do script at Window 1, and instead leave it un-targetted, Terminal will go ahead and create a new tab of its own accord. So that eliminates the need for a couple of lines of code.
So, if all you needed to do was create a bunch of tabs at different locations, it would be as easy as:
-- Launch terminal without creating any windows
launch
-- Create a bunch of tabs at desired path locations
-- then clear the window to neaten its appearance
do script "cd ~; clear"
do script "cd /; clear"
do script "cd /Applications; clear"
(You can enclose all of that in a tell block targeting Terminal; I simply set Script Editor to do it for me in the navigation bar).
To set the titles of each tab—having created the tabs all at once—requires care to make sure we target the correct window object of each tab. These additional lines of code tack straight on after the ones above, and names the tabs "Tab 1", "Tab 2", etc. in the order by which they came into existence:
-- Get window ("tab") id's and sort numerically
-- with the help of bash functionality
set IDs to id of every window
set AppleScript's text item delimiters to " "
do shell script ({¬
"echo", ¬
IDs, ¬
"| tr ' ' '\n'", ¬
"| sort -n", ¬
"| tr '\n' ' '"} as string)
set IDs to the words of result
-- Give each tab a new custom title
repeat with n from 1 to number of windows
set custom title of tab 1 of ¬
window id (item n in IDs) to ¬
{"Tab", n} as string
end repeat
Window Groups
Secondly—and most importantly, as this actually answers your original question directly—there is no need for AppleScript at all to achieve what you want. Terminal has a built-in way to save a group of windows (tabs), then relaunch those windows each and every time you start Terminal. Here's how:
- Create the tabs you want.
cd each tab into the location you desire (or run any command in the tabs you wish).
- From the Window menu, choose Save Windows as Group.... In the dialog box that pops up, enter the name for this group and choose whether you want this set of windows to open each time you start Terminal.
- Now they will. You can save multiple groups of windows, and access them from the Window menu by choosing Open Window Group.
- To manage these saved groups, go to Preferences > Window Groups, where you can delete ones you don't want anymore.
The one downside to this is that the tab titles won't set automatically, although you can predefine what information each tab title contains in general under Preferences > Profiles > Window / Tab.