Re: getting a file by URL ?
Re: getting a file by URL ?
- Subject: Re: getting a file by URL ?
- From: Thomas Fischer <email@hidden>
- Date: Tue, 1 Jun 2010 15:54:35 +0200
Hi Mark,
> On Tue, Jun 1, 2010 at 4:15 AM, Thomas Fischer <email@hidden> wrote:
>> my first problem is what AppleScript considers a URL:
>
> Precision, please. The problem you are demonstrating is that
> different applications will generate different paths/URLs for the same
> file. That's hardly AppleScript's fault.
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.
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):
set myURL to "file:///localhost/Users/Thomas/Desktop/New Folder/My Test.txt"
set myFile to POSIX file myURL
--> "Macintosh HD:localhost:Users:Thomas:Desktop:New Folder:My Test.txt"
get POSIX path of myFile
--> "/localhost/Users/Thomas/Desktop/New Folder/My Test.txt"
but
set myFile to POSIX file "file://localhost/Users/Thomas/Desktop/New Folder/My Test.txt"
--> "Macintosh HD:Users:Thomas:Desktop:New Folder:My Test.txt"
get POSIX path of myFile
--> "/Users/Thomas/Desktop/New Folder/My Test.txt"
> TextWrangler is going well against the grain here. Perhaps its
> treatment of file URLs is a leftover from pre-X Mac OS.
Here I agree, I will ask for a change, as soon as I know what a URL is…
> As to your second problem, you identified it here:
>
>> where both requests are inside a "System Events" call,
>
> Ah! By "call" here, do you mean the code is inside a 'tell
> application "System Events"'? That would be the source of your
> difficulties. I don't know what «class posx» is, or why System
> Events' vocabulary interferes with the normal interpretation of 'POSIX
> path', but it does. It may be that you have found a bug.
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.
>>
> (Having a difference when using a variable vs a literal is not
> uncommon in AS, where the compiler does as much work ahead of time as
> it can.)
But it is extremely confusing, like in the example above!
How can I know in advance what results I get when code that is logically equivalent yields different results (as in the example above)?
> Simply moving the "get POSIX path of myFile" outside the tell block
> removes the error, but I would get rid of the tell block entirely -
> it's totally unnecessary in this case. As a general rule, you want
> to use tell blocks only when necessary and make them as small as
> possible.
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).
>> All of this gets more confusing if I start with
>> "file:///Users/Thomas/Desktop/New Folder/My Test.txt"
>> where the " " may or may not be evaluated by System Events, it seems it rather isn't.
>
> Indeed, it seems that "POSIX file" does not expand %-encoded bytes in
> URL's. That would also seem to be a bug.
As the example above shows, this will be a problem.
I suppose that this affects URL-encoding in general.
>> 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"?
>
> It doesn't.
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 }
All the best
Thomas
_______________________________________________
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