{{ !articles[0].partner.isSponsoringArticle ? "Platinum" : "Portal" }} Partner

Open iTerm Here

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"
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"
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
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.

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks