19

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
  • 235,242
cwd
  • 18,788
  • 43
  • 123
  • 162

4 Answers4

16

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
  • 235,242
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
1

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
  • 11
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
  • 111,893