Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I think the title states exactly what I want to do. I want a shortcut or even a button within Finder which fires up a new iTerm Tab and changes the location to the location i've open in Finder. Some sort of open . in reverse. :-)

Thank you, Malax

share|improve this question

7 Answers 7

up vote 5 down vote accepted

There is an Open Terminal Here AppleScript that you should be able to modify to call iTerm instead. This MacOSXHints post should be helpful as well.

(I'm not on my Mac otherwise I would test it.)

share|improve this answer
    
Thank you a lot! –  Malax Jan 8 '10 at 20:23

This applescript works for me:

-- script was opened by click in toolbar
on run
tell application "Finder"
    try
        set currFolder to (folder of the front window as string)
    on error
        set currFolder to (path to desktop folder as string)
    end try
end tell
CD_to(currFolder, false)
end run

-- script run by draging file/folder to icon
on open (theList)
set newWindow to false
repeat with thePath in theList
    set thePath to thePath as string
    if not (thePath ends with ":") then
        set x to the offset of ":" in (the reverse of every character of thePath) as string
        set thePath to (characters 1 thru -(x) of thePath) as string
    end if
    CD_to(thePath, newWindow)
    set newWindow to true -- create window for any other files/folders
end repeat
return
end open

-- cd to the desired directory in iterm
on CD_to(theDir, newWindow)
set theDir to quoted form of POSIX path of theDir as string
tell application "iTerm"
    activate
    delay 1
    -- talk to the first terminal 
    try
        set myterm to the first terminal
    on error
        set myterm to (make new terminal)
    end try

    tell myterm
        try
            -- launch a default shell in a new tab in the same terminal 
            launch session "Default Session"
        on error
            display dialog "There was an error creating a new tab in iTerm." buttons {"OK"}
        end try
        tell the last session
            try
                -- cd to the finder window
                write text "cd " & theDir
            on error
                display dialog "There was an error cding to the finder window." buttons {"OK"}
            end try
        end tell
    end tell
end tell
end CD_to
share|improve this answer
1  
I think this should be the accepted answer. –  dhilipsiva Jul 6 '13 at 14:33

Using the other answers on this page I have created an App that can be dragged into the finder task bar.

You can download it from here: https://github.com/rc1/iTermTo

share|improve this answer
1  
Excellent job! Works perfectly. This should be the accepted answer. –  rcd Jun 4 '13 at 20:19
1  
I concur -- seems to work perfectly. Download zip. Drag application to Applications folder to install. Drag application to finder toolbar for the handy shortcut. –  justingordon Jun 5 '13 at 5:51

Take a look at the cdto project hosted on code.google.com "Fast mini application that opens a Terminal.app window cd'd to the front most finder window. This app is designed (including it's icon) to placed in the finder window's toolbar." Also has a plugin to work with iTerm.

share|improve this answer
    
But it opens two iTerms windows which is highly annoying after a while. –  Mike Lischke May 22 '13 at 17:08

Just for completeness, before finding this question what worked for me was:

  • adapted new_tab.sh (AppleScript issued by bash script) to an AppleScript-only solution.
  • then from the Applescript Editor-> File-> Export-> File Format = .app.
  • drag & dropped the .app to Finder's toolbar.

This results in a Finder toolbar button which opens the current directory in new iTerm2 tab. XtraFinder offers such a button, but it opens new windows.

A similar solution using services can be found here, which links to even more related AppleScript solutions:

My adapted AppleScript is:

try
    tell application "iTerm2"
        tell the last terminal
            launch session "Default Session"
            tell the last session
                tell i term application "Finder"
                    set cur_dir to (the target of the front Finder window) as string
                end tell
                set cur_dir to POSIX path of cur_dir
                write text "cd " & cur_dir
            end tell
        end tell
     end tell
end try

This solution was commented in this button-related thread.

Thanks to the iTermTo answer above.

share|improve this answer

With iTerm:

Iterm Preferences and Under Profiles tab, go to General subtab, set Working Directory to “Reuse previous session’s directory”.

share|improve this answer

Here is a simplified script that always opens a new tab (like bulljit's script):

try
    tell application "Finder"
        if number of Finder windows is 0 then
            set p to POSIX path of (desktop as alias)
        else
            set p to POSIX path of (target of Finder window 1 as alias)
        end if
    end tell
    tell application "iTerm"
        reopen
        tell current terminal
            tell (launch session "Default Session")
                write text "cd " & quoted form of p
            end tell
        end tell
        activate
    end tell
end try

If you want the script to reuse existing tabs, replace the tell current terminal block with something like this:

tell current session of current terminal
    write text "cd " & quoted form of p
end tell

But that won't work if for example the current session is busy or running a less or vim process.

Wrapping the script in a try block makes it fail silently. reopen opens a new terminal window if there are no visible windows or if only for example the preferences window is open. Finder also has an insertion location property, which is usually target of Finder window 1 or the desktop. But there is a bug in 10.7 and later where it often refers to some other window than the frontmost window.

Some potential problems with bulljit's script:

  • It has a one second delay. I don't know if it's needed for something, but I didn't need any delays when I tested this script.
  • If you have set iTerm to open new windows in full screen and there are no open windows, it opens a non-fullscreen window.
  • It tells Finder to get the path of front window (window 1), which can be an information window or a preferences window. Finder window 1 would always be a file browser window.
  • It changes the directory to / if the frontmost Finder window is displaying a view that doesn't have a path (like the Network view).

I prefer just using a function like this though:

cf () {
  c "$(osascript -e 'tell application "Finder"
    POSIX path of (target of Finder window 1 as alias
  end tell)' 2> /dev/null)"
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.