1

Is there any way to open Terminal with predefined tabs? I have 5 most common paths that I usually use and I want Terminal to open with these paths opened in different Tabs. I guess this should be AppleScript, but I don't have enough expertise to write it. Can you help me?

Thank you.

1

nm, this is the answer:

tell application "Terminal"
    activate
    do script "cd %path1%"
    tell application "Terminal" to set custom title of tab 1 of front window to "Name 1"
    tell application "System Events" to keystroke "t" using command down
    delay 0.05
    do script "cd %path2%" in window 1
    tell application "Terminal" to set custom title of tab 1 of front window to "Name 2"
end tell

This will open Terminal with Path1 and Tab Name 1 and one more tab with Path2 and Tab Name 2

0

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:

  1. Create the tabs you want. cd each tab into the location you desire (or run any command in the tabs you wish).
  2. 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.
  3. Now they will. You can save multiple groups of windows, and access them from the Window menu by choosing Open Window Group.
  4. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.