Skip to content

Instantly share code, notes, and snippets.

@RPing
Last active December 10, 2016 06:44
Show Gist options
  • Save RPing/a93c7c5629663ca28542bd5b09c59341 to your computer and use it in GitHub Desktop.
Save RPing/a93c7c5629663ca28542bd5b09c59341 to your computer and use it in GitHub Desktop.
Apple script to open an iterm2 window from right-clicking on a folder in Finder. To use:(1) Open Automator(2) Create a new service(3) Change "Service receives selected" drop downs to "Files or folders" in "Finder"(4) Select "Run applescript" from the sidebar, then paste this script in and save
-- Adapted from these sources:
-- http://peterdowns.com/posts/open-iterm-finder-service.html
-- https://gist.github.com/cowboy/905546
--
-- 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
set dir_path to quoted form of (POSIX path of my_file)
end tell
CD_to(dir_path)
end run
on CD_to(theDir)
tell application "iTerm"
activate
tell current window
set go_dir to "cd " & theDir
set newTab to (create tab with default profile)
tell newTab
select
tell current session of newTab
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