Three ways to open the current Finder window in the OS X Terminal

NewTerminalIconXOS X includes a service where you can open the OS X Terminal at a selected folder, but this is not always the optimal approach. For one, it requires you select a subfolder to act on, which will have the Terminal open it instead of the current Finder window, requiring you to then “cd ..” to get to the folder you want. This also requires there be a folder to select in order for this to work in the first place. Alternatively, there are three additional approaches you can use which may be quicker and easier approaches.

Drag to Dock

First, since the OS X Terminal supports drag-and-drop from the Finder, by placing the Terminal in the Dock (or opening it so it appears in the Dock) you can drag any items to it open that item with the Terminal. For the current window, simply drag the folder icon from the Finder window’s title bar to the Terminal icon, and the Terminal will open at that folder. Granted click-and-drag can be cumbersome so this is not the most optimal approach, but it is a quick setup if there is the sudden need for Terminal work.

Third-party tools

Another approach is to use third-party tools such as “cd to” which you can place in the Finder toolbar and when clicked, will trigger the Terminal to open at the current Finder window.

Use Automator and AppleScript

AppleScript in OS X provides you with a full set of automation tools, that along with Automator and OS X’s custom keyboard shortcut service, can place this ability right at your fingertips. For those who frequently use the Terminal, this approach may be the more optimal in the long run:

  1. Open Automator.
  2. Create a new Service workflow.
  3. Set the options at the top of the window to receive “No Input” in “Finder.”
  4. Drag the Run Applescript action to the workflow.
  5. Replace all of the boilerplate code with the following two Applescript lines (highlight them and copy and paste, to avoid any word-break hyphenations inserted by your browser here):
tell application "Finder" to set Dir to "'" & (POSIX path of (target of front window as text)) & "'"

tell window 1 of application "Terminal" to do script "cd " & Dir
Automator Applescript to open Finder window in the Terminal

After selecting the input settings and dragging the AppleScript action to the workflow, replace all template code with the two lines above, and save the workflow.

When done, save the workflow with a name like “Open Current Finder Window in Terminal.” This will make the service available in the Finder without requiring any item be selected or specifically targeted. Since this workflow is a service (a program that will accept commands and input from other programs) you can now use the Keyboard system preferences to bind a custom hotkey shortcut to this service so you can activate it quickly:

  1. Open the Keyboard system preferences.
  2. Go to the Shortcuts tab.
  3. Click Services and locate the new “Open Current Finder Window in Terminal” workflow.
  4. Click “Add Shortcut” and type the hotkey combination of your choosing.
Custom service hotkey shortcut

Setting a hotkey shortcut for the new service is the final touch, allowing you to activate it at anytime with a quick key combination.

You can use any combination of modifier keys (Shift, Option, Command, Control) and alphanumeric keys. For custom shortcuts, I recommend Option-Command-Control as a base for the hotkey, as these are easy to press simultaneously and are rarely used in other applications (especially the Finder). When done, you can open any Terminal window and press this hotkey to open the window in the Terminal.

As a quick note, when in the Terminal, simply run the command “open .” to open the current Terminal path in the Finder.

3 thoughts on “Three ways to open the current Finder window in the OS X Terminal

  1. arturocp

    I created an Automator Service with AppleScript. Just a question, is there a way to focus on the new Terminal windows after executing the script? Thanks for your help!

  2. arturocp

    I set as window title the ‘Working directory or document’ and ‘Path’ in Preferences-> Profiles (Default profile) -> Window

    After this I just added some lines to the script:

    tell application “Terminal”
    windows where name contains Dir
    if result is not {} then set index of item 1 of result to 1
    end tell

Comments are closed.