Skip to content

Instantly share code, notes, and snippets.

@gAmUssA
Forked from eric-hu/Open iterm tab here
Last active October 28, 2023 19:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gAmUssA/0729ac596c29c40b580d to your computer and use it in GitHub Desktop.
Save gAmUssA/0729ac596c29c40b580d to your computer and use it in GitHub Desktop.
Apple script to open an iterm2 tab from right-clicking on a file or 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
-- http://peterdowns.com/posts/open-iterm-finder-service.html see Application
on run {input, parameters}
tell application "Finder"
set dir_path to quoted form of (POSIX path of (folder of the front window as alias))
end tell
CD_to(dir_path)
end run
on CD_to(theDir)
tell application "iTerm"
activate
tell current window
set newTab to (create tab with default profile)
select
tell current session of current tab
write text "cd " & theDir & "; clear; pwd"
end tell
end tell
end tell
end CD_to
-- 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
-- Modified to work with iTerm 2 night builds https://iterm2.com/applescript.html
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
tell current window
set newTab to (create tab with default profile)
select
tell current session of current tab
write text "cd " & theDir & "; clear; pwd"
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