Copy paths from Finder selections
There may be times—such as when you’re trying to convey to someone where a file can be found on their machine, or maybe when you’re writing an OS X hints column—when you need to put the path to some file or folder into another document. In both OS X 10.4 and 10.5, it turns out there are many ways to do this. So short of just typing out the full path, what exactly are your options?
If you’d like to use a third-party solution, there are a number of contextual menu plug-ins available that add a “copy path to clipboard” feature to the Finder. Dan Frakes covered a couple of them in this Mac Gems entry. In addition, PathFinder offers path copying as a standard feature. This column, however, is all about doing things with what Apple provides, so here are some solutions that don’t require any third-party tools.
First, you can use the Services menu. In both 10.4 and 10.5, the Services menu works in the Finder. With the object whose path you’d like to capture selected in the Finder, choose Finder -> Services -> TextEdit -> New Window from Selection. A new TextEdit window will appear containing the full path to the selected item. Press Command-A to select it all, then Command-C to place it on the clipboard.
Another option is dragging the object from the Finder and dropping it into a TextEdit window (or a Terminal window). This will only work if your TextEdit document is in plain text mode—if you use rich text mode and drag in something TextEdit can open, such as a JPEG image file, the file will be opened. But with TextEdit set to plain text mode, you’ll instead see the path to the file or folder. Again, just select it and copy it to the clipboard, and you’re done. (Some third-party text editors support this feature as well; it works with Smultron on my machine, for instance.)
You can also copy any item (file or folder) in the Finder and paste it into Terminal. You’ll see the path on the command line; you can then select it with the mouse and copy it. You can also press Command-Space, then drag-and-drop the file into the Spotlight search box. Finally, you can drag a Finder window’s proxy icon—the small icon in the title bar of the window—into TextEdit (again, in plain text mode) to see the path to that folder. Select the path and copy it to your clipboard.
But what about one-step solutions? Is there any one-step method of getting the currently-selected item’s path from the Finder to the clipboard? While not quite a built-in solution, you can write a very simple AppleScript that will do this for you. Open Script Editor and enter these four lines:
tell application "Finder" set sel to the selection as text set the clipboard to POSIX path of sel end tell
Select File -> Save to bring up the Save dialog. Give your program a name (Get Path), save it somewhere safe (your user’s Documents folder), and set the File Format pop-up to Application, then click Save. Now navigate to the spot where you saved your four-line program, and drag it to the Dock or to a Finder’s sidebar or toolbar area. When you need the path to an item, first select that item, then click on your program’s Dock, sidebar, or toolbar icon. You’ll briefly see your program load into the dock, then quit. When it quits, switch to your target application and press Command-V to paste the path.
If you prefer, you can also easily make this AppleScript a Finder contextual menu plug-in. Launch Automator, create a blank workflow, and then drag the Automator -> Run AppleScript (Utilities -> Run AppleScript in 10.5) action to the work area. Replace the stock AppleScript text with the three-line script you see above, then choose File -> Save as Plug-in. In the new window that appears, give your plug-in a name (Copy Path to Clipboard), set the Plug-in For pop-up to Finder, and click Save. Switch back to the Finder, select something, Control-click on it, then select your new workflow from the list. Wait a second (watch the menu bar to see when the workflow has finished) and then switch to your target application and press Command-V.
I’m sure there are more ways to do this; these are just a few of the easier solutions; use whichever you prefer, obviously, including the third-party solutions that Dan covered in his write-up.