DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Open Current Finder Window In New ITerm Tab

12.11.2005
| 20350 views |
  • submit to reddit
        // cd to the current finder window folder in iTerm. Or drag a folder onto this script to cd to that folder in iTerm.

-- cd to the current finder window folder in iTerm. Or drag a folder onto this script to cd to that folder in iTerm.
-- found this script in the comments of this article: http://www.macosxhints.com/article.php?story=20050924210643297

-- Instructions for use:
-- paste this script into Script Editor and save as an application to ~/Library/Scripts/Applications/Finder/cd to in iTerm
-- run via the AppleScript Menu item (http://www.apple.com/applescript/scriptmenu/)
-- Or better yet, Control-click and drag it to the top of a finder window so it appears in every finder window.
-- Activate it by clicking on it or dragging a folder onto it.

-- Another nice touch is to give the saved script the same icon as iTerm.
-- To do this, in the finder, Get info (Command-I) of both iTerm and this saved script.
-- Click the iTerm icon (it will highlight blue) and copy it by pressing Comand-C.
-- Click on this script's icon and paste by pressing Command-V.

-- Another way to give it the same icon as iTerm is to save the script as an application bundle (instead of an application),
--  then copy the icon by entering these commands in iTerm:
-- $ cd ~/Library/Scripts/Applications/Finder/cd\ to\ in\ iTerm.app/Contents/Resources/
-- $ rm droplet.icns
-- $ cp /Applications/iTerm.app/Contents/Resources/iTerm.icns droplet.icns
-- $ touch ~/Library/Scripts/Applications/Finder/cd\ to\ in\ iTerm.app

-- 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
		delay 1
		-- 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
    

Comments

Snippets Manager replied on Mon, 2012/03/05 - 3:46am

This script only works if you have an open iTerm window. If you want to open an new window, if there isn't an open one apply the following changes to the script: Legend Trail

Snippets Manager replied on Mon, 2012/03/05 - 3:46am

Then it doesn't matter what the filename has in it :-)professional resume writing services

Snippets Manager replied on Wed, 2009/09/09 - 12:47pm

Thanks for such a nice code i am now short to it use, i was looking some good things on Open current Finder window in new iTerm tab and now i can manage it easily, i was not much in programming codes because previously i was dealing with free hosting services along with the cheap packages of cheap domain hosting services which are quite reliable and easy to use before choosing this i have read many hosting reviews so i can choose what i want and can get more benefits. i like your work too and will be here again as i get some time because you are doing really nice job.

Snippets Manager replied on Sun, 2007/10/28 - 7:27am

This script only works if you have an open iTerm window. If you want to open an new window, if there isn't an open one apply the following changes to the script: -- replace this line: tell the first terminal -- with this lines: try set myterm to the first terminal on error set myterm to (make new terminal) end try tell myterm

Snippets Manager replied on Mon, 2012/05/07 - 2:17pm

Thanks for the good Apple themed snippets, we are really short of them.