Note that, to be able to use custom workflow in Alfred, you need to purchase Alfred Powerpack. Otherwise this post may not be helpful to you.

If you are a developer using macOS you are probably using iTerm2 as your main Terminal. Won’t it be nice if you can launch iTerm2 and automatically cd into the current Finder directory?

Well there are some existing tools for this, but these solutions require you to drag and drop the folder into an AppleScript or right click on the folder. But I don’t want to move my fingers away from keyboard, which affects my productivity. As a heavy user of Alfred.app, I think it would be great if I could do this with Alfred.

I started by writing an AppleScript that gets the full path of current directory in Finder.app and cd into it in iTerm2.app (modified from this StackOverflow question).

1
2
3
4
5
6
7
8
9
10
11
tell application "Finder"
set dir to (target of front Finder window) as text
end tell
tell application "iTerm2"
activate
set w to current window
set t to create tab with default profile w
set s to get the current session of the t
write s text "cd " & (quoted form of POSIX path of dir)
end tell

The script obtains the path to directory of the top most Finder window (in case you are using TotalFinder which supports multiple tabs in a window, this will be the directory of the top most tab). Then it creates a new tab in iTerm2 and cd to the path.

And the rest are easy. I just created a new blank workflow in Alfred, set the keyword input and configured it to trigger the AppleScript shown above.

After setting up some meta data and icon, the workflow is good to go.

In case you need it, here‘s the workflow file,which you can import to your Alfred.app directly.