AppleScript Finder FAQ: How do I get the full Mac OS X Finder path in an AppleScript script?
Here’s the source code for an AppleScript Finder program I wrote a long time ago. (Full disclosure: I created this script so long ago I probably got at least a portion of it from a Mac OS X or AppleScript book.) If you run this program while the Finder is open, it gets the full current path to your location in the Finder, and shows that in a dialog. The dialog shows a button labeled "Clipboard" that lets you send that path directly to the clipboard, so you can paste it into any other application.
For example, if I've used the Finder to navigate to a folder named /Users/al/foo/bar
, the only thing I'll see in the Finder is the name bar
at the top of the Finder window. But if I then run this script and click the Clipboard button, that full path (/Users/al/foo/bar
) is copied to the clipboard, and I can then paste that location into TextMate or any other application.
Here's the complete AppleScript Finder program:
tell application "Finder" set theWindow to window 1 set thePath to (POSIX path of (target of theWindow as alias)) display dialog thePath buttons {"Clipboard", "OK"} default button 2 if the button returned of the result is "Clipboard" then set the clipboard to thePath end if end tell
Here’s an example of what this AppleScript dialog looks like when it’s displayed:
At this point you can manually copy and paste the Finder path, or just press the Clipboard button to have the path sent to your Mac OS X clipboard.