Skip to content

Instantly share code, notes, and snippets.

@pdanford
Last active March 13, 2024 14:33
Star You must be signed in to star a gist
Save pdanford/158d74e2026f393e953ed43ff8168ec1 to your computer and use it in GitHub Desktop.
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


To Launch an iTerm2 Window at:

a selected Folder in a focused 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 (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.

the current working Folder of a focused 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 (e.g. ^Y). 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 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.

the current working Folder of a focused Finder window - via Finder Toolbar icon

To make an Automator App that can reside on the Finder Toolbar to open an iTerm2 window at the current focused working Folder:

  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 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, 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.
  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
Copy link

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

Copy link

ghost 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
Copy link

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
Copy link

jorgeehernandez commented Aug 24, 2018

Works like a charm! Thanks for sharing.

@Zillion01
Copy link

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
Copy link

It works thanks

@WANGJIEKE
Copy link

Thanks

@tonisives
Copy link

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!

Copy link

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
Copy link

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.

@miroboru
Copy link

miroboru 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
Copy link

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
Copy link

Thankyou!

@Cretanian
Copy link

Thank you! Thank you! Thank you! Thank you! Thank you! Thank you!

@lingtalfi
Copy link

Thanks, automator rocks.

@mmolero
Copy link

mmolero commented Aug 20, 2023

Awesome! Thanks

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