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


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: 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 | # ]