Re: getting a file by URL ?
Re: getting a file by URL ?
- Subject: Re: getting a file by URL ?
- From: "Mark J. Reed" <email@hidden>
- Date: Tue, 1 Jun 2010 15:50:11 -0400
> the point is that it is not clear what a "URL" is on the Mac or for AppleScript, if it isn't defined someplace – and
> I didn't find it.
A URL is a subclass of URI, which is string in a particular format,
period. Anything beyond that is up to individual implementations to
decide.
This goes for any other word or phrase you see anywhere in an
AppleScript. If the code is inside a 'tell application "foo"' block,
then all bets are off, because the application can redefine
terminology as it sees fit. There is no enforced consistency, only the
good will of the implementors of each scripting interface.
It's unfortunate that there seems to be some disagreement between
System Events and Standard Additions as regards the translation of
AppleScript into Apple Events when it comes to POSIX file and variable
names. I made a mistake in my original message by naming the former;
I meant the latter.
> And since at least Safari, Mozilla, Opera and TextWrangler are scriptable, there may arise some confusion.
> It seems, that the Opera and IE version doesn't always work (without System Events)
See above. You cannot, under any circumstances, assume that any piece
of terminology (class name, enumeration, command, ehatever) means the
same thing in application A as in application B. Even "file" is
different between the Finder and System Events.
> set myURL to "file:///localhost/Users/Thomas/Desktop/New Folder/My Test.txt"
That's an invalid URL unless you are referring to a file in the top
level of your hard drive named "localhost". It's two slashes, then
the hostname, then the POSIX path. If you leave the hostname blank,
then you wind up with three slashes together, and "localhost" is
assumed.
That is the difference between your two examples: a typo.
> That goes back to your first suggestion in this
>
>>> Should work as-is with System Events; add "as alias" if you intend to use it with the Finder.
Well, as I said above, that was a mistake; I meant "Standard
Additions". Both "POSIX path of" and "POSIX file" are defined in the
Standard Additions osax, not in the System Events dictionary.
> How can I know in advance what results I get when code that is logically equivalent yields different results (as in the example above)?
You can't, really. Try it and see what happens.
The Applescript compiler often does confusing things. If you want a
language that is orthogonal or even consistent, even in theory, then
this is not the language for you. Look at Ruby or Python + appscript,
or Objective C + Scripting Bridge.
> There actually seems to be a difference between the implementation of POSIX in "System Events" and "StandardAdditons" respectively, which is something Apple could have avoided (as with files being different things in System Events and Finder).
There are a lot of moving parts involved in AppleScripting; the whole
point of the language is to let you glue diverse components together.
They don't all move in lock-step, even the Apple technology bits.
> As the example above shows, this will be a problem.
> I suppose that this affects URL-encoding in general.
Why is this a problem? Where is this URL coming from?
If you are talking to an application (browser?) that has loaded a file
via URL and trying to determine what file on disk it was loaded from,
that's not a generally soluble problem. If you're lucky, the
application has a command that will tell you the file associated with
a given document or window directly. Otherwise, you have to do your
own string munging and do it in an application-specific way, since
different apps can do different things and still call them "URLs".
>
>>> By the way, hovering the mouse pointer over "file" in "POSIX path of file" of the result and error messages above reveals strange differences:
>>
>> What are you talking about here? The 'POSIX path of file' bit
>> doesn't even appear in the result and error logs in AppleScript Editor
>> OMM, and hovering the mouse over the code doesn't show anything of the
>> sort. What editor program are you using? Do you have a plug-in?
>
> Paste the following script into a new AppleScript Editor window:
>
> tell application "System Events"
> get POSIX path of (file "Macintosh HD:Users:Thomas:Desktop:New Folder:My Test.txt")
> log the result
> set myFile to POSIX file "file://localhost/Users/Thomas/Desktop/New Folder/My Test.txt"
> log myFile
> get POSIX path of myFile
> end tell
>
> If you run the script, 'POSIX path of file' will appear in the event (not the result) pane, and if you let your pointer (patiently) hover over "file" you will see what I mean. (AppleScript-Editor Version 2.3 (118), AppleScript 2.1.2; Mac OS X 10.6.3)
>>> set myFile to POSIX file "file:///Users/Thomas/Desktop/Test.txt"
>>> log "myFile: " & myFile
>>> --> (*myFile: Macintosh HD:Users:Thomas:Desktop:Test.txt*)
>>> get POSIX path of myFile
>>> How can myFile in the last line have the value "file:///localhost/Users/Thomas/Desktop/Test.txt"?
That returns "/Users/Thomas/Desktop/Test.txt" OMM. The raw Apple
Event data is "file:///Users/Thomas/Desktop/Test.txt", with no
"localhost". Did you perhaps typo it as above?
> I claim it does, as this is what the Apple Event tells me:
>
> 'core'\'getd'{'----':obj '{ 'form':'prop','want':'prop', 'seld':'posx', 'from':'furl'("file:///localhost/Users/Thomas/Desktop/Test.txt") }, &'csig':'65536 }
One way to capture this data is in the Terminal. Close AppleScript
Editor, start up Terminal, do this:
$ cd
$ script
Script started, output file is typescript
$ export AEDebug=1 AEDebugSends=1 AEDebugReceives=1
$ open -a "AppleScript Editor"
Now every event in either direction will be logged to the file
"typescript" in your home directory (as well as scrolling by in that
Terminal).
--
Mark J. Reed <email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden