Copy Finder Path to Clipboard – Tip 2

March 28, 2008

Yesterday I wrote a short AppleScript application that copied the current path of the Finder window to the clipboard. A rather unassuming, yet handy application. One downside to this application is that if you want the path of a folder selected in Finder, you would have to double-click on the folder to change the Finder path to that folder location, and then you could engage the AppleSscript application to copy the path.

For example, in the figure below clicking the Script Editor icon that we created in yesterday’s post (to the right of the terminal window with green arrow) will copy the path /Applications/ to the clipboard. However, what if you intention was to copy the path of the highlighted folder, that is, /Applications/Utilities/Java/ ?

I’ve reworked the original example to add support for dragging a folder onto the icon and also moved the code for displaying a dialog box when an error occurs into a subroutine. Take a look at updated application below:

Script Editor Click here to paste the code below into the Script Editor

-- When clicking on the icon
try
  tell application "Finder"
    set currentPath to (POSIX path of (target of front window as alias))
    set the clipboard to currentPath
  end tell
on error
  my finderErrorMsg()
end try

-- When dropping a folder onto the icon
on open {droppedFolder}
  try
    tell application "Finder"
      set the currentPath to (POSIX path of droppedFolder as text)
      set the clipboard to currentPath
    end tell
  on error
    my finderErrorMsg
  end try
end open

-- Error msg when unable to copy a path to the clipboard
on finderErrorMsg()
  display dialog "Unable to copy a path to the clipboard.
                      Make sure Finder is referencing a directory/folder
                      within the file system."
                      buttons {"Ok"} with icon caution with title "Error"
end finderErrorMsg

With the addition of the on open reference (line 12) you can now drag/drop a folder in Finder onto the application icon and its path will be copied to the clipboard. Before you can use the script, you will need to save the AppleScript code as an application and drag the script onto the toolbar (remove any previous version of the program by right clicking and selecting ‘Remove Item’).

As a quick debugging tip, you can add a dialog box as shown below to display the path that was copied into the clipboard:

tell application "Finder"
  set currentPath to (POSIX path of (target of front window as text))
  set the clipboard to currentPath
  display dialog currentPath buttons {"ok"}
end tell
...

Now you can view the clipboard contents (through the dialog box) which makes the debugging process a little easier should you decide to tweak this application.

Another good example of the power of scripting on a Mac.

7 comments

That is exactly what I was looking for; Thank you!

by JessD on Dec 30, 2008 at 3:18 pm. Reply #

Thanks for this little tidbit. Very handy.

One quick note, on my machine (Mac Pro, 10.5.6) I had to change the error dialog code so it looks like:

on finderErrorMsg()
display dialog “Unable to copy a path to the clipboard.
Make sure Finder is referencing a directory/folder
within the file system.” with icon caution with title “Error”
buttons(“Ok”)
end finderErrorMsg

Otherwise I’d get an error that “a icon can’t go after buttons” or something.

by Sean on Jan 15, 2009 at 6:11 pm. Reply #

Thanks for the script, it’s very very useful! But I seem to have a little problem using it… When the folder has ” ” (spaces) and I want to copy the path to the Terminal, it isn’t able to find the directory :-(

Is there any easy (or obvious) solution for this issue?

Thanks!
Ricardo

by rjfvaac on Jun 5, 2009 at 3:42 pm. Reply #

Well, I’ve just solved my little issue, after talking to a friend. I only needed to change your script from

set the clipboard to currentPath

to:

set the clipboard to "\"" & currentPath & "\""

And all is perfect :D

Thanks again

by rjfvaac on Jun 5, 2009 at 7:16 pm. Reply #

Thanks! That’s exactly what I was looking for. I still can’t believe the Finder doesn’t have a feature like this yet. I added rjfvaac’s modification, but unfortunately some applications mis-handle quoted paths in save-as dialogs and apparently treat the entire quoted string as a filename.

by Paton Lewis on Feb 7, 2010 at 3:45 pm. Reply #

Hey. If you you want a more clean/integrated solution, FinderPath may be worth a look:

http://finderpath.bahoom.de/

by beaverbee on Apr 26, 2010 at 6:51 am. Reply #

Select file
Get Info
Triple-click path after Where: in the General section
Copy

by none on Mar 9, 2012 at 2:22 pm. Reply #

Post a comment:

Required.

Required. Not published.