Skip to content

Instantly share code, notes, and snippets.

@DocOnDev
Created February 26, 2011 19:38
Show Gist options
  • Save DocOnDev/845541 to your computer and use it in GitHub Desktop.
Save DocOnDev/845541 to your computer and use it in GitHub Desktop.
Open iTerm Here
(*
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
@iMacThere4iAm
Copy link

You can change the first part to make it work in search folders such as Lion's "All My Files" :

on run
    tell application "Finder"
        try
            set this_folder to (the target of the front window) as alias
        on error
            try
                set this_folder to container of (selection as alias) as alias
            on error
                set this_folder to path to home folder as alias
            end try
        end try
        my open_item(this_folder)
    end tell
end run

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