Open iTerm2 Here context menu option

October 07, 20162 min read

One of the things I miss most from using Ubuntu is the ability to open the terminal from any directory. Instead of cd-ing down a long directory path you can add an Open iTerm Here option to Finder's context menu (right click) for easy directory access to terminal.

Setup

1) Open up Automator and create a new file and select Services as your document type.

Screenshot

2) For the Service receives selected field, select Folders.

Screenshot

3) Drag Run AppleScript onto the right hand window to add some of the AppleScript code that will power the service.

Screenshot

4) Paste the following code:

on run {input, parameters}
    cd(input)
end run

on cd(dir)
    tell application "iTerm"
        activate
        set terminalWindow to current window
        
        if terminalWindow is equal to missing value then
            set terminalWindow to (create window with default profile)
        else
            tell terminalWindow
                set t to (create tab with default profile)
            end tell
        end if
        
        tell current session of terminalWindow
            write text "cd " & (quoted form of POSIX path of (dir as string))
        end tell
    end tell
end cd

5) Hit Command+S to save the service. Whichever name you save it under will appear in the context menu when you right click on a folder.

Screenshot

6) Right click on any finder folder and click Open in iTerm2! Voila!

Caveats

The context menu will only appear when you right click on a folder. Unfortunately that means if you're already in a folder, you cannot open the iTerm in that location. You'll have to go up a directory level and open the context menu from there.