18

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.

slhck
  • 223,558
  • 70
  • 607
  • 592
cwd
  • 17,668
  • 42
  • 121
  • 159
  • 1
    Go to [their website](http://www.iterm2.com/#/section/home), click "[Documentation](http://www.iterm2.com/#/section/documentation)", then click "[Scripting](http://www.iterm2.com/#/section/documentation/scripting)". Or what do you mean by "scripting guide"? – Daniel Beck Jun 20 '11 at 06:31
  • If your trying this with iTerm2, the solution is posted here: https://stackoverflow.com/questions/38692346/new-tab-in-iterm2#_=_ – Andy Cochrane Apr 25 '17 at 02:57

4 Answers4

15

The following does almost what you asked for, but it opens a new window.

tell application "iTerm"
    set newWindow to (create window with default profile)
    tell current session of newWindow
        write text "cd ~/Desktop; clear; pwd"
    end tell
end tell

(This was based on the new iTerm documentation and the below answer, which you should upvote!)

slhck
  • 223,558
  • 70
  • 607
  • 592
  • write text adds the return/newline on its own? – Daniel Beck Jun 20 '11 at 09: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 09: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: http://code.google.com/p/iterm2/wiki/AppleScript – cwd Jun 21 '11 at 04:16
  • I believe exec actually runs [`exec(3)`](http://linux.die.net/man/3/exec) and so only works in a new session, and runs the exec:ed process instead of a shell. – Henrik N Sep 05 '12 at 13:44
  • @slhck I'm having a hell of a time formatting this to be able to execute with `osascript -e` in the shell. Any advice? – Ken Jun 22 '14 at 16:26
  • 1
    @slhck nvm, found this little gem with EOD usage:http://apple.stackexchange.com/questions/103621/run-applescript-from-bash-script – Ken Jun 22 '14 at 16:30
  • This doesn't work anymore. `The variable terminal is not defined` – duplex143 May 28 '22 at 17:34
5

If anyone in 2020 looking for answer to this, find below:

tell application "iTerm"
    set newWindow to (create window with default profile)
    tell current session of newWindow
        write text "cd ~/Desktop; clear; pwd"
    end tell
end tell
Jogendra Kumar
  • 151
  • 1
  • 3
0

I have try this script, and it works for me. Reference: https://iterm2.com/documentation-scripting.html

tell application "iTerm2"
  tell current window
    set newTab to (create tab with default profile)
    tell current session of newTab
      write text "cd ~/Documents; pwd"
    end tell
  end tell
end tell
Lei
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 25 '23 at 08:58
0

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
Daniel Beck
  • 109,300
  • 14
  • 287
  • 334