Opening Finder Folder in iTerm


I saw a little script in Macworld that allows you to right click on a folder in Finder and have it open in Terminal. I often find it handy to use the command line and the Finder simultaneously, so this seemed like a handy thing to do. Note that you can always open the current directory in Finder by typing open . at the command line.

The problem is that the Macworld script is set up for Terminal and I use iTerm because I like the tabs. I found this script but it required the installation of some other things which I didn't want to do. So, I combined the ideas from both scripts to create one that does the job:

tell application "Finder"
    set myWin to window 1
    set theWin to (quoted form of POSIX path of  (target of myWin as alias))
\ttell application "iTerm"
    \tmake new terminal
    \ttell the first terminal
       \t\tactivate current session
       \t\tlaunch session "Default Session"
       \t\ttell the last session
          \t\twrite text "cd " & theWin
       \t\tend tell
    \tend tell
    end tell
end tell
tell application "Finder"
    activate
end tell
tell application "iTerm"
    activate
end tell

To use this, open Automator and select Finder in the Library column and then drag the Get Selected Finder Items action followed by the Run Applescript action. Replace all the code in the Run Applescript action with the above code. Save it as a plug-in for Finder with a meaningful name (like "Open in iTerm"). You're done. Now, right-click on a folder in Finder and select the script from the menu. You'll get that directory in a new tab in iTerm.


Please leave comments using the Hypothes.is sidebar.

Last modified: Thu Oct 10 12:47:18 2019.