2014/05/17

Open Finder window from Terminal directory; a better way


In Mac OS X's Terminal app, it's quite common (for me at least) to be working away in a directory and want to then view that folder in the Finder; whether to browse around or attach a file or what have you.

This is easily done with ‘open .’, which takes the current directory (.) and opens it using Mac OS X's default application for that filetype (in this case, Finder for a folder).

However, after doing this a few times you end up with multiple windows open in the Finder, and it's all rather cluttered.

I attempt to only have three Finder windows open at any one time: two view-by-columns browsing windows, and one view-as-list Downloads folder (it's a bit of a dumping ground). So when I want to view the current directory from Terminal, it'd be better if I could just change the current view of one of my windows to where I'd like to go.

AppleScript is your friend for any such task, and while I could do more to make this fancy (like switch away from the Downloads folder if necessary, etc.) here's a shell script (I call it fin) to do exactly that:

#!/bin/bash

osascript -e "tell application \"Finder\" to activate" \
  -e "tell application \"Finder\" to set the folder of the front window to POSIX file \"`pwd`\" " > /dev/null

Save this as a text file in ~/bin/ (which might need to be added to your path by default; no longer sure), then hit chmod +x fin (assuming you call it fin like me) and your new command fin is ready to go.

Better.