Finder and URLs

Well, I am banging my head on the Finder icon today. I had created a simple droplet that displays the URL of the dropped file in a dialog box. The droplet uses the “tell application “Finder” to set theURL to URL of theFilePath” method.

Well, it happened like this. I sat down to see if I could get the POSIX path of a URL. I know, I know, but I like experimenting. But I got some weird string so I commented out the POSIX conversion line and just had this:

set p to (choose file)

tell application "Finder" to set ofP to URL of p

That usually returns the URL. I had a PDF on my desktop called AppleScript Guide 1. So I pressed run and I got

“file:///.file/id=6571367.2300908”

This confused me, so I went over to my URL-finding droplet (which I haven’t used since upgrading to Lion) and tried it on numerous files and guess what? It didn’t work!

So I am here to ask, has anyone else noticed this? Is there another way to do this? I am disappointed at the number of AppleScript errors that Lion contains.

Thanks,
L :slight_smile:

I’m not sure if this is an error. Since 10.6 the NSURL class also supports the volfs (is since 10.5 embedded in the system) notation called “file reference url” where it is notated as .. With this reference you’re only limited to open and close a file but the advantage is that opening a file is a lot quicker, specially when the file is small. Also it works like an Alias, the file can be renamed or moved but the file reference url still knows where to find it.

Thank you, DJ! Just one thing. Do you know of another way to get a URL?
And also, can URLs be converted to POSIX? I guess it’s just removing the "%20"s…

every alias specifier has a property POSIX path


set p to (choose file)
set POSIXpathOfP to POSIX path of p

Normally nodes can’t resolve names because the file name of a file is not stored within the file. The directory holds a bundle of nodes to files and bind names to the file. When having the node of a file you’re a level below file names and it is a one way binding. This was one of the reasons Mac OS X used volfs to resolve the path of a file reference. I’m not sure if it’s possible but with AppleScriptObjC it is and if it doesn’t exists yet it’s easy to write a foundation tool (command line utility) who can do it for you. I’m not behind a Lion machine at the moment but again it is possible one way or another.

EDIT:

Because you can write AppleScriptObjC in Lion in script editor your code would look like this.

set p to (choose file)
tell application "Finder" to set urlString to URL of p as string
set fileURL to current application's class "NSURL"'s alloc()'s initWithString_(urlString)
fileURL's |path|() --returns: Posix Path if file url is file:///path/to/file or file://./file/xxxxxx.xxxxxx

Thanks, DJ!

But now I have another problem. For some reason, my Editor refuses to accept ASOC commands. This is a strange problem…

hm, I don’t understand the sense of determining the POSIX path from an alias via the URL

Can you get the file path of a URL?

I thinkn what Stefan meant was that you create an alias with choose file, then get the url of it, then get back the path of the url. it feels like you’re saying 1 as string as integer as string. But then your wrote:

Well, it happened like this. I sat down to see if I could get the POSIX path of a URL. I know, I know, but I like experimenting. But I got some weird string so I commented out the POSIX conversion line and just had this:

So my code is odd as well but also aware of that you where experimenting.

Yes, it was an experiment. But my question now is this.

Lets say I have a URL:

“file:///Users/Joe/Desktop/Hello%20World.html”

It is a string in my script. Is it possible to get the path and/or the POSIX path from that URL without using ASOC?

EDIT Sorry :slight_smile:

this is a mixture of path and url.

“/Users/Joe/Desktop/Hello World.html” is a path and “file:///Users/Joe/Desktop/Hello%20World.html” is an URL. So to coerce an URL to an POSIX path can be done with python (but also PHP and many other scripting languages):


decodeURL("file:///Users/Joe/Desktop/Hello%20World.html")
on decodeURL(someURL)
	return do shell script "/usr/bin/python -c '" & ¬
		"from sys import argv; " & ¬
		"from urllib import unquote; " & ¬
		"from urlparse import urlparse; " & ¬
		"print unquote(urlparse(argv[1])[2])' " & quoted form of someURL
end decodeURL

Where do you get this “URL” from? A percent escaped path is very unusual

I edited the post you quoted. Somehow I forgot to put the “file:///” at the beginning. I was thinking too much about paths. :slight_smile:

Have you rtied my function then? It will work to resolve urls

It was exactly what I was looking for, thank you!

But still my AppleScript Editor does not accept ASOC commands. I am running the new version and Lion. This will not work:

set fileURL to current application's class "NSURL"'s alloc()'s initWithString_(urlString)

I get the error

“class "NSURL" doesn’t understand the alloc message.”

?