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

A script to open current Finder folder in the Terminal Desktop
I have been using this script for about a year and everyone I have passed it on to has found it useful. This script does two things:
  1. Running it opens a new Terminal window and changes the directory to match the dirctory of the front most Finder window
  2. Dragging onto it changes the directory to the path of the item (or its containing folder)
I've now placed it in the sidebar of the Finder windows (previously in the menubar at the top), but it also works from the Dock. You can download the compiled script or compile it yourself with the code in the remainder of the hint...

[robg adds: There's an earlier hint with a similar script, though this one handles paths more robustly, I think...]


(*
Open in Terminal
Copyright 2002 Gregory Ramsperger <oit@jitjat.com>

To use:
     * Drag  a single container onto Open In Terminal to go
      to it in Terminal
       * Drag Open In Terminal to the toolbar of any finder
      window to add it to the toolbar
   * Place Open In Terminal in the Dock and use it to
      open the frontmost window or drag containers to it to open them.
*)

property badPathChars : ¨
{"\\","`","!","$","&","(",")","{","}","|","'","\"",";","<",">","?"," "}

-- drag-and-drop open
on open theList
  -- only open the first item in the list
  set dir to (item 1 of theList) as text
  
  -- cd in the terminal
  my oit(dir)
end open

-- direct run
on run
  try
    -- get the frontmost window of the finder
    tell application "Finder"
      set dir to the target of Finder window 1 as text
    end tell
    
    -- convert the path from Mac to unix
    my oit(dir)
  on error
    -- if there is no window open in the finder, open the desktop
    my oit(path to desktop)
  end try
end run

-- run a command in the terminal
on oit(d)
  tell application "Finder"
    
    -- make sure that the item is a directory
    if d does not end with ":" then
      set d to (container of item d) as text
    end if
  end tell
  
  set p to (POSIX path of d)
  
  -- the extra safe path translation method
  -- add a \ before every character
  repeat with c in badPathChars
    set AppleScript's text item delimiters to c
    set tempitemList to every text item of p
    set AppleScript's text item delimiters to ("\\" & c)
    set p to the tempitemList as string
    set AppleScript's text item delimiters to ""
  end repeat
  
  tell application "Terminal"
    activate
    do script with command ("cd " & p)
  end tell
end oit
[robg adds: Paste the above into Script Editor and save it (I saved it as an Application). Drop in the sidebar, menu bar, whereever ... a very useful addition!]
    •    
  • Currently 2.67 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[18,775 views]  

A script to open current Finder folder in the Terminal | 6 comments | Create New Account
Click here to return to the 'A script to open current Finder folder in the Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to open current Finder folder in the Terminal
Authored by: greggo on Nov 16, '03 03:09:43AM
I've been using this script to do the same thing...
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"
		activate
		tell window 1
			do script "cd " & theWin & ";ls -al | more"
		end tell
	end tell
end tell


[ Reply to This | # ]
A script to open current Finder folder in the Terminal
Authored by: kerbaugh on Nov 16, '03 04:42:36PM
   I couldn't agree with you more that the capability of interaction between the finder and terminal made possible by AppleScript is indispensable. However, I prefer being able to move around in the terminal window in which I'm currently working rather than opening a new window. I also like being able to do it from the terminal. I've written AppleScripts and shell scripts to help me do this. I also have scripts that change the Finder window. A friend, Bill Scott, prefers iTerm so he wrote comparable scripts for that application. He put icons on most these and put them in a disk image that you can get at:

http://kerbaugh.uncfsu.edu/pos.dmg.tgz

   It was rather difficult to get the Terminal to change directory in the current window. Terminal.app does make it possible to tell if the window is busy so that you don't step on an ongoing process. However, I had to use GUI scripting to make the actual change. In order to make this work on Panther, you must open System Preferences, go to the Universal Access Pane and check "Enable access for assistive devices" down at the bottom. Also, my script doesn't check for bad characters like Gregory's but it quotes the path so doesn't have problems with many of them.

   The ability to "grab" the selection in the finder is useful for more than just changing directory. I also like to be able to grep this directory and find files within it. The above package includes a few functions and aliases that do things like that.

[ Reply to This | # ]

A script to open current Finder folder in the Terminal
Authored by: wgscott on Nov 16, '03 05:14:33PM

I think I forgot to tell Gary about this previous hint:

http://www.macosxhints.com/article.php?story=20030915193326598



[ Reply to This | # ]
A script to open current Finder folder in the Terminal
Authored by: wgscott on Nov 16, '03 05:24:34PM
I should have added a clickable link of above

[ Reply to This | # ]
A script to open current Finder folder in the Terminal
Authored by: magnamous on Aug 18, '05 02:16:11PM
I recently found a similar script elsewhere which does the same thing (the attribution is in the script comments). The intriguing part of it was that the author had devised a very difficult test case for the script. The test case is a folder named te'st"ö te%s`t. In order to test whether a script works for this test case, you have to try it once when Terminal is not running, then again when Terminal is already running. I tried the test case on the two other scripts in this hint, and both failed.

Here's the script:

(* 

    Open Terminal Here 
     
    A toolbar script for Mac OS X 10.3/10.4
     
    Written by Marc Liyanage 

     
    See http://www.apple.com/applescript/macosx/toolbar_scripts/ for 
    more information about toolbar scripts. 
     
    See http://www.entropy.ch/software/applescript/ for the latest 
    version of this script. 
     
     
    History: 
    11-AUG-2005: Version 2.1.1 by magnamous. minor changes to process_item(this_item)
    21-MAR-2005: Version 2.1 by Will Norris. code cleanup and minor additions 
    18-AUG-2004: Version 2.0 by Allan Marcus. uses posix path    
    30-OCT-2001: Version 1.0, adapted from one of the example toolbar scripts 
    30-OCT-2001: Now handles embedded single quote characters in file names 
    30-OCT-2001: Now handles folders on volumes other than the startup volume 
    30-OCT-2001: Now handles click on icon in top-level (machine) window 
    31-OCT-2001: Now displays a nicer terminal window title, courtesy of Alain Content 
    11-NOV-2001: Now folders within application packages (.app directories) and has a new icon 
    12-NOV-2001: New properties to set terminal columns and rows as the Terminal does not use default settings 
    14-NOV-2001: Major change, now handles 8-bit characters in all shells, and quotes and spaces in tcsh 
    18-NOV-2001: Version 1.1: Rewrite, now uses a temporary file  ~/.OpenTerminalHere to communicate 
    the directory name between AppleScript and the shell because this is much more reliable for 8-bit characters 
     
 *)


property debug : true

-- when the toolbar script icon is clicked 
-- 
on run
	tell application "Finder"
		try
			set this_folder to (the target of the front window) as alias
		on error
			set this_folder to startup disk
		end try
		
		my process_item(this_folder)
		
	end tell
end run


-- This handler processes folders dropped onto the toolbar script icon 
-- 
on open these_items
	repeat with i from 1 to the count of these_items
		set this_item to item i of these_items
		my process_item(this_item)
	end repeat
end open


-- this subroutine processes does the actual work 
-- this version can handle this weirdo case: a folder named "te'st"ö te%s`t"

on process_item(this_item)
	
	set thePath to quoted form of POSIX path of this_item
	
	tell application "Terminal"
		-- just open a terminal and cd to thePath
		activate
		
		if window frontmost exists then
			
			tell window frontmost to activate
			--	do shell script "sleep .5"
			do script "cd " & thePath in window frontmost
			
		else
			
			tell application "System Events"
				keystroke "n" using command down
			end tell
			tell window frontmost to activate
			do shell script "sleep .5"
			do script "cd " & thePath in window frontmost
		end if
		
	end tell
	
end process_item


[ Reply to This | # ]
A script to open current Finder folder in the Terminal
Authored by: ScienceMan on Feb 18, '06 01:52:20PM

This doesn't work in 10.3.9 -- I just get a terminal in my home area, no matter where the window resides in which I use this. Same regardless of whether I use drag-and-drop onto the compiled script or put it in the toolbaf and click on it there.

Is there a reason for this, or an update? Will try it under 10.4.x also to see if the result is any different.



[ Reply to This | # ]