# TextExpander, POSIX paths, and forgetfulness

It’s not uncommon for me to be working in the Terminal and want to insert the directory path of the topmost Finder window into a command. Maybe I want to cd into it; maybe I want to copy a file to it from the directory I’m currently working in. Whatever the reason, it comes up fairly often. You can, of course, drag the little icon in the Finder window’s titlebar into the Terminal window to insert the path, but that means switching apps and moving your hand to the mouse or trackpad. I’d rather not do that.

On Friday afternoon, I found myself doing this three or four times and got fed up with it. This, I thought, is a perfect spot for an AppleScript-powered TextExpander snippet. So I opened up the AppleScript Editor and began experimenting. I soon had a one-liner that returned the quoted path to the directory shown in the topmost Finder window:

applescript:
tell application "Finder" to get quoted form of POSIX path of (target of front Finder window as text)


The key is to say that you want the target of the front Finder window and convert it to text. That returns an Apple-style, colon-separated path, which can be changed into a Unix-style, slash-separated path with the POSIX path of operator.

I copied the script from the AppleScript Editor window and opened TextExpander to make a new snippet. This is what I found:

I’d already made the snippet and given it a pretty reasonable abbreviation of ;dir. While it was nice to see that I came up with almost the same script that I had before,1 it would have been even nicer to have remembered that I’d already written it.

How long ago did I write this snippet? I searched through the blog and couldn’t find it. Did I copy it from someone else? A Google search found this page of tweets favorited by Kevin Clark (@ultgames). And the tweet he favorited that had the snippet’s code in it was mine:

Helpful (to me) AppleScript @TextExpander snippet:

tell application "Finder" to get POSIX path of (target of front Finder window as text)

Dr. Drang (@drdrang) Thu Sep 1 2011 1:00 PM CDT

So apparently I didn’t copy it. Or if I did, I forgot who I copied it from.

My guess is that I came up with the snippet after a day like Friday where I kept needing to insert the path to the current Finder window. And I probably forgot about it because I didn’t need it again until weeks or months had passed. Or I just forgot about it because that’s what I do. With luck, writing this little post will help me remember it the next time I need it.

1. The new one was better because it quoted the path, avoiding problems with spaces and other characters that the shell treats as special.