Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Launching iTerm2 from macOS Finder

Launching iTerm2 from macOS Finder

(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


From a selected directory in a Finder window - via keyboard Shortcut

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.

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.

From the working directory of a Finder window - via keyboard Shortcut

  1. Run Automator, File -> New and choose Quick Action.
  2. Click Library -> Utilities and double click Run AppleScript.
  3. Workflow receives Automatic (Nothing) in any application.
  4. Paste the script from AppleScript section below.
  5. 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.
  6. Add the keyboard shortcut under System Preferences -> Keyboard -> Shortcuts -> Services under the General section. Check iTermOpenHereScript and Add Shortcut. Note that the first time you launch there may be a security popup saying iTermOpenHereScript wants access... Click OK to all.
  7. 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.
  8. 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 directory).

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.

From the working directory of a Finder window - via Finder toolbar icon

  1. Run Automator, File -> New and choose Application.
  2. Click Library -> Utilities and double click Run AppleScript.
  3. Paste the script from AppleScript section below.
  4. 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.
  5. Drag and drop iTermOpenHereScript.app onto the Finder toolbar while holding down the option+command 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.
  6. Right click on the spot on the toolbar and select Icon Only. To replace the generic Automator app icon with the iTerm2 icon, command+i on the original iTerm.app file, select the application icon in the top left corner of the window and command+c. Then do command+i to iTermOpenHereScript.app file, select the application icon in the top left corner of the window and command+v.
  7. 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.
  8. To make windows open a bit faster, in iTerm2's Preferences -> General -> Closing uncheck Quit when all windows are closed.

AppleScript

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

📜 MIT License

@luisgerhorst

This comment has been minimized.

Copy link

@luisgerhorst luisgerhorst commented Jul 20, 2016

Is there no longer a way to make it use the current iTerm window?

For that behavior, see the original author's version at https://github.com/peterldowns/iterm2-finder-tools

@nvh6

This comment has been minimized.

Copy link

@nvh6 nvh6 commented Nov 28, 2017

Thank you so much. You make my life much much better. Also I learned a lot and started liking Apple script now. Thanks again.

@ishaiMeresman

This comment has been minimized.

Copy link

@ishaiMeresman ishaiMeresman commented Dec 6, 2017

Working! Nice,
Is there a way to create a similar shortcut that will open a terminal in the location of the folder I'm in?

Yes! I updated the instructions and script for that. See the "From the working directory of a Finder window - via keyboard Shortcut" section.

@jorgeehernandez

This comment has been minimized.

Copy link

@jorgeehernandez jorgeehernandez commented Aug 24, 2018

Works like a charm! Thanks for sharing.

@Zillion01

This comment has been minimized.

Copy link

@Zillion01 Zillion01 commented Nov 6, 2018

Very nice thanks! Any suggestion for a nice icon, which wont look blurry?

The one copied from the iTerm app looks pretty good to me (not blurry). Not sure why yours would be.

@hoonseo0409

This comment has been minimized.

Copy link

@hoonseo0409 hoonseo0409 commented Feb 22, 2019

It works thanks

@WANGJIEKE

This comment has been minimized.

Copy link

@WANGJIEKE WANGJIEKE commented May 12, 2019

Thanks

@tonisives

This comment has been minimized.

Copy link

@tonisives tonisives commented Jun 17, 2019

New iTerm2 Tab Here already existed for me in keyboard services. Maybe it is added automatically with newer iTerm2 versions

Yes, newer iTerm2 versions already have this now for opening an iterm window at the selected Finder directory. I'll update the gist to point this out. But to open one in the current Finder directory, the second script is still needed. Thanks!

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Jul 2, 2019

New iTerm2 Tab Here already existed for me in keyboard services. Maybe it is added automatically with newer iTerm2 versions

yes, we can set it wit ctrl + T, others has been coverd.

Yes, newer iTerm2 versions already have this now for opening an iterm window at the selected Finder directory. I'll update the gist to point this out. But to open one in the current Finder directory, the second script is still needed. Thanks!

@nvmnghia

This comment has been minimized.

Copy link

@nvmnghia nvmnghia commented Sep 19, 2019

Somehow I can't assign a keyboard shortcut to this service, or any service that tries to open Terminal or iTerm2 from Finder (10.14.6).
The right-mouse menu works, but not handy at all.

Edit: strangely, your script triggers 2 iTerm windows, one at the default #HOME, the other at the current folder as intended.

See step 7 under "From the working directory of a Finder window - via keyboard Shortcut" to fix this.

@Myrvoll

This comment has been minimized.

Copy link

@Myrvoll Myrvoll commented Sep 27, 2019

Somehow I can't assign a keyboard shortcut to this service, or any service that tries to open Terminal or iTerm2 from Finder (10.14.6). >The right-mouse menu works, but not handy at all.

I see this too. Not sure if it is an OS bug or not.

Is there any way to run an arbitrary service like this from e.g. Keyboard Maestro?

I've seen this when the shortcut key was already assigned somewhere else - and it can be fairly well hidden where. Also, less often, it takes a machine reboot to get the shortcut key working.

@MuttakinHasib

This comment has been minimized.

Copy link

@MuttakinHasib MuttakinHasib commented Apr 11, 2021

I tried with Hyper terminal & got this error

[after replacing iTerm with Hyper in script]

Screen Shot 2021-04-11 at 12 07 58 PM

I had a look at Hyper. It's not been set up to be usable with AppleScript. The Hyper devs would need to do something like this to make it work: https://christiantietze.de/posts/2020/02/applescript-boilerplate-2020

@olilarkin

This comment has been minimized.

Copy link

@olilarkin olilarkin commented Apr 21, 2021

Thankyou!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment