Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
-- 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 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"
activate
set go_dir to "cd " & theDir
try
set newWindow to current window
on error
set newWindow to (create window with default profile)
end try
tell newWindow
tell current session of (create tab with default profile)
write text go_dir
end tell
end tell
end tell
end CD_to
@tasdikrahman

This comment has been minimized.

Copy link

tasdikrahman commented Jan 11, 2017

I am getting an error saying variable my_file is not defined

@suxiaogang

This comment has been minimized.

Copy link

suxiaogang commented Feb 3, 2017

+1 variable my_file is not defined

@SenorWesley

This comment has been minimized.

Copy link

SenorWesley commented Feb 4, 2017

Just save it as a service in automator without trying to run it there. When ran from finder it works perfectly.

@PulpSpy

This comment has been minimized.

Copy link

PulpSpy commented Feb 15, 2017

This is great, thank you. It gives an error for me if you close (but do not quit) iTerm and run the script. But it works if a window is open or if the app has been quit.

@kamontat

This comment has been minimized.

Copy link

kamontat commented Feb 25, 2017

My fork.
I try to fix the error, can you test it, and tell me is the bug is disappear?

@tokra

This comment has been minimized.

Copy link

tokra commented Mar 1, 2017

image

@you-fail-me

This comment has been minimized.

Copy link

you-fail-me commented Mar 3, 2017

Guys, is there a possibility to make it also drop down the iterm hotkey window when it's run? For me it really opens a new tab with that folder but iterm stays unrevealed and I have to drop it down on my own, with alt + space

@lamba

This comment has been minimized.

Copy link

lamba commented Mar 11, 2017

Works! Thanks.

@tkcranny

This comment has been minimized.

Copy link

tkcranny commented Apr 7, 2017

I had some trouble with this service not handling new window instances very gracefully; it would crash if iTerm was already open.
I've made some slight modifications here, that opens only one tab if iTerm wasn't running before, or otherwise makes a new tab if iTerm already has some running sessions.

@nw4869

This comment has been minimized.

Copy link

nw4869 commented Nov 6, 2017

Works! Thanks.

@ttimasdf

This comment has been minimized.

Copy link
Owner Author

ttimasdf commented Feb 24, 2018

Sorry, comments in Gists does not received any notifications.

For those who still receiving errors(this script WFM for years till now, 10.13), follow the instruction in the descriptions, most importantly change "Service receives selected" drop downs to "Files or folders" in "Finder" so that my_file variable would correctly set by right click on finder item.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.