16

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.

9 Answers 9

6

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

0
10

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
1
  • 1
    I think this should be the accepted answer.
    – dhilipsiva
    Jul 6, 2013 at 14:33
8

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

2
  • 1
    Excellent job! Works perfectly. This should be the accepted answer.
    – rcd
    Jun 4, 2013 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. Jun 5, 2013 at 5:51
5

This is built in to iTerm2 as of version 3.1.0.

To use the functionality:
in Finder right click a folder -> Services -> New iTerm2 Window Here

Note: the Services submenu is at the very bottom of the right-click menu.

Reference
At this link click Show Older Versions, then under iTerm2 3.1.0 click Show Changelog and look for services, you'll find this:

Add support for finder services. You can right click in Finder to launch iTerm2 in that location.

2

I guess it is because the internals of iTerm has changed, but none of the solutions worked for me. What did was the following code:

tell application "Finder"
    set cur_dir to POSIX path of ((the target of the front Finder window) as string)
end tell
tell application "iTerm"
    tell (create window with default profile)
        write current session text "cd " & quoted form of cur_dir
    end tell
end tell

Or using Automator as a finder service:

on run {input, parameters}
    tell application "Finder"
        set cur_dir to POSIX path of (input as string)
    end tell
    tell application "iTerm"
        tell (create window with default profile)
            write current session text "cd " & quoted form of cur_dir
        end tell
    end tell
end run
2

Take a look at the cdto project hosted on https://github.com/jbtule/cdto "Finder Toolbar app to open the current directory in the Terminal (or iTerm, X11). This app is designed (including its icon) to placed in the finder window's toolbar."

1
  • But it opens two iTerms windows which is highly annoying after a while. May 22, 2013 at 17:08
1

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.

0

With iTerm:

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

0

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)"
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .