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

10.4: Use Automator to open chosen folder in Terminal UNIX
Tiger only hintWant an Automator plug-in to cause Terminal to cd into the selected folder (or the containg folder, if you have a file selected).

It's a pretty simple thing to do, with only two elements. Launch Automator and add these two steps to the workflow area via drag and drop:
  1. In the Library column, select Finder, then drag the Action named Get Selected Finder Items into the right-side work area.

  2. Click the Automator entry in the Library column, then drag the Run AppleScript action to the work area. The contents of the script shoudl be:
    on run {input, parameters}
      tell application "Terminal"
        activate
        if (the (count of the window) = 0) or 
         (the busy of window 1  = true) then
          tell application "System Events"
            keystroke "n" using command down
          end tell
        end if
        do script "cd \"" & (POSIX path of 
         (input as string)) & "\"" in window 1
      end tell
      return input
    end run
    
  3. Next, select File: Save as Plug-in, give your new command a name (I called mine "cd"), and make sure the pop-up shows that it will be a Finder plug-in.
When activated via the contextual menu, this command will issue a cd path_of_finder_selection to the frontmost Terminal window. I like it this 'light' way, since it's less window-cluttering than opening a new window each time.

[robg adds: I made a couple small modifications to the script above. First, I added the quoting to protect paths with spaces in their names. Second, I added the activate line to bring the Terminal to the front after executing the cd command.

If you'd rather have a new Terminal window open each time you use the plug-in, you can simplify the script somewhat. First, remove the if (the (count section (through the endif, as you'll be making a new window anyway. Second, change do script "cd.. into do script with command "cd...". There may be slightly better ways to do this, too, but this worked well for me in testing.

Two older hints have scripts to do similar things, but directly in the Finder via the sidebar. These could also be converted into Automator actions, I believe.]
    •    
  • Currently 1.83 / 5
  You rated: 4 / 5 (6 votes cast)
 
[32,698 views]  

10.4: Use Automator to open chosen folder in Terminal | 28 comments | Create New Account
Click here to return to the '10.4: Use Automator to open chosen folder in Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4: Use Automator to open chosen folder in Terminal
Authored by: Arlo on Aug 31, '05 10:37:46AM
Not many people realize this, but for a Finder plugin you don't need the initial "Get Selected Finder Items" action; the plug-in itself performs this. This workflow works fine with the applescript step alone.

---
www.automatorworld.com
"Better Living Through Macintosh Scripting"

[ Reply to This | # ]

10.4: Use Automator to open chosen folder in Terminal
Authored by: jbella on Aug 31, '05 01:06:02PM

Another trick that people dont seem to know about is that they can drag any finder item into a terminal window, and the path with be pasted in. Very handy!



[ Reply to This | # ]
10.4: Use Automator to open chosen folder in Terminal
Authored by: juanfal on Aug 31, '05 02:40:35PM

Ok, ok.

But what if you haven't open Terminal, nor wants to position at the start of the dropped line to write "cd "

I do this kind of command (cd'ing to a finder path) and I can assure you this is the most natural and quick way!



[ Reply to This | # ]
Converted for iTerm?
Authored by: Tomnibus on Aug 31, '05 01:37:34PM

Can this be converted to use iTerm?

I would like to open a new tab each time I changed the script to this:

[code]on run {input, parameters}
tell application "iTerm.app"
activate
tell application "System Events"
keystroke "t" using command down
end tell
do script with command "cd \"" & (POSIX path of
(input as string)) & "\"" in window 1
end tell
return input
end run[/code]

But it's not working the automator returns "AppleScript Error (null)"



[ Reply to This | # ]
Converted for iTerm?
Authored by: juanfal on Aug 31, '05 02:42:51PM

I dont use iTerm. Perhaps "iTerm" do not recognize "do command"



[ Reply to This | # ]
Converted for iTerm?
Authored by: wgscott on Aug 31, '05 04:10:09PM
This works with iTerm:

[ Reply to This | # ]
Converted for iTerm?
Authored by: juanfal on Sep 01, '05 06:29:05AM

Thanks a lot, I think they will be very useful when you are working in Terminal/iTerm:

cdf
posd
posfind
posgrep
fdc
rmm

Thanks, I've downloaded them and testing.



[ Reply to This | # ]
Converted for iTerm?
Authored by: luomat on Feb 16, '11 07:03:23AM

This seems to work well for iTerm:

http://putnamhill.net/applescript/open-iterm-here

Edited on Feb 16, '11 07:04:25AM by luomat



[ Reply to This | # ]
10.4: Use Automator to open chosen folder in Terminal
Authored by: juanfal on Aug 31, '05 02:54:03PM
Thanks Rob and Arlo
Finally the most simple (and secure) way should be this only Script in Automator!:
on run {input, parameters}
	
	tell application "Terminal"
		activate
		if (the (count of the window) = 0) or (the busy of window 1  = true) then
			tell application "System Events"
				keystroke "n" using command down
			end tell
		end if
		
		do script "cd " & (quoted form of POSIX path of  (input as string))  in window 1
	end tell
	return input
end run

I have tested it with "accented" paths and paths withs spaces and it works perfectly.
thanks for your comments!

[ Reply to This | # ]
10.4: Use Automator to open chosen folder in Terminal
Authored by: fabrizio on Aug 31, '05 04:00:48PM
There's no need to open a new window using System Events. If you use "do script" command without specifying a window, Terminal will open a new one.

on run {input, parameters}
tell application "Terminal"
activate
if (the (count of the window) = 0) or (the busy of window 1  = true) then
do script "cd \"" & (POSIX path of (input as string)) & "\""
else
do script "cd \"" & (POSIX path of (input as string)) & "\"" in window 1
end if
end tell
return input
end run


[ Reply to This | # ]
10.4: Use Automator to open chosen folder in Terminal
Authored by: mrodkey on Aug 31, '05 06:14:06PM

This does not work your terminal window is open to with SSH session. It tries to execute the cd command on the remote host. So unless you always keep your first terminal window local only it may be best to modify this to always create a new window.



[ Reply to This | # ]
10.4: Use Automator to open chosen folder in Terminal
Authored by: juanfal on Sep 01, '05 06:44:14AM
No.

If the front-most window is doing ssh, Terminal (at least, I don't know iTerm), considers itself "busy", then the script will open a new window, as fabrizio points, since the "do command" without reference to any window issues the command in a fresh new window.
Then the last version:
on run {input, parameters}
	tell application "Terminal"
		activate
		if (the (count of the window) = 0) or (the busy of window 1  = true) then
			do script "cd " & (quoted form of POSIX path of  (input as string))
		else
			do script "cd " & (quoted form of POSIX path of  (input as string))  in window 1
		end if
	end tell
	return input
end run
     

[ Reply to This | # ]
10.4: Use Automator to open chosen folder in Terminal
Authored by: mrodkey on Sep 01, '05 07:07:54PM

Yes :)

I copied and pasted the above script into automator and saved it as a finder plug-in. my 1 Terminal window was an ssh session to a remote machine and the behavior I observed was that the cd command was sent to that window and not a new one.

Reproduction Steps:
open terminal
ssh to remote host (with command ssh zlinux )
switch to terminal and run the above action on a file or folder

here are the results ...
[mrodkey@zlinux mrodkey]$ cd '/Users/mrodkey/'
-bash: cd: /Users/mrodkey/: No such file or directory

(zlinux is a remote machine)



[ Reply to This | # ]
10.4: Use Automator to open chosen folder in Terminal
Authored by: juanfal on Sep 01, '05 08:17:43PM

Ok, you are wright. You can remove 'ssh' from the 'don't alert on close' preference or look specifically for that kind of process in the title of the front-most window or, finally make _always_ a new window. I prefer not to make new windows when possible.



[ Reply to This | # ]
10.4: Use Automator to open chosen folder in Terminal
Authored by: dkulp on Jan 25, '06 11:36:34AM
I want to right click on a folder or a file. If a file then it should open the terminal in the enclosing folder.


on run {input, parameters}
	
	tell application "Terminal"
		activate
		set p to (quoted form of POSIX path of (input as string))
		set s to "if [ -d " & p & " ]; then cd " & p & "; else cd `dirname " & p & "`; fi"
		if (the (count of the window) = 0) or (the busy of window 1 = true) then
			do script s
		else
			do script s in window 1
		end if
	end tell
	return input
end run


[ Reply to This | # ]
10.4: Use Automator to open chosen folder in Terminal
Authored by: mush on Aug 31, '05 04:50:20PM

Can do this with contextual menus...

http://www.tco.net/~dscott/software/itermhere/



[ Reply to This | # ]
10.4: Use Automator to open chosen folder in Terminal
Authored by: pauljlucas on Aug 31, '05 11:28:08PM

You realize you can just drag any file/folder to a Terminal window and the full path to the file/folder will be pasted in, right?

---
- Paul



[ Reply to This | # ]
10.4: Use Automator to open chosen folder in Terminal
Authored by: juanfal on Sep 01, '05 06:48:23AM

Of course. That way is much longer. The contextual menu way is the shorter, precise and faster you can find, no doubt. I am costume myself to contextual menu actions and they can help you a lot. With the new Mighty Mouse, they will also be revamped.



[ Reply to This | # ]
10.4: Use Automator to open chosen folder in Terminal
Authored by: pauljlucas on Sep 01, '05 11:59:59AM

No, it's much shorter. You have to click, drag down to the Automator menu item, drag sideways to pop-up the Automator menu, drag down again to the cd item. Then the script has to run. This takes at least 5 seconds.

Dragging a folder to a Terminal window takes about 2 seconds.

---
- Paul



[ Reply to This | # ]
10.4: Use Automator to open chosen folder in Terminal
Authored by: malarkey on Sep 01, '05 01:54:50PM

Which is great if:

a) You already have Terminal running.

and

b) You already have the characters "cd" typed into the command prompt so you can go to that directory.

Otherwise, using the Automator script is much more convenient.



[ Reply to This | # ]
10.4: Use Automator to open chosen folder in Terminal
Authored by: skoggis on Sep 01, '05 02:59:12AM
This is even better if you add "; ls -la" to the command. It then prints the contents of the directory where you moved.
 do script "cd \"" & (POSIX path of (input as string)) & "\" ; ls -la"


[ Reply to This | # ]
iTerm Script
Authored by: Tomnibus on Sep 01, '05 12:06:11PM
With the help of "wgscott" I used the example of the scripts he pointed me to to come up with this:

on run {input, parameters}
  set the clipboard to "cd \"" & (POSIX path of 
     (input as string)) & "\""
  tell application "iTerm.app"
    activate
    tell application "System Events"
      keystroke "t" using command down
	  keystroke "v" using command down
	  keystroke return
    end tell

  end tell
  return input
end run
This will always open up a new tab and then CD to the directory. It works nicely for me.

[ Reply to This | # ]
10.4: Use Automator to open chosen folder in Terminal
Authored by: sporkstorms on Sep 03, '05 08:03:05PM

Here are a few more changes:

  • Works with iTerm
  • Opens a new terminal or tab for each file selected
  • if the selected thing isn't a folder, it cd's to that files folder instead of erroneously trying to cd to the file itself.

You can also download this dmg


on run {input, parameters}
   tell application "iTerm"
      activate
      repeat with someFilename in input
         make new terminal
         tell the first terminal
            activate current session
            launch session "Default Session"
            tell the last session
               set thePath to (quoted form of POSIX path of 
                  (someFilename as string))
               set cdCommand to "if [ -d " & thePath & " ]; then cd "  
                  & thePath & "; else cd `dirname " & thePath &  
                  "`; fi; clear"
               write text cdCommand
            end tell
         end tell
      end repeat
   end tell
   return input
end run


[ Reply to This | # ]
10.4: Use Automator to open chosen folder in Terminal
Authored by: daiyu on Oct 01, '05 04:22:33PM

Is there any way to do the opposite operation, that is, to open a Finder window of the directory you are currently in in the Terminal?


---
BJS



[ Reply to This | # ]
10.4: Use Automator to open chosen folder in Terminal
Authored by: sporkstorms on Oct 07, '05 12:03:24PM

There's a wonderful command-line program called "open." It is, for the most part, like double clicking something in Finder.

If you do "open Foo.txt" it will open that file in TextEdit (or whichever program it would open in from finder).

It also works with directories. You can do "open /some/path" and it will open a Finder window to that directory.

So.... you just need to type "open ." (a command i use allll the time). :) The '.' (dot) just means "the directory you're in" in various shells, in case you're not aware.



[ Reply to This | # ]
10.4: Use Automator to open chosen folder in Terminal
Authored by: osxpounder on Jul 09, '06 08:59:23AM
Yes. In Terminal, type:

open .

... and a Finder window will open for the directory you are in, in Terminal, when you type that.

The "open" command tells the Mac to open something using whatever the GUI normally uses when double-click a file [I think].

The period, ".", has a special meaning in path names: it means "this directory".

Thus, "open ." translates in English to "open this directory using whatever you'd normally use to do that", which on most Macs means open a Finder window.

I wonder what happens if you use PathFinder?

[ Reply to This | # ]

10.4: Use Automator to open chosen folder in Terminal
Authored by: v8media on Aug 23, '10 11:51:12AM

How would I change this so that I could run the terminal command "file" on the file I am right clicking on?

I've tried many variations, but I either am creating an uncompleted command (likely wrong # of quotes), or I've gotten the "file" command's help screen.

Thanks, Ian

---


Ian Page-Echols
v8media.com
visuals, interactive/installations, video, mac support



[ Reply to This | # ]
10.4: Use Automator to open chosen folder in Terminal
Authored by: tedw on Aug 23, '10 01:45:26PM
There is a downloadable application from Mark Liyanage called OpenTerminalHere.app. Download it, add it to the menubar of a Finder window (just drag and drop it onto the menu bar), and from that point on you can click it from any open Finder window to get a Terminal shell opened to that folder. It can be modified to use iTerm as well (I use that all the time); I'll post the details on request.

[ Reply to This | # ]