Re: Converting 'file:/' URL to MacOS path name...
Re: Converting 'file:/' URL to MacOS path name...
- Subject: Re: Converting 'file:/' URL to MacOS path name...
- From: has <email@hidden>
- Date: Mon, 28 Feb 2005 20:34:38 +0000
David Crowe wrote:
Given a URL-like file specification like:
file:/Tinantia/Users/dad/Documents/Green/AIDS/AIDS-Databases/AIDS
References.fp7
is there a simple way to convert this into the corresponding MacOS filename?
Where did that come from? It looks like an ordinary POSIX path with
'file:' and a drive name plonked on one end. A proper URL would look
like:
file://localhost/Users/dad/Documents/Green/AIDS/AIDS-Databases/AIDS References.fp7
Your string could be converted to a POSIX path (which AS understands)
simply by chopping off the 'file:/Tinantia' part. For argument's sake
though, let's assume you've got a real URL from which you want to
extract the filepath component. I don't know any third-party
AppleScript-specific solutions for working with URLs and there's
nothing in the language or Standard Additions to help (while AS does
have a «class furl» type, there's very little you can usefully do
with it), so your options are probably as follows:
1. You could write your own vanilla AS solution. That'd a right chore
though since you'd have to write your own URL parser and Unicode
encoder libraries from scratch.
2. If you're using Studio, I imagine you could use an NSURL instance
to perform the conversion easily enough via 'call method'; see
NSURL's initWithString and path methods.
3. If you know some C, you could whip up an osax that performs the
conversion using Core Foundation's CFURLCreateWithString and
CFURLCopyFileSystemPath.
4. Finally, probably the simplest (though not particularly fast)
solution is to leverage something like Perl or Python that already
has libraries for this kind of stuff via trusty old 'do shell script':
property pythonPath : "/usr/bin/python" -- for
Apple-installed MacPython on OS 10.3
set theURL to "file://localhost/Users/has/Untitled ƒ"
set posixPath to do shell script (pythonPath & " -c 'from sys
import argv; from urlparse import urlparse; from urllib import
unquote; print unquote(urlparse(argv[1])[2])' " & quoted form of
theURL)
--> "/Users/has/Untitled "
--
Laine Lee wrote:
That URL should probably begin with "file:///", but, in any case, you can
reverse engineer this, I suppose:
http://lonestar.utsa.edu/llee/applescript/create_file_URL.html
This code doesn't encode non-legal characters properly, so will break
very easily. See <http://www.faqs.org/rfcs/rfc2396.html> for how to
encode URLs correctly (also works great on insomnia). Again, the
easiest solution is just to 'do shell script' something that already
knows how to do this stuff.
HTH
has
--
http://freespace.virgin.net/hamish.sanderson/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden