How to use AppleScript to launch three scripts, each in new Terminal tab?

I'm trying to launch 3 tabs and run a script in each with AppleScript. So far I haven't been able to get this done. Currently I have:

tell application "Terminal"
    activate
    do script "ping google.com"
    tell application "System Events"
        keystroke "t" using {command down}
        do script "ping yahoo.com"
    end tell
    tell application "System Events"
        keystroke "t" using {command down}
        do script "ping msn.com"
    end tell
end tell

This will launch three tabs but the pings don't execute in the last two tabs.

I'm probably missing something basic here.


Solution 1:

  1. "System Events" doesn't understand what you want to do with do script, use the do script command outside of the tell application "System Events" block.

  2. You must specify a window or tab when using the do script command, otherwise a new window will open.

tell application "Terminal"
    activate
    do script "ping google.com" in front window
    my makeTab()
    do script "ping yahoo.com" in front window
    my makeTab()
    do script "ping msn.com" in front window
end tell

on makeTab()
    tell application "System Events" to keystroke "t" using {command down}
    delay 0.2
end makeTab