Finder and Terminal are Friends

January 10, 2017

As developers, we tend to spend a lot of time typing in Terminal windows. Even so, I often find it more helpful to browse directories and files in Finder. I have three little hacks that simplify moving between the two modes.

pwdf

The first hack is a shell function that logs the path of the directory shown by the front-most Finder window.

# pwdf: echoes path of front-most window of Finder
pwdf ()
{ 
   currFolderPath=$( /usr/bin/osascript <<"   EOT"
       tell application "Finder"
           try
               set currFolder to (folder of the front window as alias)
           on error
               set currFolder to (path to desktop folder as alias)
           end try
           POSIX path of currFolder
       end tell
   EOT
   )
   echo "$currFolderPath"
}

I picked up the basic gist of this from a Mac OS X Hints post by Cameron Hayes in the long ago. A much belated thank you to Cameron.

I use zsh as my shell, so this function is declared in my ~/.zshrc. (I think the same function declaration would work in .bashrc, but it’s been six years since I used bash, so please correct me if I’m wrong.)

Add this function declaration and restart Terminal. Then open a Finder window, switch to Terminal, and type pwdf. The path to the Finder window’s folder will be logged:

curt@Fozzie-Bear% pwdf
/Users/curt/Documents/SourceDirs/curtclifton.net/

Where this gets really useful is wrapping the command in backticks to embed the result in other commands. For example, we can count the number of lines in all Swift files in the current Finder window:

curt@Fozzie-Bear% wc -l "`pwdf`"*.swift
      48 /Users/curt/Documents/SourceDirs/curtclifton.net/BlogGenApp/BlogGenApp/AppDelegate.swift
     170 /Users/curt/Documents/SourceDirs/curtclifton.net/BlogGenApp/BlogGenApp/BlogGenModel.swift
     488 /Users/curt/Documents/SourceDirs/curtclifton.net/BlogGenApp/BlogGenApp/SwiftyUserDefaults.swift
     177 /Users/curt/Documents/SourceDirs/curtclifton.net/BlogGenApp/BlogGenApp/ViewController.swift
      22 /Users/curt/Documents/SourceDirs/curtclifton.net/BlogGenApp/BlogGenApp/main.swift
     905 total

cdf

One use of pwdf comes up enough that I created an alias.

alias cdf='cd "`pwdf`"'

Add this to your .zshrc, being careful to get all the quotes and ticks leaning the right directions. Restart Terminal and then type cdf at a prompt to change your working directory to that of the front-most Finder window. I find this super handy when I’ve browsed through directories in a Finder window and want to work some Terminal magic on the files therein. Just ⌘-Tab to Terminal, type cdf, and go to town.

open .

Sometimes I want to move in the opposite direction, opening a Finder window on the directory that I’m working within in Terminal. The built-in Mac system command open solves this problem. If you pass a directory as the argument to open, it opens a Finder window. So, to start browsing your current working directory just:

curt@Fozzie-Bear% open .

Sometimes little hacks like these can make a big difference over time. And I get a real kick out of the fact that pwdf combines zsh and AppleScript.

What are your favorite little hacks? What do you like about them?