Skip to content

Instantly share code, notes, and snippets.

@Softsapiens
Forked from tkcranny/Open in iTerm.workflow
Created June 22, 2017 09:40
Show Gist options
  • Save Softsapiens/7b8db56c38f743a3c9c3c28b6d73f640 to your computer and use it in GitHub Desktop.
Save Softsapiens/7b8db56c38f743a3c9c3c28b6d73f640 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment