Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created April 6, 2011 12:10
Show Gist options
  • Save cowboy/905546 to your computer and use it in GitHub Desktop.
Save cowboy/905546 to your computer and use it in GitHub Desktop.
Finder Open iTerm Here: Drop a folder on this script (or just run it) to open a new iTerm window there. If the selected item isn't a folder, it will open the item's parent folder. Great when bound to a hotkey in FastScripts or added to the Finder Sidebar
(*
* Finder Open iTerm Here - v1.0.2 - 4/14/2011
* http://benalman.com/
*
* Copyright (c) 2011 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*)
tell application "Finder"
set mySelected to get selection as list
if (count of mySelected) is not 0 then
set myItem to first item of mySelected
if class of myItem is alias file then
set myItem to original item of myItem
end if
if class of myItem is in {file, document file, internet location file} then
set myItem to container of myItem
end if
else if the (count of window) is not 0 then
set myItem to folder of the front window
else
set myItem to path to desktop folder
end if
my open_iTerm(myItem)
end tell
on open these_items
my open_iTerm(first item of these_items)
end open
on open_iTerm(myItem)
set myPath to quoted form of POSIX path of (myItem as string)
tell application "iTerm"
activate
if the (count of terminal) is 0 then
set myTerm to make new terminal
else
set myTerm to the current terminal
end if
tell myTerm
launch session "Default Session"
tell the last session to write text "cd " & myPath
end tell
end tell
end open_iTerm
@mathiasbynens
Copy link

Thanks!

I love how it opens a new iTerm tab instead of spawning multiple windows (like some other similar scripts do).

I’m using this in the Finder Toolbar now. (Took me a while to figure out you have to open the .scpt file in Xcode and save it as an .app first.)

@cowboy
Copy link
Author

cowboy commented Apr 7, 2011

1.0.1 - Fixed a bug where it would fail on directories with spaces in their name. Man, I want QUnit for AppleScript!

@mraaroncruz
Copy link

@mathiasbynens You can save it in the applscript editor as an Application
Any idea on how to get an icon for this?

@mraaroncruz
Copy link

Figured it out. Just copied the .icns file from this project http://code.google.com/p/cdto/ to the Contents/Resources and renamed it to what the old icon was named.
Thanks for this!

@mathiasbynens
Copy link

@pferdefleisch Here’s how you can change any application/file/folder’s icon on OS X: http://mathiasbynens.be/notes/shell-script-mac-apps#icon

@mraaroncruz
Copy link

Thanks man. I was inspired by this and created an "Open in Vim" finder link. I used Icon Composer.
Here's my finder bar now.

finder
and a gist to the Vim opener
This is for mvim in vi mode in iTerm. I like to be able to drop down to the command line super fast (hence not mvim in window mode).
Thanks again @cowboy and @mathiasbynens

@mraaroncruz
Copy link

I'll totally start a repo of these if i have time this week!

@aaronsnoswell
Copy link

I love this, but for the fact that the newly created iTerm tab/window shows the "cd" command - I'd much prefer it if the iTerm window was created already in the specified directory. I've tried modifying the AppleScript to do this, but can't figure it out. Any pointers? Clearing the screen is one option, but then you don't get the MOTD / log in prompt...

Update: The exec command documented on the iTerm2 FAQ Page seems like the right thing, but I can't seem to get it to work. Eg, if I change line 44 to read tell the last session to exec command ("cd " & myPath) nothing happens. Anyone able to shed some light on this?

@aaronsnoswell
Copy link

Oh, I also added

    tell application "System Events"
        key code 111 # press F12
    end tell

at line 46 to press F12 and show my guake-style terminal :)

@pystone
Copy link

pystone commented May 10, 2013

There is no need to use Xcode. AppleScript Editor.app can easily turn the codes to .app.

@tekwiz
Copy link

tekwiz commented Jun 25, 2013

@cowboy Can you tell me if this script was originally written for OS X 10.7 beta compatibility, or were you running 10.6? (trying to figure out compatibility of this code in the Sublime Terminal plugin for Sublime Text

@tucq88
Copy link

tucq88 commented Aug 28, 2015

If I installed iTerm via Homebrew Cask, this script doesn't work to me :( Anybody help please ?

@inderpreet99
Copy link

@tucq88 I forked this to make it compatible with iTerm2-v3 (version number 2.9). Link: https://gist.github.com/inderpreet99/fe8db408780bd1a0350d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment