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.
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…
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
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.
“/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