Last active
February 2, 2017 21:48
-
-
Save wmertens/dd05073004d1ead2f6fa71e85108579a to your computer and use it in GitHub Desktop.
Open a new iTerm window (https://iterm2.com) in the current application folder via Applescript
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
on run | |
-- Figure out if we want to do the cd (doIt) | |
-- Figure out what the path is and quote it (myPath) | |
set doIt to false | |
try | |
tell application "Finder" to set doIt to frontmost | |
set myPath to finder_path() | |
if myPath is equal to "" then | |
set doIt to false | |
else | |
set myPath to quote_for_bash(myPath) | |
end if | |
on error | |
set doIt to false | |
end try | |
if false and not doIt then | |
try | |
set myPath to path of document of front window | |
if myPath is not equal to "" then | |
set myPath to quote_for_bash(myPath) | |
set doIt to true | |
end if | |
end try | |
end if | |
if not doIt then | |
try | |
set n to name of front window | |
display alert n | |
set o to offset of "/" in n | |
if o is not equal to 0 then | |
set myPath to text o thru end of n | |
set myPath to quote_for_bash(myPath) | |
set doIt to true | |
end if | |
end try | |
end if | |
tell application "iTerm" | |
set mylittlewindow to create window with default profile | |
if doIt then | |
write current session of mylittlewindow text "cd " & myPath | |
end if | |
end tell | |
end run | |
on finder_path() | |
try | |
tell application "Finder" to set the source_folder to (folder of the front window) as alias | |
set thePath to (POSIX path of the source_folder as string) | |
on error -- no open folder windows | |
set thePath to "" | |
end try | |
return thePath | |
end finder_path | |
-- This simply quotes all occurrences of ' and puts the whole thing between 's | |
on quote_for_bash(theString) | |
set oldDelims to AppleScript's text item delimiters | |
set AppleScript's text item delimiters to "'" | |
set the parsedList to every text item of theString | |
set AppleScript's text item delimiters to "'\\''" | |
set theString to the parsedList as string | |
set AppleScript's text item delimiters to oldDelims | |
return "'" & theString & "'" | |
end quote_for_bash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that github doesn't send notification for gists so if you want to tell me something send me email at [email protected]