Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In OS X, how do I set up an AppleScript to

  • open a new iTerm2 tab
  • change to a directory
  • clear the console
  • echo the current directory

I had something like this before for regular Terminal, but I can't even find the scripting guide for iTerm2.

share|improve this question
1  
Go to their website, click "Documentation", then click "Scripting". Or what do you mean by "scripting guide"? –  Daniel Beck Jun 20 '11 at 6:31

2 Answers 2

up vote 10 down vote accepted

Daniel's solution somehow opens a new Window – also, the exec command statement does not work as expected. One has to write text instead.

Also, you have to use

launch session "Default Session" 

in order to get a new tab.

The following does what you asked for:

tell application "iTerm"
    make new terminal
    tell the current terminal
        activate current session
        launch session "Default Session"
        tell the last session
            write text "cd ~/Downloads; clear; pwd"
        end tell
    end tell
end tell
share|improve this answer
    
write text adds the return/newline on its own? –  Daniel Beck Jun 20 '11 at 9:06
    
Apparently, it does! I tried everything before posting. exec command does something, but I don't know what exactly. –  slhck Jun 20 '11 at 9:19
    
Thanks for a nice answer. I also found the comments at the bottom of this page to be helpful in writing a "cd to" script: code.google.com/p/iterm2/wiki/AppleScript –  cwd Jun 21 '11 at 4:16
    
I believe exec actually runs exec(3) and so only works in a new session, and runs the exec:ed process instead of a shell. –  Henrik N Sep 5 '12 at 13:44
1  
@slhck nvm, found this little gem with EOD usage:apple.stackexchange.com/questions/103621/… –  Ken Jun 22 at 16:30

Not on a Mac right now, so it might not work 100% (adapted this answer of mine).

tell application "iTerm"
    activate
    set t to (make new terminal)
    tell t
        tell (make new session at the end of sessions)
            exec command "cd Downloads"
            exec command "clear"
            exec command "pwd"
        end tell
    end tell
end tell

You can probably concatenate the commands to

cd Downloads ; clear ; pwd
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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