Mar 28 2008

Copy Finder Path to Clipboard – Tip 2

Published by at 6:03 am under AppleScript,System,Tools/Utilities

  

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 responses so far

7 Responses to “Copy Finder Path to Clipboard – Tip 2”

  1. JessDon 30 Dec 2008 at 3:18 pm

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

  2. Seanon 15 Jan 2009 at 6:11 pm

    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.

  3. rjfvaacon 05 Jun 2009 at 3:42 pm

    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

  4. rjfvaacon 05 Jun 2009 at 7:16 pm

    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

  5. Paton Lewison 07 Feb 2010 at 3:45 pm

    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.

  6. beaverbeeon 26 Apr 2010 at 6:51 am

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

    http://finderpath.bahoom.de/

  7. noneon 09 Mar 2012 at 2:22 pm

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

Leave a Reply