Re: When is a list not a list?
Re: When is a list not a list?
- Subject: Re: When is a list not a list?
- From: Bill Cheeseman <email@hidden>
- Date: Fri, 16 Feb 2001 07:44:45 -0500
on 2/16/01 12:00 AM, Ric Phillips at email@hidden wrote:
>
tell application "Finder"
>
set root to (choose folder)
>
repeat with thisFolder in (the name of every folder in root)
>
display dialog thisFolder
>
end repeat
>
end tell
>
>
does not bring up a sequence of dialog boxes with the names of each sub
>
folder, whereas;
>
>
tell application "Finder"
>
set root to (choose folder)
>
repeat with thisFolder in (the name of every folder in root as list)
>
display dialog thisFolder
>
end repeat
>
end tell
>
>
does in fact bring up the expected sequence of dialog boxes.
I believe the key to this conundrum lies in the fact that thisFolder in the
first version is a Finder reference. What you want is a list, as shown by
the second version.
Loop counter variables are special in AppleScript, as they are in many
languages. Some of the consequences of this are described in my little
article, "Getting the Value of a Loop Counter," here:
<
http://www.AppleScriptSourcebook.com/tips/gotchas/loopcounter.html>
But this issue is even more complex in the Finder. I guess I should add
something to the article about this, but I don't yet fully understand what
the Finder is doing here.
In your first snippet, the value of thisFolder is (on my system) "item 1 of
name of every folder of alias "Sucia:Desktop Folder:Pending Items:"".
In your second script, it is (on my system) "item 1 of {"CarbonLib 1.2",
"CarbonLib 1.2.5a3", "idleLib in JavaScript OSA", "Keychain Scripting",
"MacMoney/Excel budge", "Omni AppleScript notes", "Power Management
AScript"}". (Aren't those folder names interesting!)
This difference is crucial.
But never mind all that. I would have written your script this way, applying
my personal rule of thumb that scripts are often easier to understand if you
keep the loop counter as simple as possible, then pull the information you
want out of it once you're inside the loop. (Somebody will probably tell me
that this is sending many more Apple events than your second version and
will therefore be slower, but perhaps that doesn't matter in most situations
where you would want to get this information. Michelle's version avoids this
problem.)
tell application "Finder"
set root to choose folder
repeat with thisFolder in every folder in root
display dialog name of thisFolder
end repeat
end tell
Getting "name of thisFolder" inside the loop in effect gets the "contents
of" the loop variable for you, which my article and the AppleScript Language
Guide tell you to do.
-
Bill Cheeseman, Quechee, Vermont <
mailto:email@hidden>
The AppleScript Sourcebook
<
http://www.AppleScriptSourcebook.com/>
Vermont Recipes-A Cocoa Cookbook
<
http://www.stepwise.com/Articles/VermontRecipes/>