Home » Questions » Computers [ Ask a new question ]

MacOS X: How to have a handy "Open this folder in iTerm" shortcut?

MacOS X: How to have a handy "Open this folder in iTerm" shortcut?

I think the title states exactly what I want to do. I want a shortcut or even a button within Finder which fires up a new iTerm Tab and changes the location to the location I've open in Finder. Some sort of open . in reverse.

Asked by: Guest | Views: 365
Total answers/comments: 5
Guest [Entry]

"There is an Open Terminal Here AppleScript that you should be able to modify to call iTerm instead. This MacOSXHints post should be helpful as well.

(I'm not on my Mac otherwise I would test it.)"
Guest [Entry]

"Using the other answers on this page I have created an App that can be dragged into the finder task bar.

You can download it from here: github.com/rc1/iTermTo"
Guest [Entry]

"Just for completeness, before finding this question what worked for me was:

adapted new_tab.sh (AppleScript issued by bash script) to an AppleScript-only solution.
then from the Applescript Editor-> File-> Export-> File Format = .app.
drag & dropped the .app to Finder's toolbar.

This results in a Finder toolbar button which opens the current directory in new iTerm2 tab.
XtraFinder offers such a button, but it opens new windows.

A similar solution using services can be found here, which links to even more related AppleScript solutions:

Open iTerm tab here,
Open iTerm in current folder,
Finder open iTerm here,
Open Current Finder Window In New ITerm Tab (notably from 2005).

My adapted AppleScript is:

try
tell application ""iTerm2""
tell the last terminal
launch session ""Default Session""
tell the last session
tell i term application ""Finder""
set cur_dir to (the target of the front Finder window) as string
end tell
set cur_dir to POSIX path of cur_dir
write text ""cd "" & cur_dir
end tell
end tell
end tell
end try

This solution was commented in this button-related thread.

Thanks to the iTermTo answer above."
Guest [Entry]

"I guess it is because the internals of iTerm has changed, but none of the solutions worked for me. What did was the following code:

tell application ""Finder""
set cur_dir to POSIX path of ((the target of the front Finder window) as string)
end tell
tell application ""iTerm""
tell (create window with default profile)
write current session text ""cd "" & quoted form of cur_dir
end tell
end tell

Or using Automator as a finder service:

on run {input, parameters}
tell application ""Finder""
set cur_dir to POSIX path of (input as string)
end tell
tell application ""iTerm""
tell (create window with default profile)
write current session text ""cd "" & quoted form of cur_dir
end tell
end tell
end run"
Guest [Entry]

"With iTerm:

Iterm Preferences and Under Profiles tab, go to General subtab, set Working Directory to “Reuse previous session’s directory”."