# 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.

## 7 Responses to “TextExpander, POSIX paths, and forgetfulness”

1. Wow, this is great. Thanks! I find myself needing to do the same all the time, but I didn’t know about the trick of dragging the icon from the title bar into the Terminal window.

I think this is what will finally convince me to purchase TextExpander. Thanks again for the script!

2. I am but a simple user of Quicksilver and a downright newb at AppleScript, and seized your script to do this:

try tell application “Finder” to get quoted form of POSIX path of (target of front Finder window as text) tell application “Finder” to set the clipboard to the result end try

Now I hit Command twice (my years old Quicksilver trigger) and type Path, then return. Then sitting in my clipboard, where I like it, is your well constructed path. Plus it doesn’t go wrong if I trigger it without any Finder windows open by mistake.

Thanks Doc.

3. John says:

You’re the inverse of a politician. Appreciate the useful snippet, again.

4. Clark says:

I put up a few of my bash_profile functions that do something similar. It’s not a macro and I can see the appeal of that. But since 90% of the time I’m just cding to the directory this works well. I have a cdf command that goes to the front Finder window’s directory. I have a selected command that passes the selected icons. (With some caveats due to a bug of Apple’s - see the comments)

5. Wow, fantastic. Really useful. Thanks, Dr Drang!

6. Neal Lippman says:

Great idea; very handy. It’s amazing how useful little time savers like this can be!

I modified your script just a bit, because as written if there are no Finder windows open, TextExpander actually inserts the entire tell command, so I am using:

tell application “Finder” if (the count of windows) is equal to 0 then tell application “System Events” to beep return “” else return the quoted form of POSIX path of (target of front window as text) end end tell

I haven’t played around with dealing with what happens if you use multiple desktops (or spaces, I cannot remember what Mountain Lion is calling them these days) and you have Finder windows open in more than one of them. Do you want the inserted folder to be the top of all Finder windows, or the top of the windows on the current desktop? I’m not yet sure how to use AppleScript to figure that out, anyway.

7. Under System Preferences → Keyboard → Keyboard Shortcuts → Services → Files and Folders, there are a couple of options, viz., New Terminal at Folder and _New Terminal Tab at Folder_ (only in Montain Lion, as I understand). When enabled, they appear in the context menu. Either on its own, or in combination with your script (thanks!), this seems to work well for me.

In zsh shell, together with steeef theme, I have stopped using cd as a command. Instead, I just type Documents and press enter, and it does cd Documents in Terminal if a Documents folder does exist.