Agile Zone is brought to you in partnership with:

Michael Norton (doc) is Director of Engineering for Groupon in Chicago, IL. Michael's experience covers a wide range of development topics. Michael declares expertise in no single language or methodology and is immediately suspicious of anyone who declares such expertise. Michael is a DZone MVB and is not an employee of DZone and has posted 41 posts at DZone. You can read more from them at their website. View Full User Profile

Open iTerm Here

02.28.2011
| 3561 views |
  • submit to reddit

I've been using iTerm for over a year now. I like it over Terminal for a few reasons, but primarily because it supports multiple frames. This makes my workflow easier as I can see everything on a single screen and can easily navigate from one frame to the other without reaching for the mouse. I've additionally abandoned MacVim for Vim. Now, I can do most everything I need in a single, maximized, iTerm window. But I get annoyed with opening iTerm and having to navigate to the directory of my current desire. Most of the time, I am already browsing the desired directory in Finder as I begin a hacking session.

What is a guy to do?

AppleScript to the rescue

I created an AppleScript app based on work from a couple of other folks, whom I've given attribution. The AppleScript launches iTerm and changes to whatever directory was active in Finder. If iTerm is running, the script opens a new terminal. If iTerm is not running, the script launches it and changes directories accordingly.


Screen shot 2011-02-26 at 3.24.41 PM

(*
iTerm Here - Michael "Doc" Norton

Version 0.1.1

A Button for opening the iTerm.app and CD-ing to the location of the selected files or folder.
Inspired by the workflow from Jeroen Leenars, but using iTerm instead of Terminal.
Additional contributions to concept (and code) from:

Felipe Meneguzzi
See http://fmeneguzzi.blogspot.com/2009/09/open-iterm-here.html for more information.

Marc Liyanage
See http://putnamhill.net/applescript/open-iterm-here for more information.
*)

on run
tell application "Finder"
try
set this_folder to (the target of the front window) as alias
on error
set this_folder to startup disk
end try
my open_item(this_folder)
end tell
end run

on open these_items
repeat with i from 1 to the count of these_items
set this_item to item i of these_items
my open_item(this_item)
end repeat
end open

on open_item(this_path)
set thePath to (quoted form of POSIX path of this_path)
set iTermRunning to isAppRunning("iTerm")

tell application "iTerm"
if iTermRunning then
set myTerm to (make new terminal)
tell myTerm to launch session "Default Session"
else
activate
set myTerm to first terminal
tell myTerm to activate current session
end if
tell myTerm
tell the last session
write text "cd " & thePath & "; clear"
end tell
end tell
end tell
end open_item

on isAppRunning(app_name)
tell application "System Events" to set app_list to every application process whose name is app_name
if the (count of app_list) > 0 then
return true
else
return false
end if
end isAppRunning

You can use the script for yourself.
  1. Paste it into the Script Editor application and save it as an application.
  2. In Finder, navigate to the application and drag it to the Finder bar.
  3. You can now click the button and it will open the foremost directory in Finder.
  4. Or you can drag and drop folders onto the icon.
  5. Locate iTerm and Select the More Info... button.
  6. Locate your new app and Select the More Info... button.
  7. Copy the small icon from the top-left of the iTem Info window.
  8. Paste it onto the small icon of your app's Info Window.
References
Published at DZone with permission of Michael Norton, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)