How to get a file URL

I would a contextual menu item that copies the full file path as a URL (e.g. file://Macintosh HD/Application/Word). Disk item has a property URL. But when I run a contextual Finder script with

set s to (URL of f)
set the clipboard to (s as string)

the clipboard remains empty. Whereas

set s to (POSIX path of f)
set the clipboard to (s as string)

does copy the path to the clipboard.

What’s wrong with ‘URL of f’?

Since you didn’t post your whole script, I have no idea what’s happening before you get to the point where your posted code runs. “URL” is a property that only the finder knows about, so you must include it in a ‘tell finder’ block. Also, you don’t mention what the variable “f” contains. Is it an alias, a string, a posix or hfs path… it’s a mystery. Other than that, the following works fine…

set tmpFile to (choose file without invisibles) --> Result: an alias
tell application "Finder"
	set tmpProperties to (properties of tmpFile)
	set theUrl to (URL of tmpProperties)
	set the clipboard to (theUrl as string)
end tell

j

Thanks and sorry for the incomplete script. I was trying to make a contextual menu item using Ranchero’s BigCat contextual menu plugin. So the complete script looked like:

on main(f)
	
	set s to (URL of f)
	set the clipboard to (s as string)
	
end main

f is the list of selected files (I was assuming only one selected file).

Maybe it is also helpful to describe what I’m actually trying to achieve. That is a way to copy a reference to a word document and paste it in an HTML-form, so that clicking on the link results in opening the document in Word.

First, as jobu mentioned, you need to tell Finder to get the URL property. Second, “f” is a list, and you should handle it as such.

Try something like this:

on main(selectedFiles)
	set URLs to {}
	repeat with thisItem in selectedFiles
		tell application "Finder" to get URL of thisItem
		set URLs's end to (result as text)
	end repeat
	
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to (ASCII character 13)
	set the clipboard to (URLs as text)
	set AppleScript's text item delimiters to ASTID
end main

Thanks very much! This works perfect – almost, because the resulting url is something like:

file://localhost/Volumes/server.company.com/aFolder/aFile.txt

But to access a file on an SMB mounted volume it should look like:

file:///Volumes/server.company.com/aFolder/aFile.txt

(Note that the share-name is absent.)

This is the path as listed in Get Info (preceded with ‘file://’).

Is there any chance that that form of the path is available as a property?

I don’t note that. Isn’t the share name “aFolder”?

Localhost and “/” should refer to the same thing. If I use either one in BBEdit, the files opens. If I put a localhost URL into Safari, it’s automatically changed to “/”. What are you doing that won’t accept the localhost format?

Now, if you must have the “/” style, you could try something like this:

on main(selectedFiles)
	set ASTID to AppleScript's text item delimiters
	
	set URLs to {}
	repeat with thisItem in selectedFiles
		set AppleScript's text item delimiters to "localhost"
		tell application "Finder" to set resource to URL of thisItem
		set protocol to first text item of resource
		set resource to (text items 2 through -1 of resource) as text
		
		set AppleScript's text item delimiters to "/"
		set URLs's end to ((protocol as text) & resource)
	end repeat
	
	set AppleScript's text item delimiters to (ASCII character 13)
	set the clipboard to (URLs as text)
	set AppleScript's text item delimiters to ASTID
end main

No, the file server is referred to as ‘server.company.com’ which is immediately followed by the name of a directory on the share, which I referred to as ‘aFolder’. This seemed strange to me too. But if you are able to ‘Get Info’ on a file on an SMB share, you’ll see that the path is in the form ‘file://localhost/Volumes/server.company.com/aFolder/aFile.txt’. Whereas URL returns the path in the form ‘file://localhost/Volumes/ShareName/aFolder/aFile.txt’ (Sorry, I made a mistake in my previous post, the path returned by URL does include the share name).

You are right about ‘localhost’ and ‘/’ being synonymous.