Recently, I stumbled on the Textmate droplet by Henrik Nyh allowing me to quickly open a file or a folder in Textmate by dragging it to the Finder toolbar. Since I had just setup iTerm2 and learned to script it with AppleScript, I figured I’d put the two together and create my own iTerm2 droplet. Since this was going to be my first droplet, I searched for some guidance and found a great article by Jarel Remick which covers the subject of Finder productivity and droplets in great depth.
Creating the droplet
A Finder droplet is really an AppleScript which interacts with the Finder in order to obtain either the path to the current directory or the selected file and with another app in order to launch it or interact with it. This AppleScript is then saved as an application which we will add to the Finder menu by a simple drag and drop.
My droplet is simple : it will get the path to the directory opened in the current Finder window. Launch iterm if itsn’t already running and cd into that directory.
So, here is the code behind my droplet :
tell application "Finder"
set currentPath to (POSIX path of (target of front window as alias))
tell application "iTerm"
tell the last terminal
launch session "Default"
tell the last session
set name to currentPath
write text "cd " & currentPath
Customizing the droplet
At this point, you can save your code as an application from the AppleScriptEditor and drag the resulting file to the Finder toolbar. Click it, and the current folder is opened in iTerm.
Still, before adding this to my Finder’s toolbar, I customized it with a little terminal icon. Mac OS really make this simple. Simply find an icon you like and open it using Preview. Copy the image to clipboard (CTRL-C). Then, select the Application you just created with AppleScriptEditor and right click to display the file info (or use CTRL-I). Select the icon at the top left of the window (it will be selected in blue) and paste your image (CTRL-V).
That’s it. This step was surprisingly simple.
Installing the droplet
Nothing complicated in this step : simply drag your application (or droplet) to the Finder’s toolbar. Click it, iTerm will open and your pwd will be set to the current Finder’s window directory. Handy.