Open current Finder folder in Terminal

Today's Best Tech Deals

Picked by Macworld's Editors

Top Deals On Great Products

Picked by Techconnect's Editors

As someone who works with the Terminal a lot, I often find myself wanting to do things between the Terminal and the Finder. For example, there are times when I want to open the current Terminal directory in the Finder. That’s actually pretty simple to do—just type open . (note the trailing dot!) and press Return. The Finder will activate with the proper folder opened. Most of the time, though, it seems what I want to do is work with the current Finder folder in the Terminal. The solution to that challenge isn’t quite so simple.

A couple years back on, we ran a hint that explained how to use AppleScript to create a droplet to cd to open the chosen folder in the Terminal via drag and drop. The droplet could be kept in your Sidebar (that’s where mine is), Dock, or anywhere else you liked for fast access. I’ve been using that one ever since, and it’s worked quite well. But I also dislike using the mouse to drag and drop every time I wanted to open a folder in the Terminal.

Enter 10.4 and Automator. Using a very simple Automator workflow, you can create a Finder plug-in that lets you control-click on an object in the Finder and navigate to it in the Terminal. If you choose a file, the file’s containing folder is opened; if you choose a folder, then that folder itself is opened. Since it’s a contextual menu, much less mouse movement is required, which I definitely like (though I still have my Sidebar droplet as well).

To create the plug-in, open Automator and click on the Finder entry in the Library column. In the Action column, click and drag Get Selected Finder Items into the blank work area on the right side of the window. Next, click on the Automator entry in the Library column, then drag the Run AppleScript entry from the Action column into the work area—drop it below the Get Selected Finder Items action that’s already there.

OK, now for the hard part—the actual AppleScript code that will do all the work. Thankfully, thoughtful macosxhints reader Greg Spence has already done the hard work for us (I tweaked it just a bit for some additional options). Just copy and paste the below code into the script area of the Run AppleScript action you just placed into the work area—replace all the existing text you see there with this:

	on run {input, parameters}

tell application "Finder"
set myWin to window 1
set theWin to (quoted form of POSIX path of (target of myWin as alias))
tell application "Terminal"
tell window 1
do script "cd " & theWin
-- do script "cd " & theWin & ";ls -al | more"
end tell
end tell
end tell

return input
end run

When you’re done, your workflow should look like this:

Note that the script gets cut off in that screenshot (the script area can’t be expanded), so make sure you have all the code from above, not just what you see there.

If everything looks good, you’re ready to make your workflow an easy-to-use contextual menu item. Choose File: Save As Plug-In from Automator’s menus. In the Save Plug-in As box, give your new workflow a meaningful name, like Open in Terminal or similar. Make sure the Plug-in For pop-up says Finder, and then click Save. That’s it; you’re done!

To test your new menu item, switch to the Finder, select any item, and then control-click on your selection. Choose Automator: Open in Terminal (or whatever you named your action), and you should see a new Terminal window open, and then the cd command will execute to switch you into the folder you’ve selected.

Customizing the script

Once you’ve got the basics working, you may wish to tweak the script a bit to your liking. I’ve left one example already in the code to show what’s possible:

	do script "cd " & theWin

-- do script "cd " & theWin & ";ls -al | more"

Note that there are two lines there, both quite similar. The second, though, is commented out (the -- symbols denote a comment in AppleScript). This means that the command wont’ be executed. As you can see, it’s quite similar to the one above, but adds a semicolon and then the ls -al command. If you uncomment this line (remove the dashes) and comment (or delete) the one above it, then after the Terminal navigates into the proper directory, it will also do a directory listing for you. Remember to save your changed workflow after doing this edit. Using this example as a template, you could modify the workflow to run the command(s) of your choice after the cd command.

Note: When you purchase something after clicking links in our articles, we may earn a small commission. Read our affiliate link policy for more details.
Shop Tech Products at Amazon