Quick — check out the new and improved version of this project on Github, and read about it in my new blog post!

Opening iTerm From a Finder Directory

If, like me, you work mostly from OS X, you've probably already discovered iTerm2. If you haven't: it's an incredibly good replacement for the default Terminal application. Combined with the solarized color scheme, it does what all software should: it fades away and lets me get my work done without an issue.

If you're like me in another way, you will occasionally use the Finder to browse through your files. The problem is that it's difficult to open an iTerm window set to the current Finder directory.

A couple of different people have come up with solutions. Simon Dorfman came up with an Applescript to keep in the Finder sidebar or your dock; it opens iTerm tabs in any directory dropped on it. Ben Alman wrote a similar script that does the same thing. But both of these require you to drag and drop an item onto a script that takes up space in your sidebar or dock.

I didn't want to clutter up my dock or Finder sidebar with a droppable script, so I adapted Lance E Sloan's script and turned it into a Finder service that you can access by right-clicking on a folder. Here's the script:

on run {input, parameters}
  tell application "Finder"
    set dir_path to quoted form of (POSIX path of (input as alias))
  end tell
  CD_to(dir_path)
end run

on CD_to(theDir)
  tell application "iTerm"
    activate

    try
      set t to the last terminal
    on error
      set t to (make new terminal)
    end try

    tell t
      launch session "Default Session"
      tell the last session
        write text "cd " & theDir & ";clear;"
      end tell
    end tell
  end tell
end CD_to

Using Automator, create a new "Service" that accepts folders in Finder.

setting up the service

Add a "Run Applescript" action and paste in the above code. Save it as whatever you'd like - I saved mine as open_iterm. Now, you can just right-click any folder from within Finder to open an iTerm tab there. Cool.

the service in action

Update – June 24, 2013

I just got a great email from Eric Hu describing a modified version of this script that can be run on files, not just folders. Check it out here — thanks, Eric!

Update – May 15, 2014

John Kokkinidis was kind enough to write in with a solution I like even more than the Finder service presented above. His version is designed to be run with a single click from the Finder, once you've navigated to the folder that you'd like to open. If you take his script:

on run {input, parameters}
  tell application "Finder"
    set dir_path to quoted form of (POSIX path of (folder of the front window as alias))
  end tell
  CD_to(dir_path)
end run

on CD_to(theDir)
  tell application "iTerm"
    activate

    try
      set sesh to current session of current terminal
    on error
      set term to (make new terminal)
      tell term
        launch session "Default"
        set sesh to current session
      end tell
    end try

    tell sesh
      write text "cd " & theDir & ";clear;"
    end tell
  end tell
end CD_to

create an "Application" in Automator:

setting up the application

and drag it onto the Finder window while holding the command key (in Yosemite, the command and option keys. Thanks Peter!)

installing the application

You can then click that icon any time you're in the Finder to get a new iTerm shell at that location. Thanks, John! Also, thanks to Eryan Cobham and Adam Mclain for both writing in with the same suggestion: to use quoted form of (POSIX path of ...) instead of POSIX path of ..., allowing the script to work with directories including spaces. And a big thanks to Peter Scott for writing in to suggest a fix for a problem where two windows were being created. He points to CtWise's efforts here as his inspiration.