// cd to the current finder window folder in iTerm. Or drag a folder onto this script to cd to that folder in iTerm.
1
2 -- cd to the current finder window folder in iTerm. Or drag a folder onto this script to cd to that folder in iTerm.
3 -- found this script in the comments of this article: http://www.macosxhints.com/article.php?story=20050924210643297
4
5 -- Instructions for use:
6 -- paste this script into Script Editor and save as an application to ~/Library/Scripts/Applications/Finder/cd to in iTerm
7 -- run via the AppleScript Menu item (http://www.apple.com/applescript/scriptmenu/)
8 -- Or better yet, Control-click and drag it to the top of a finder window so it appears in every finder window.
9 -- Activate it by clicking on it or dragging a folder onto it.
10
11 -- Another nice touch is to give the saved script the same icon as iTerm.
12 -- To do this, in the finder, Get info (Command-I) of both iTerm and this saved script.
13 -- Click the iTerm icon (it will highlight blue) and copy it by pressing Comand-C.
14 -- Click on this script's icon and paste by pressing Command-V.
15
16 -- Another way to give it the same icon as iTerm is to save the script as an application bundle (instead of an application),
17 -- then copy the icon by entering these commands in iTerm:
18 -- $ cd ~/Library/Scripts/Applications/Finder/cd\ to\ in\ iTerm.app/Contents/Resources/
19 -- $ rm droplet.icns
20 -- $ cp /Applications/iTerm.app/Contents/Resources/iTerm.icns droplet.icns
21 -- $ touch ~/Library/Scripts/Applications/Finder/cd\ to\ in\ iTerm.app
22
23 -- script was opened by click in toolbar
24 on run
25 tell application "Finder"
26 try
27 set currFolder to (folder of the front window as string)
28 on error
29 set currFolder to (path to desktop folder as string)
30 end try
31 end tell
32 CD_to(currFolder, false)
33 end run
34
35 -- script run by draging file/folder to icon
36 on open (theList)
37 set newWindow to false
38 repeat with thePath in theList
39 set thePath to thePath as string
40 if not (thePath ends with ":") then
41 set x to the offset of ":" in (the reverse of every character of thePath) as string
42 set thePath to (characters 1 thru -(x) of thePath) as string
43 end if
44 CD_to(thePath, newWindow)
45 set newWindow to true -- create window for any other files/folders
46 end repeat
47 return
48 end open
49
50 -- cd to the desired directory in iterm
51 on CD_to(theDir, newWindow)
52 set theDir to quoted form of POSIX path of theDir as string
53 tell application "iTerm"
54 activate
55 delay 1
56 -- talk to the first terminal
57 tell the first terminal
58 try
59 -- launch a default shell in a new tab in the same terminal
60 launch session "Default Session"
61 on error
62 display dialog "There was an error creating a new tab in iTerm." buttons {"OK"}
63 end try
64 tell the last session
65 try
66 -- cd to the finder window
67 write text "cd " & theDir
68 on error
69 display dialog "There was an error cding to the finder window." buttons {"OK"}
70 end try
71 end tell
72 end tell
73 end tell
74 end CD_to