Skip to content

Instantly share code, notes, and snippets.

@tkcranny
Last active August 29, 2019 05:09
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save tkcranny/1d366cc1a3507d1abe2012c5da7744c7 to your computer and use it in GitHub Desktop.
Save tkcranny/1d366cc1a3507d1abe2012c5da7744c7 to your computer and use it in GitHub Desktop.
macOS Sierra Automater service to open iTerm2 from a finder window
-- Adapted from these sources:
-- http://peterdowns.com/posts/open-iterm-finder-service.html
-- https://gist.github.com/cowboy/905546
-- https://gist.github.com/ttimasdf/7bb02ed419db4b472b534e1a57008a3b
--
-- 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 is_folder to (do shell script "file -b " & quoted form of (POSIX path of my_file))
if is_folder ends with "directory" 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"
set go_dir to "cd " & theDir
set ctrlL to character id 12
-- Count windows before launching, to determine if to make a new session.
set startCount to (count of windows)
activate
-- Make a new window if iTerm wasn't open.
if startCount = 0 then
create window with default profile
end if
tell current window
-- Create a new tab if a window is already open, otherwise use newly opened one.
if startCount > 0 then
create tab with default profile
end if
tell current session of current tab
write text go_dir
write text ctrlL newline no -- Clear the terminal after loading.
end tell
end tell
end tell
end CD_to
@TCalkhoven
Copy link

It works perfectly for me. Thank you!

@Reapor-Yurnero
Copy link

Work perfectly so far on my Sierra and 2.3.4 iterm2!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment