Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.6: Create a Service to copy a file path URL to the clipboard Desktop
Snow Leopard only hintThis simple Automator Service will copy the path of a Finder item to the clipboard as a file:// URL; this is very helpful in my environment, where I need to send a shared file's location to someone, rather than emailing around copies of a file.

In Automator, create a new Service and set it to receive selected files or folders in the Finder. Next add the Copy to Clipboard action. For the final step, drag in the Run a Shell Script action, and paste in the following code (all on one line, replacing any existing code):

sed -e 's/:/\//g' -e 's/\ /%20/g' -e 's/^/file:\/\//g' | pbcopy

This transforms the Mac-style colon-delimited path into a POSIX path, and prepends file://, so the path becomes a clickable link when pasted into an email.

[robg adds: This worked as described in my testing.]
    •    
  • Currently 2.67 / 5
  You rated: 2 / 5 (18 votes cast)
 
[14,745 views]  

10.6: Create a Service to copy a file path URL to the clipboard | 21 comments | Create New Account
Click here to return to the '10.6: Create a Service to copy a file path URL to the clipboard' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.6: Create a Service to copy a file path URL to the clipboard
Authored by: shiyinjia on Dec 04, '09 08:10:26AM

This hint brings up another question I was wondering how to do: linking address book cards to iCal events in the notes section. Let's say that I have an iCal event to call someone at a specific time, but that I don't want to invite that person to that particular event. How could I obtain the path to that address card so that when I click on it in iCal, I will be taken to that contact? Could this script be modified to do that?

---
jeff hargrove
photography
www.jeffhargrove.com



[ Reply to This | # ]
10.6: Create a Service to copy a file path URL to the clipboard
Authored by: gtaranti on Dec 04, '09 09:37:26AM

What about international characters? When I try to copy a file's URL with greek characters I get something like ..

file:///Users/gtaranti/Desktop/%20댰.psd



[ Reply to This | # ]
10.6: Create a Service to copy a file path URL to the clipboard
Authored by: CBStephenson on Dec 04, '09 10:01:45AM

If you use PathFinder this is trivially easy; PF has a Copy Path command with six choices for the path format.



[ Reply to This | # ]
10.6: Create a Service to copy a file path URL to the clipboard
Authored by: ghrossman on Dec 04, '09 10:45:13AM
Change the script to this if you want to get the POSIX directory path w/ no "file://" prefix
sed -e 's/:/\//g' -e 's/\ /%20/g' -e 's,[^/]*$,,' | pbcopy

[ Reply to This | # ]
10.6: Create a Service to copy a file path URL to the clipboard
Authored by: ihadmeavision on Dec 04, '09 02:00:09PM

I tried this one and I only get the parent folder's path. The original post above works great but I also don't want the file://. I'm running 10.6.2 if that makes any difference. Great hint by the way.



[ Reply to This | # ]
10.6: Create a Service to copy a file path URL to the clipboard
Authored by: shemp9999 on Dec 04, '09 11:43:44PM
to get a full space and quote escaped POSIX path, try this (Useful for terminal, omits 'file:' prefix).
sed -e 's/:/\//g' -e 's/\ /\\ /g' -e "s/\\'/\\\'/g"  -e 's/\"/\\"/g' | pbcopy
Add expressions for other exotic characters as needed.

[ Reply to This | # ]
10.6: Create a Service to copy a file path URL to the clipboard
Authored by: ihadmeavision on Dec 08, '09 07:38:47AM

Works great now. Thank you so much!



[ Reply to This | # ]
10.6: Create a Service to copy a file path URL to the clipboard
Authored by: bretperry on Jan 27, '10 01:40:31PM

Cool. And works great in most cases, but not for BASH if the Mac file path includes a slash. I know, shouldn't use slashes but we're in a big company and Mac lets you put a slash in File + Folder names, so people do...

This works OK for Forward slashes and back-slashes in the Mac Filename (quotes whole path):

on run {input}
set input to POSIX path of input
set input to "\"" & input & "\""
return input
end run

then add a copy to clipboard action
Maybe someone knows a more elegant way to do this in a shell script?





[ Reply to This | # ]
10.6: Create a Service to copy a file path URL to the clipboard
Authored by: broz on Mar 18, '10 10:43:00AM
This works, but always adds a newline to the end. This is VERY annoying when pasting into the Terminal, as it executes the command you're trying to create. I added at tr -d "\n" to the end
sed -e 's/:/\//g' -e 's/\ /\\ /g' -e "s/\\'/\\\'/g" -e 's/\"/\\"/g' | tr -d "\n" | pbcopy
which fixes that problem. This Automator command does nothing about the selection of multiple items in the Finder. All the paths get copied, and land in the clipboard. I don't want them all pasted into the Terminal, but might want them pasted somewhere else. In that case, the tr bit on the end will not be what you want.

[ Reply to This | # ]
10.6: Create a Service to copy a file path URL to the clipboard
Authored by: deanburchell on Dec 04, '09 11:38:39AM

Would there be a way to modify this so that I could point others to files in a shared DropBox directory?



[ Reply to This | # ]
10.6: Create a Service to copy a file path URL to the clipboard
Authored by: boxcarl on Dec 04, '09 11:46:00AM

Dropbox already has that built in. Just right-click on the file and go into the Dropbox submenu.



[ Reply to This | # ]
10.6: Create a Service to copy a file path URL to the clipboard
Authored by: deanburchell on Dec 04, '09 11:50:29AM

Only "View Previous Versions" appears for me but I'll sure I'll find out more. I should have searched DropBox.com first obviously! I guess the hint just led me to impulse-comment. Thanks!



[ Reply to This | # ]
10.6: Create a Service to copy a file path URL to the clipboard
Authored by: hobbster on Dec 04, '09 02:04:18PM

You do get a "Copy Public Link" contextual menu item for files in your... wait for it... "Public" Dropbox directory.



[ Reply to This | # ]
10.6: Create a Service to copy a file path URL to the clipboard
Authored by: deanburchell on Dec 04, '09 02:07:11PM

Ahhh. Interesting. Well that makes sense. But it's a shame they didn't include one for non-Public file locations as well.



[ Reply to This | # ]
10.6: Create a Service to copy a file path URL to the clipboard
Authored by: mr. applescript on Dec 04, '09 12:06:59PM
Guys, the Finder scripting dictionary has a URL property of every disk item. Here's what the script would look like to get the url of passed items:
set these_URLs to ""
tell application "Finder"
	repeat with i from 1 to the count of input
		if i is 1 then
			set these_URLs to the URL of item i of input
		else
			set these_URLs to these_URLs & return &  the URL of item i of input
		end if
	end repeat
end tell
Edited on Dec 04, '09 12:14:23PM by mr. applescript


[ Reply to This | # ]
10.6: Create a Service to copy a file path URL to the clipboard
Authored by: ewelch on Dec 04, '09 08:21:08PM

The Applscript stopped with an error for me.

---
Eric

Ernest Hemingway's writing reminds me of the farting of an old horse. - E.B. White



[ Reply to This | # ]
10.6: Create a Service to copy a file path URL to the clipboard
Authored by: trosberg on Dec 05, '09 06:29:27AM
The script above needs some file reference(s) as input, for example
set input to selection
Works with international characters in file names.

[ Reply to This | # ]
10.6: Create a Service to copy a file path URL to the clipboard
Authored by: jamietshaw on Dec 06, '09 02:15:20AM

If you dont need the file:// bit, change the Automator steps so you have:

  1. Get File Path
  2. Copy to Clipboard

The Get File Path action gives you various options over how the path is displayed.



[ Reply to This | # ]
10.6: Create a Service to copy a file path URL to the clipboard
Authored by: slferris on Apr 30, '10 12:36:24PM

I don't see "Get File Path" action in Automator. Am using Snow Leopard 6.3.

Thanks



[ Reply to This | # ]
10.6: Create a Service to copy a file path URL to the clipboard
Authored by: Sikanda on Dec 10, '09 11:57:36PM

Works also great with 10.5.8. If I try to use it with 10.4.11 I only get "file://" as a result.
Is anybody able to help to get it done with 10.4.11?



[ Reply to This | # ]
how to open the file directly?
Authored by: prramesh on Apr 28, '10 12:30:45PM

The above shell script (sed -e 's/:/\//g' -e 's/\ /%20/g' -e 's/^/file:\/\//g' | pbcopy) offers the file path like file:///.........

When I click on this path it reveals the file in finder, but it doesn't open the file (for example; it doesn't opens the pdf file). Does any one knows "how to directly open the file from the path"?



[ Reply to This | # ]