How I can copy to the clipboard the path (or the full url) of a file or a folder? [duplicate]

Solution 1:

This is sort of easy.

Select the file and then Command-C will put the path to the file in the clipboard. If you then paste into the Terminal you will get the entire path, unfortunately most other apps you will just get the filename.


If you want the full path then you can create an Automator Service to do it easily :-

enter image description here

Create a new Automator service and change the 'Service receives Selected' to : Files or folders And the in to the Finder application.

Add a Copy to Clipboard Action and Save the Service.

You can now select a file, right click to get a contextual menu and select your service in the Service submenu.

The path will be in the clipboard ready for you to paste. You can even set a keyboard shortcut for it if you like.

Originally I used a Run Applescript Action to get the Posix path to the file and pass it on to the Copy to Clipboard Action.

But I discovered all you needed is to have the selection passed directly to the Copy to Clipboard Action.

This improved on the Applescript because the Applescript could only deal with one item being selected as written.

But now you can select multiple files and get multiple paths in the clipboard without writing any code.

Solution 2:

On OS X "El Capitan",

Option+Command+C the selected file/folder

OR

  1. option right click the file/folder (in Finder)

  2. Select 'Copy "file/folder" as Pathname'.

Copy "file/folder" as Pathname

Copy file path in Finder

You now have the option to copy the path to a file without copying the file itself, perfect for working with files stored on a server. To copy the file path, right-click the file in the Finder, and then hold the Option key down and choose Copy as Pathname.

(From Apple) http://www.apple.com/osx/all-features/#other-features

Solution 3:

You can assign a shortcut to a script like this:

tell application "Finder" to set s to selection as alias list
if s is {} then return
set out to ""
repeat with f in s
    set out to out & POSIX path of f & linefeed
end repeat
set the clipboard to out

There was a bug in 10.7 and 10.8 that made it difficult to get the selection property reliably, but it was fixed in 10.9. (Finder ignored windows that were created after the last time focus was moved to another application and back.) If you use 10.7 or 10.8, add these lines to the start of the script:

activate application "SystemUIServer"
activate application "Finder"