Re: don't say "repeat with x in every"
Re: don't say "repeat with x in every"
- Subject: Re: don't say "repeat with x in every"
- From: has <email@hidden>
- Date: Mon, 4 Jul 2005 21:27:55 +0100
Matt Neuburg wrote:
>In my book (p. 225) I warn the reader to not to say "repeat with x in
>every". An example is:
>
>set L to {}
>tell application "Finder"
> repeat with f in every folder
> set end of L to f
> end repeat
>end tell
>length of L
>
>As you know, "every folder" in this unqualified form means folders on the
>desktop to the Finder.
>
>The odd thing, which I've only just noticed, is that the length of L is
>insane - far more than the number of folders on the desktop.
>
>And here's why. As I explain in my book, when you talk like this, AS does an
>odd "optimization"; it doesn't actually fetch every folder in the repeat
>loop, but instead just asks for a "count".
This makes perfect sense. AppleScript's 'repeat with x in y' construct takes an iterable object and construct a series of by-index references based on that object: 'item 1 of y', 'item 2 of y', etc. It doesn't need to know what those references point to, only how many of them to generate. And the easiest way to find out that information is to send the object a 'count' message. You can easily confirm this yourself, btw:
script obj
on count x each y
return 10
end count
end script
set lst to {}
repeat with i in obj
set end of lst to i
end repeat
lst --> {item 1 of «script obj», item 2 of «script obj», item 3 of «script obj»,..., item 10 of «script obj»}
There is a subtle weakness in this design: users naturally assume that when they use 'repeat with...in' with an application reference it will automatically evaluate the reference by sending a 'get' event and then iterating over the resulting list, as this is how AppleScript implicitly evaluates application references elsewhere. It doesn't though; it just extends the existing reference object to create new ones. On its own this shouldn't matter:
>But what it asks is this:
>
> count every folder
AppleScript packs 'count' events in a very strange way: it *always* includes an 'each' parameter, even when one isn't explicitly given. (Something I noticed myself while chasing down an unrelated bug in appscript.) In fact, the event it's actually sending here is:
count every folder each item
You can confirm this yourself by turning on AEDebug. Which, as you say, is effectively interpreted by Finder as 'count every item of every folder'. Which is exactly the way Finder should evaluate it.
Now, here's the extra wrinkle. If you run:
tell app "Finder" to count every folder
it returns the expected(!) value. Again, AEDebug reveals all: AppleScript actually pulls apart the 'every folder of current application' reference and packs the 'count' event as:
count current application each folder
I can only guess this special-case mangling is some legacy kludge to get around some problem inadvertently created by early scriptable applications' implementations. (For example, if you look carefully at Finder's dictionary, you'll notice its 'count' command *requires* an 'each' parameter - something that more modern scriptable applications sensibly do not. One could easily imagine some plucky young AppleScript engineer heroically coming up with some ingenious workaround to this clumsy design, rather than go through all that tedious hassle of filing bug reports with the hope of it some day being fixed at source.) Only the original AS engineers could tell you for sure though, and in any case it hardly matters now what those historical reasons are because it's what we're stuck with.
So what you have is not a bug in its simplest sense, but two subtle misfeatures in the AppleScript interpreter that together conspire to produce buggy behaviours whenever they meet. Finder's not to blame at all (except maybe indirectly for its stupid insistence on getting an 'each' parameter in 'count'; but it's hardly the worst offender when it comes to Scriptable Applications That Ask For Stupid And Unreasonable Things).
Hope that clarifies... and feel free to submit a you-know-what on it. ;)
has
(Who has just spent half the weekend troubleshooting and applying another round of patches to appscript to compensate for All The Stupid Things Applications Do Which AppleScript Stupidly Covers Up For Them Allowing Them Go Bite Everyone Else Instead, and is therefore not feeling in a charitable mood towards any of 'em.)
--
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