Applescript to Duplicate Terminal Tab

From my personal experience, I realized that I’d always need at least two terminal tabs whenever I do any Rails development: one to run the server ($ ./script/server) and one to do the rest of the rails stuff (generate things, run rake, etc). Pressing Cmd-t in Terminal will open a new tab, but that new tab would always start at my home directory.

Now I can first navigate to the folder I wanted to got to in Terminal, then this script will open a new tab in the foremost window of Terminal, then “cd” into the directory of the previous tab.

tell application "Terminal"
    activate
    tell front window
        set cur_path to do script "osascript \
            -e 'tell application \"Terminal\"' \
            -e 'tell application \"System Events\" \
            to tell process \"Terminal\" to \
            keystroke \"t\" using command down' \
            -e \"do script with \
            command \\\"cd `pwd`;clear\\\" \
            in selected tab of the front window\" \
            -e 'end tell'" in selected tab
    end tell
end tell

Post a Comment