(Based on info from Peter Downs' gitub but with modified behavior to open a new terminal window for each invocation instead of reusing an already open window.)
The following three ways to launch an iTerm2 window from Finder have been tested on iTerm2 version 3+ running on macOS Mojave+.
pdanford - April 2020
Starting sometime with iTerm2 version 3.x, it puts New iTerm2 Window Here in System Preferences -> Keyboard -> Shortcuts -> Services under the Files and Folders section. You just have to check it and Add Shortcut (e.g. ^⌥⌘T).
Note that sometimes macOS doesn't register a new keyboard Shortcut assignment immediately and may even require an OS restart to get it working. But also make sure the keyboard Shortcut is unique and not already assigned somewhere else.
This is only active when a Folder is currently selected in a Finder window that has focus.
- Run Automator, File -> New and choose Quick Action.
- Click Library -> Utilities and double click Run AppleScript.
- Workflow receives Automatic (Nothing) in any application.
- Paste the script from AppleScript section below.
- Save; if Automator tries to save to iCloud drive, better to save locally - save as iTermOpenHereScript.workflow to the Desktop and then move it to
~/Library/Services
. - Add the keyboard shortcut under System Preferences -> Keyboard -> Shortcuts -> Services under the General section. Check iTermOpenHereScript and Add Shortcut (e.g. ^⌥⌘Y). Note that the first time you launch there may be a security popup saying iTermOpenHereScript wants access... Click OK to all.
- To prevent two windows from opening when you use the keyboard Shortcut when iTerm2 is not already running, set iTerm2's Preferences -> General -> Startup to Only Restore Hotkey Window for the Window restoration policy.
- To make windows open a bit faster, in iTerm2's Preferences -> General -> Closing uncheck Quit when all windows are closed.
Note that sometimes macOS doesn't register a new keyboard Shortcut assignment immediately and may even require an OS restart to get it working. But also make sure the keyboard Shortcut is unique and not already assigned somewhere else.
This is only active when a Finder window (or the Desktop itself) has focus (and ignores any selected Finder Folders).
The Desktop having focus isn't really the intended use case, but for completeness, if it's the Desktop that has focus the working directory of its last focused still-open Finder window is used. If the current Desktop does not have any Finder windows, it's a bit indeterministic, but a working directory from a Finder window in other Space is used. If there are no Finder windows open in any other Spaces, it will default to the user's home directory.
To make an Automator App that can reside on the Finder Toolbar to open an iTerm2 window at the current focused working Folder:
- Run Automator, File -> New and choose Application.
- Click Library -> Utilities and double click Run AppleScript.
- Paste the script from AppleScript section below.
- Save; if Automator tries to save to iCloud drive, better to save locally - save as iTermOpenHereScript.app to the Desktop and then move it to
~/Library/Services
. - Drag and drop iTermOpenHereScript.app onto the Finder Toolbar while holding down the ⌥⌘ keys to place a toolbar launch icon. Note that the first time you launch there may be a security popup saying iTermOpenHereScript wants access... Click OK to all.
- Right click on the spot on the toolbar and select Icon Only. To replace the generic Automator app icon with the iTerm2 icon, ⌘i on the actual iTerm.app file in
/Applications
, select the application icon in the top left corner of the window and ⌘c. Then do ⌘i to iTermOpenHereScript.app file, select the application icon in the top left corner of the window and ⌘v. - To prevent two windows from opening when you click the toolbar launch icon when iTerm2 is not already running, set iTerm2's Preferences -> General -> Startup to Only Restore Hotkey Window for the Window restoration policy.
- To make windows open a bit faster, in iTerm2's Preferences -> General -> Closing uncheck Quit when all windows are closed.
on run {input, parameters}
set frontApp to (path to frontmost application as Unicode text)
if (frontApp does not contain "Finder.app") then
-- Finder does not have focus.
return
end if
tell application "Finder"
set listSize to count of (every window)
if listSize is equal to 0 then
-- The Finder desktop has focus and no windows anywhere else. default to home dir.
set dir_path to "~"
else
try
set dir_path to quoted form of (POSIX path of (Folder of the front window as alias))
on error errMsg
-- This is a special dir (e.g. Network or "machine name"). default to home dir.
set dir_path to "~"
end try
end if
end tell
CD_to(dir_path)
end run
on CD_to(theDir)
tell application "iTerm"
activate -- Bring the iTerm window on the foreground
set term_window to (create window with default profile)
set sesh to (current session of term_window)
tell sesh to write text " cd " & theDir & ";clear"
end tell
end CD_to