Skip to content

Instantly share code, notes, and snippets.

@jooeycheng
Created December 10, 2016 07:48
Show Gist options
  • Save jooeycheng/3af0fe02160a8ff70e6a6791947637c6 to your computer and use it in GitHub Desktop.
Save jooeycheng/3af0fe02160a8ff70e6a6791947637c6 to your computer and use it in GitHub Desktop.
Open in iTerm AppleScript
-- Adapted from:
-- https://gist.github.com/eric-hu/5846890
--
-- Modified to work with files as well, cd-ing to their container folder
on run {input, parameters}
tell application "Finder"
set my_file to first item of input
set filetype to (kind of (info for my_file))
-- Treats OS X applications as files. To treat them as folders, integrate this SO answer:
-- http://stackoverflow.com/a/6881524/640517
if filetype is "Folder" or filetype is "Volume" then
set dir_path to quoted form of (POSIX path of my_file)
else
set dir_path to quoted form of (POSIX path of (container of my_file as string))
end if
end tell
CD_to(dir_path)
end run
on CD_to(theDir)
tell application "iTerm"
activate
set go_dir to "cd " & theDir
try
set currentWindow to the last window
on error
set currentWindow to (create window with default profile)
end try
tell current window
-- These commands return a tab
set newTab to (create tab with default profile)
tell newTab
select
tell current session of currentWindow
write text go_dir
end tell
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