Skip to content

Instantly share code, notes, and snippets.

@NatashaKSS
Last active May 17, 2022 09:17
Show Gist options
  • Save NatashaKSS/429c9ac374628c79bfd929b19976516d to your computer and use it in GitHub Desktop.
Save NatashaKSS/429c9ac374628c79bfd929b19976516d to your computer and use it in GitHub Desktop.
Open a new iTerm2 tab / window from a Finder shortcut

Open a new iTerm2 tab / window from a Finder shortcut

This script opens a new iTerm tab at your currently active Finder window's directory. It opens a new iTerm window there instead if you don't have iTerm running yet.

Referenced from the original tutorial written by Peter Downs at http://www.jofu.org/blog/2016/02/04/open-iterm-tab-from-finder-by-shortcut.

This is for you if...

You just want a short script with minimal configuration to get this shortcut working. For a more fully-featured solution, please use "iTerm2 Finder Tools" which you can download from Peter Down's GitHub repository.

Getting Started

Version Compatibility

It should work for most other MacOS / iTerm2 versions, but this is my setup:

  • MacOS X 13.3 High Sierra
  • iTerm2 Build 3.1.5

Steps

  1. Run Automator. It'll prompt you to create a new workflow / application / service ...
  2. Select Application and click on Choose
  3. On the left sidebar, go to Utilities > Run Applescript
  4. Double-click on Run Applescript to create a new Applescript file
  5. Copy & Paste the code in iterm_shortcut.scpt below into the Applescript file
  6. Save the application somewhere in a local directory
  7. Open Finder and navigate to the directory where you saved the application
  8. Hold down command+option and drag & drop the application into your Finder window's toolbar as such:

Customizing the shortcut's icon

  1. Click on the application and press command+i to bring up its Get info page
  2. Find your original iTerm app in your /Applications folder and bring up its Get info page too
  3. Click on the iTerm app's icon
  4. Press command+C to copy the iTerm app's icon
  5. Click on the iTerm shortcut application's icon (in its Get info page) and paste with command+V like this:

Known issues

  • [PENDING] It seems that if I click on the red x close button on iTerm and try to invoke the shortcut again, it crashes and instead opens a new separate window.
# == Opens an iTerm tab at an active Finder window ==
#
# If an iTerm session already exists, a new tab is created.
#
on run {input, parameters}
# Get the path of the directory of the active window
tell application "Finder"
try
set curr_dir to quoted form of (POSIX path of (folder of the front window as alias))
on error
set curr_dir to startup disk
end try
end tell
cd_and_run_iterm_on(curr_dir)
end run
# == Launches iTerm ==
#
# First checks if a window is already open and creates a new tab if it is, else it
# creates its own window. The terminal session will then 'cd' into the directory of
# the currently active Finder window.
#
# @param curr_dir path to directory of Finder's currently active window.
#
on cd_and_run_iterm_on(curr_dir)
tell application "iTerm"
activate
set new_window to current window
if new_window is equal to missing value then
tell new_window to (create window with default profile)
else
tell new_window to (create tab with default profile)
end if
# command to 'cd' into Finder's current window session
tell current session of new_window
write text "cd " & curr_dir & ";clear"
end tell
end tell
end cd_and_run_iterm_on
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment