Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to automatically open a Terminal window to its directory UNIX
How many times have you been to a folder in Finder and needed a Terminal window open to that folder? For me, the answer was many. So, I wrote little AppleScript to take care of it automatically.

This script works by opening a Terminal window and cd'ing into the directory the script was in. Just copy the script into every folder you want to use if from; it's got a tiny footprint. Here's the code:
set myPath to (do shell script "dirname " & quoted form of (POSIX path of (path to me) as string)) as string
tell application "Terminal"
  activate
  do script ("cd " & quoted form of myPath)
end tell
Paste this into AppleScript Editor and save it as an application. Copy it into each folder you'll want to use it with.

I recommend naming it something like " Terminal" (the space in front will make it float to the top of a list of files).

And, it will Just Work. Double-click the app and Terminal will open with a new window cd'd to the folder where you just were in Finder.

[crarko adds: I tested this, and it works as described. We've run hints of this type before; here is another method.]
    •    
  • Currently 2.85 / 5
  You rated: 3 / 5 (13 votes cast)
 
[7,808 views]  

A script to automatically open a Terminal window to its directory | 25 comments | Create New Account
Click here to return to the 'A script to automatically open a Terminal window to its directory' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to automatically open a Terminal window to its directory
Authored by: richwiss on Jun 28, '11 07:51:15AM

The other way to do this that's even easier... install DTerm from the App Store (it's free). When you're in the Finder window, use the hot key (command-shift-enter by default), then type the command you want to run in the terminal followed by command-enter. If you just want the terminal with no command, just press command-enter and you get a terminal in that window. It also has a nice way of running commands in that directory without opening Terminal, and also a nice way of running a command outside of the Terminal and saving the result to the copy buffer.

Having just discovered DTerm the other day, I feel like it's my duty to tell everyone how cool it is. Enjoy!

Edited on Jun 28, '11 07:51:51AM by richwiss



[ Reply to This | # ]
A script to automatically open a Terminal window to its directory
Authored by: karog on Jun 28, '11 07:54:34AM
Or use OpenTerminal which puts an icon in the Finder Toolbar.

[ Reply to This | # ]
A script to automatically open a Terminal window to its directory
Authored by: RaceTripper on Jun 28, '11 09:01:34AM

If you have LaunchBar you can do this by typing Command-space-g-t. No need for scripts or anything else.



[ Reply to This | # ]
How is this easier than just dragging the icon?
Authored by: babbage on Jun 28, '11 10:07:14AM

I don't get it.

• Have Finder window open.
• Have Terminal window open.
• Type `cd ` in Terminal (note the space after "cd " ).
• Switch to Finder, drag the icon for the folder to Terminal.
• Terminal now says `cd /path/to/your/folder`, so hit return.

This has worked more or less forever. Why would anyone need a special script or third party apps to solve this "problem" ?

---
--
DO NOT LEAVE IT IS NOT REAL



[ Reply to This | # ]
How is this easier than just dragging the icon?
Authored by: everkleer80 on Jun 28, '11 11:18:59AM

Because most people find clicking/doubleclicking an icon (one step) easier than performing the four extra steps listed above. I too have been using your method for quite a while now, but I came across a hint like this last year and added a button to my Finder toolbar to Open the current folder in Terminal. I still use the drag and drop method, but when I don't happen to already be in Terminal, it's nice to be able to quickly open a Terminal window to the folder I'm working in.

As a side note, I didn't even realize I could drag files/folders into the Terminal window until a few years ago, and everyone I talk to is surprised when I show them they can do that (granted, most of the people here are a lot more Terminal-literate than any of my friends.)



[ Reply to This | # ]
How is this easier than just dragging the icon?
Authored by: babbage on Jun 28, '11 11:36:20AM

Yeah, but it isn't actually any extra steps.

The whole premise of the "hint" is that you have something you want in Finder, and you want to use it in Terminal. That, to me, presupposes that you have both Finder & Terminal running.

All I'm pointing out is that you can drag anything from Finder into a Terminal window, and run whatever command you want on it (cd, cp, tar, open, etc).

---
--
DO NOT LEAVE IT IS NOT REAL



[ Reply to This | # ]
How is this easier than just dragging the icon?
Authored by: nathanator11 on Jun 28, '11 02:38:33PM

You're right; your option is also good. It isn't extra steps, but for me it's extra time. That's because I use the keyboard more than the mouse, and since I can open this script with just a few keystrokes (especially in Column View), this way is faster for me. Your way is faster for a lot of people who prefer the mouse, be but I prefer the keyboard. Both are good options; it just depends on which is best for you.

As for assuming that both Finder and Terminal are open: Finder is always open (for most people), and the script opens Terminal if it isn't already.

Edited on Jun 28, '11 02:39:47PM by nathanator11



[ Reply to This | # ]
How is this easier than just dragging the icon?
Authored by: Anonymous on Jun 28, '11 03:37:27PM

"That, to me, presupposes that you have both Finder & Terminal running."

...or can have at the click of a Dock icon... or heck, a Finder Toolbar icon.



[ Reply to This | # ]
A script to automatically open a Terminal window to its directory
Authored by: Doc Drang on Jun 28, '11 11:09:55AM
ShellHere includes a nice Snow Leopard-style button to add to your Finder toolbar. And the advantage of tools like this is speed. They require no dragging and no pre-existing Terminal window.

[ Reply to This | # ]
A script to automatically open a Terminal window to its directory
Authored by: schutt on Jun 28, '11 11:24:59AM

Since AppleScripts often take a long time to start, it may be faster to just type "cd" into terminal, then drag the file to the terminal window. The path is automatically pasted.



[ Reply to This | # ]
A script to automatically open a Terminal window to its directory
Authored by: nathanator11 on Jun 28, '11 02:41:13PM

Actually, this script is just about instant OMM.

To me, it's way faster to invoke this with the keyboard than to use the mouse to drag. But, the mouse is fatser for many people, it's just a preference.



[ Reply to This | # ]
A script to automatically open a Terminal window to its directory
Authored by: Anonymous on Jun 28, '11 03:39:55PM

You probably have the Applescript interpreter running already. Many people don't use Applescript all that much, so using it for a single purpose like this really isn't that much of a timesaver.



[ Reply to This | # ]
A script to automatically open a Terminal window to its directory
Authored by: schutt on Jun 29, '11 12:21:06PM

I haven't looked into how long the AppleScript interpreter stays open, but I use it daily. Sometimes scripts are instant, other times there is a long delay before they run. If I have been running scripts, it is usually fast, but not always.

I also try to keep mostly to the keyboard, so if I need something in terminal, I usually just stay in terminal. I need this rarely enough that its faster for me to use the `cd'+drag method.



[ Reply to This | # ]
A script to automatically open a Terminal window to its directory
Authored by: Eric3 on Jun 28, '11 04:57:05PM
A script to automatically open a Terminal window to its directory
Authored by: mistersquid on Jun 29, '11 06:56:37PM

I was going to link these, too. There’s no need to reinvent this wheel. Liyanage’s scripts have served me well since (I think) Jaguar.



[ Reply to This | # ]
A script to automatically open a Terminal window to its directory
Authored by: regulus on Jun 28, '11 08:31:22PM
The hint is OK except for the fact that you have to place a copy of the script in every folder where you want this to work. Why not have 1 script that works no matter which folder you are in? Here's that. Just put this in your Script menu and then it's accessible any time from any folder.
tell application "Finder" to set f to (target of window 1) as text
tell application "Terminal" to do script "cd " & quoted form of POSIX path of f
NOTE: to show the script menu open AppleScript Editor, under preferences check the box "Show Script Menu". Then add this script to that menu by placing it in the folder ~/Library/Scripts.
---
Hank
http://www.hamsoftengineering.com


[ Reply to This | # ]
A script to automatically open a Terminal window to its directory
Authored by: Lri on Jul 06, '11 02:25:32AM
tell application "Finder" to set il to insertion location as alias
set cmd to "cd " & quoted form of POSIX path of il
tell application "Terminal"
	activate
	try
		do script with command cmd in window 1
	on error
		do script with command cmd
	end try
end tell
  • Doesn't always open a new window
  • Makes the Terminal window frontmost
  • Uses insertion location, so it works on for example Desktop


[ Reply to This | # ]
How about an app from the app-store?
Authored by: dat5h on Jun 28, '11 10:11:51PM

I strongly recommend you check out "DTerm" from the app-store. I was looking for a way to open a terminal window from the Finder and tried making this kind of AppleScript once before myself. I also checked out some old Leopard app that sits in the finder window and you click it, but these only did what your AppleScript does (open the folder in the Terminal). DTerm will let you open up the folder, but also lets you just run shell scripts from the Finder and then go about your business without necessarily opening up the terminal if you don't need it for too many operations. Check it out



[ Reply to This | # ]
DTerm - awesome!!
Authored by: everkleer80 on Jun 29, '11 05:10:35PM

Sweet! I just checked out this app and it looks awesome! It provides a nice little on demand window for those times I just need a quick Terminal to run one or two small commands, and if I'm using it and I decide I need a full Terminal window, it will open one in the blink of an eye!



[ Reply to This | # ]
A script to automatically open a Terminal window to its directory
Authored by: Mr.C.Tippins on Jun 29, '11 12:00:10AM
Why not use AUtomator to create a Service for Finder so you can just right-click on your folder and select "Open folder in Terminal"?

Do this:

Open Automator
Create a new Service
Drag the Run Applescript action over
Paste in the following Applescript slightly modified from the original poster:
tell application "Finder" to set the myPath ¬
	to (the target of the front window) as alias
set myPath to POSIX path of myPath

tell application "Terminal"
	activate
	do script ("cd " & quoted form of myPath)
end tell

Save that as something like: Open folder in Terminal
And there ya go......

There is a way to pass your selection from the FInder to your script but I didn't work long enough to figure out how that works.

Enjoy....

[ Reply to This | # ]
A script to automatically open a Terminal window to its directory
Authored by: Mr.C.Tippins on Jun 30, '11 08:52:42PM

You can even use the Keyboard pref pane to assign a key combo for your newly create Context menu item!



[ Reply to This | # ]
A script to automatically open a Terminal window to its directory
Authored by: cuban321 on Jun 29, '11 04:38:05PM

You won't need this much longer:

http://macosxautomation.com/lion/terminal.html

Hooray!

Edited on Jun 29, '11 04:38:19PM by cuban321



[ Reply to This | # ]
A script to automatically open a Terminal window to its directory
Authored by: everkleer80 on Jun 29, '11 04:54:16PM

That's pretty cool, thanks for pointing it out!

Although I don't think anything there is a replacement for having a 1-click method to open a terminal window for the current folder.



[ Reply to This | # ]
A script to automatically open a Terminal window to its directory
Authored by: ecbtln on Jul 06, '11 07:04:06PM
> cd to ...
Edited on Jul 06, '11 07:07:33PM by ecbtln


[ Reply to This | # ]
A script to automatically open a Terminal window to its directory
Authored by: ceej on Jul 29, '11 03:32:24AM

There's actually already a Service for this. (In Lion at least. I don't remember if it was there before.)

System Preferences -> Keyboard -> Keyboard Shortcuts -> Services -> Files and Folders -> New Terminal at Folder (or New Terminal Tab at Folder). You can create a keyboard shortcut here, too, if you're in to that kind of thing.

Right-click on any folder and the item appears in the bottom section of the contextual menu (or use your new keyboard shortcut). Works on "package" files, too.

Note: Services do not work on sidebar items or aliases; you have to select an actual folder or file.

(I'll submit a hint for this in the 10.7 category.)



[ Reply to This | # ]