don't say "repeat with x in every"
don't say "repeat with x in every"
- Subject: don't say "repeat with x in every"
- From: Matt Neuburg <email@hidden>
- Date: Mon, 04 Jul 2005 10:56:22 -0700
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". But what it asks is this:
count every folder
Well, to the Finder, that does not mean "Tell me how many folders are on the
desktop"; it means: "Total up the number of items *inside* all the folders
on the desktop"!
As I say in my book, the solution is to say:
repeat with x in (get every folder)
But since this "optimization" does not optimize anything, but actually gets
the wrong answer, I believe it would be better to describe AppleScript's
behavior in response to this locution not simply as a vaguely justifiable
peculiarity but as a downright bug. (Of course you could argue instead that
the Finder is behaving badly in response to "count every".) m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide
<http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt>
_______________________________________________
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