Re: Repeat Loop Skipping Items
Re: Repeat Loop Skipping Items
- Subject: Re: Repeat Loop Skipping Items
- From: Nigel Garvey <email@hidden>
- Date: Sat, 9 Jun 2001 12:04:42 +0100
Steve Suranie wrote on Fri, 8 Jun 2001 15:53:10 -0400 :
>
Has anyone had this happened before. I am running a script that checks a
>
folder that allows the user to select a folder. The script then loops
>
through all the items of the folder and if they are a TEXT type, opens them
>
in Tex-Edit Plus and copies the contents into an existing file. The script
>
read each file sequentially just like it was designed to. Good script.
>
>
However, I then wanted to move the aforementioned read articles out of the
>
folder to a seperate folder. When I added that bit o script the script
>
continued to move sequentially through the files but in leaps of four or so.
>
It did move those files it read but as I explained to my computer in no
>
uncertain terms, that's not what it was designed to do. Bad script.
You haven't posted the offending code, but it's possible you're using a
repeat loop that looks something like this:
tell application "Finder"
repeat with thisFile in every file of thisFolder
-- check
-- move out of folder
end repeat
end tell
The expression 'every file of thisFolder' is not actually an AppleScript
list of the files, but a reference to the Finder's own list of them. As
you move files out of the folder, they cease to be members of 'every file
of thisFolder' and the Finder list is updated. If you remove, say, the
first file from the folder, the second file becomes the first, the third
file becomes the second, and so on. The AppleScript repeat loop, however,
continues to access consecutive *positions* in this list - so the second
time through, it misses the old second file (now in the first position)
and accesses what used to be the third. The more files you remove, the
more files get skipped.
The way round this is to get a static, AppleScript version of the list,
either by setting a variable:
set theFiles to every file of thisFolder
repeat with thisFile in theFiles
-- etc.
or forcing a 'result':
repeat with thisFile in (get every file of thisFolder)
-- etc.
Whatever happens to the files in the Finder list, their positions in the
AppleScript list never change and they're all attended to in turn by the
repeat.
NG