Create a gist now

Instantly share code, notes, and snippets.

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:
-- 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:
if filetype is "Folder" or filetype is "Volume" then
set dir_path to quoted form of (POSIX path of my_file)
set dir_path to quoted form of (POSIX path of (container of my_file as string))
end if
end tell
end run
on CD_to(theDir)
tell application "iTerm"
set t to the last terminal
on error
set t to (make new terminal)
end try
tell t
launch session "Default Session"
tell the last session
write text "cd " & theDir
write text "ls"
end tell
end tell
end tell
end CD_to

How could this be altered to allow the menu item to show when white space is clicked?


I second zakdances' request, that would be awesome.


It has always annoyed me that you don't get "Services" in the right click menu when your target is the background of the window... I mean it should technically be a valid target for a service that receives folders... alas nothing can be done other than send apple feedback requesting it

delgadom commented May 2, 2014

Not quite a solution to zakdances question, but you can do a shortcut instead...
Instead of using "service receives selected" choose "no input" in "finder", then add the "Get selected Finder Items" action from the developer tab and place it above this script. Since the action then does not require arguments it now works with keyboard shortcuts. To set it go to system preferences > keyboard > shortcuts > services and find your script under "General". you should be able to set a shortcut and have it work on any file or folder in finder.
thanks for the post eric-hu! I've been looking for this solution for a long time!

eric-hu commented May 16, 2016

@delgadom -- Thanks for sharing that addition. I now have both the Service above and another keyboard shortcut service.

For people who are setting up the keyboard shortcut, note that once I had it set up, the keyboard shortcut wasn't working until I went into finder, clicked File -> Services -> (my new keyboard shortcut). After that, my keyboard shortcut worked. Also note that some OS X hotkeys can't be easily disabled (I wanted to override iCloud's default Cmd + Shift + i. I settled for ctrl-i).

Leglaw commented May 24, 2016

For applescript that works with iTerm2 nightly this fork works:

craigcosmo commented Dec 14, 2016 edited

using the code provided by OP. I got this error Expected class name but found identifier.

kitizz commented Dec 17, 2016

I'm getting the same error as craigcosmo

ttimasdf commented Dec 20, 2016 edited

Here's my working fork, tested against latest iTerm2.

mainly changed:

  • detect file type by shell script, fix the problem in fork by @Leglaw that non-English (e.g. Chinese) locale may encounter.
  • create new tab if existing window is detected, instead of always creating new windows.

iTerm updated their interface. For those who keep getting error, have a try with it~ @craigcosmo @kitizz


It's working for mw now @ttimasdf Thanks for the correction

to-kra commented Mar 1, 2017

@ttimasdf not working for me, im getting this:

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