Last active
February 26, 2016 17:31
-
-
Save jellyjellyrobot/6bfa34c48e7461af698d to your computer and use it in GitHub Desktop.
new iterm has different api. updated the code to reflect that
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Adapted from canuckistani/openItermHere.scpt | |
-- https://iterm2.com/applescript.html <- new api | |
-- 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 current window | |
try | |
-- launch a default shell in a new tab in the same terminal | |
set newTab to (create tab with default profile) | |
on error | |
display dialog "There was an error creating a new tab in iTerm." buttons {"OK"} | |
end try | |
tell current session | |
try | |
-- cd to the finder window | |
write text "cd " & theDir & " && clear" | |
on error | |
-- display dialog "There was an error cding to the finder window." buttons {"OK"} | |
set newWindow to (create window with default profile) | |
write text "cd " & theDir & " && clear" | |
--tell current window | |
-- tell current session | |
-- end tell | |
--end tell | |
end try | |
end tell | |
end tell | |
end tell | |
end CD_to |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment