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


Click here to return to the 'Create a command to 'cd' to frontmost Finder window' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Create a command to 'cd' to frontmost Finder window
Authored by: szedula on Oct 05, '05 02:40:44PM
I have this script in the Finder window toolbar. When clicked, it will open a new terminal window (if none is open) and change the directory. If one or more files/folders are draged to the icon, terminal windows are opened and the directories changed to the folder or the folder containing the file. I wrote/modified this from this hint and previously posted hints.

-- script was opened by click in toolbar
on run
   tell application "Finder"
      try
         set currFolder to (folder of the front window as string)
      on error
         set currFolder to (path to desktop folder as string)
      end try
   end tell
   CD_to(currFolder, false)
end run

-- script run by draging file/folder to icon
on open (theList)
   set newWindow to false
   repeat with thePath in theList
      set thePath to thePath as string
      if not (thePath ends with ":") then
         set x to the offset of ":" in (the reverse of every character of thePath) as string
         set thePath to (characters 1 thru -(x) of thePath) as string
      end if
      CD_to(thePath, newWindow)
      set newWindow to true -- create window for any other files/folders
   end repeat
   return
end open

-- cd to the desired directory in terminal
on CD_to(theDir, newWindow)
   set theDir to quoted form of POSIX path of theDir as string
   tell application "Terminal"
      activate
      if ((count of the window) = 0) or (the busy of window 1) or newWindow then
         tell application "System Events"
            keystroke "n" using command down
            delay 1 -- sometimes "cd" is lost
         end tell
      end if
      do script "cd " & theDir in window 1
   end tell
end CD_to


[ Reply to This | # ]
Create a command to 'cd' to frontmost Finder window
Authored by: Blah on Oct 07, '05 11:25:16PM

Sa-weeet!

Very nice job. As a long-time Mac developer, I've only recently started to take advantage of using the Finder's toolbar area for scripts (and can't understand why it's taken me this long to give it a try). As far as this script goes, I guess one might want to open a -new- terminal session at times, but then again, not always. Just thinking out loud....

In any case, this is a keeper, thanks.



[ Reply to This | # ]
Create a command to 'cd' to frontmost Finder window
Authored by: szedula on Oct 08, '05 05:13:24AM

If you'd prefer that as the default response, you can easily get that by changing the "false" argument in calls to "CD_to" to "true".



[ Reply to This | # ]
Create a command to 'cd' to frontmost Finder window
Authored by: suarez on Oct 09, '05 11:26:30AM
One problem with using osascript -e is that it's a tiny bit slow. I know, most people won't notice the speed difference, especially when you see the timing results below. What I use instead is:
tell application "Finder"
   try
      POSIX path of (target of window 1 as alias)
   on error
      POSIX path of (path to desktop folder as alias)
   end try
end tell
and save that as a script in my ~/bin folder. Then, in my .bashrc file, I alias cdf to use the script with:
alias cdf='cd "`osascript ~/bin/cdf.scpt`"'
(That's back-tick, double-quote, and single-quote, from inside to out.) Rather than have the 2>/dev/null code ignore the error message, I prefer to go to the desktop if there's no open Finder window. I tried the original solution, and it ran a little slower (by a mean of 85 ms over 10 trials) than this one. I thought I had noticed a little difference, so that's why I timed it.
This, combined with the TerminalHere contextual menu, satisfies my wish-list. There's another AppleScript floating around that does the opposite of this -- opens the current folder in Finder, but this works just fine for me and it's fast:
$ open .


[ Reply to This | # ]
Create a command to 'cd' to frontmost Finder window
Authored by: SimonDorfman.com on Dec 04, '05 01:20:41AM
very cool script! I altered that last section to open a new tab in iTerm instead of Terminal:

-- script was opened by click in toolbar
on run
	tell application "Finder"
		try
			set currFolder to (folder of the front window as string)
		on error
			set currFolder to (path to desktop folder as string)
		end try
	end tell
	CD_to(currFolder, false)
end run

-- script run by draging file/folder to icon
on open (theList)
	set newWindow to false
	repeat with thePath in theList
		set thePath to thePath as string
		if not (thePath ends with ":") then
			set x to the offset of ":" in (the reverse of every character of thePath) as string
			set thePath to (characters 1 thru -(x) of thePath) as string
		end if
		CD_to(thePath, newWindow)
		set newWindow to true -- create window for any other files/folders
	end repeat
	return
end open

-- cd to the desired directory in iterm
on CD_to(theDir, newWindow)
	set theDir to quoted form of POSIX path of theDir as string
	tell application "iTerm"
		activate
		-- talk to the first terminal 
		tell the first terminal
			try
				-- launch a default shell in a new tab in the same terminal 
				launch session "Default Session"
			on error
				display dialog "There was an error creating a new tab in iTerm." buttons {"OK"}
			end try
			tell the last session
				try
					-- cd to the finder window
					write text "cd " & theDir
				on error
					display dialog "There was an error cding to the finder window." buttons {"OK"}
				end try
			end tell
		end tell
	end tell
end CD_to


[ Reply to This | # ]