Open iTerm2 Terminal At Current Finder Path Via AppleScript
December 5, 2012
In a previous post I mentioned iTerm2 as a Mac OS X terminal replacement. Hopefully you’ve taken the time to check it out. If so, and you appreciate all it has to offer, the next step is to find a means to easily open iTerm2.
I have a number of scripts (Applescript code) that I have written and saved as applications. I then add the application to the toolbar in Finder. My toolbar looks as follows, all of the icons in the green box I’ve added:
Let me walk you through the steps to create a script that will open a new iTerm2 window and change to the current Finder path. I’ll also show you how to create an icon for the application so it is easy to recognize in Finder.
First, open up the AppleScript Editor and copy/paste the code below (code courtesy Stefan van den Oord):
-- Stefan van den Oord, 2010-12-29 -- The "cd to" command for iTerm2 tell application "Finder" set _cwd to POSIX path of ((folder of (front window)) as alias) end tell tell application "iTerm2" activate try set _session to current session of current terminal on error set _term to (make new terminal) tell _term launch session "Default" set _session to current session end tell end try tell _session write text "pushd \"" & _cwd & "\"" end tell end tell
- Save the file as an application.
Before we drag/drop the application onto Finder, let’s create an icon for the application.
- Go to Iterm2 and use the Mac copy to clipboard feature (shift-command-4) to copy the logo.
- Open the screenshot in Preview, mark the image (control-a) and copy to clipboard (control-c).
- Find the application you saved above, right click and select Get Info.
- Tap the icon in the upper left corner and paste the saved image (control-v).
You now have Applescript that has been saved as an application, with a shiny new icon.
The final step is to drag the application onto the Finder toolbar.
You are good to go, iTerm2 at the current Finder path, one click away.