Do applescript repeat loops reflect changes instantly?

Go To StackoverFlow.com

0

I'm making a script to open multiple folders in a directory, and it's not working as planned. I've tried outlining it and walking through the steps one by one pretending I'm the computer executing it but when I run it the outcome is very different.

It uses repeat and repeat with a lot. The repeat repeats for as long as there is ANYTHING in a certain array (I mean List) and the repeat with is INSIDE of the first repeat, which repeats it's own loop with everything in the array at that time.

Now, one of the actions of the repeat with loop is to change the array. Which I think would change the loop would it not?

Example

(foldList is A, B, C)

repeat until {}
repeat with folder_name in foldList
do something
set foldList to 1, 2, 3
end repeat
end repeat

What I would THINK that does is iterate through the first loop as "A", but before hitting the end it would change foldList to 1, 2, 3. So instead of going through the next loop as "B" I'd think it would go as "1" instead.

But if it did that then I don't think my manual walkthrough would be off by so much. So I'm under the assumption that in Applescript when you're in a repeat with, regardless of changing the List you WILL end that loop on the last item of the first List (before the list was replaced.) Is this right?

2012-04-03 22:52
by user1159454


1

This will help you find the value of each loop.

set foldList to {"A", "B", "C"}
repeat 3 times
repeat with folder_name in foldList
    display dialog folder_name
    set foldList to {1, 2, 3}
end repeat
end repeat

Version 2:

set foldList to {"A", "B", "C"}
repeat 3 times
repeat with i from 1 to count of foldList
    display dialog item i of foldList
    set foldList to {1, 2, 3}
end repeat
end repeat
2012-04-03 23:17
by adayzdone
Yeah it looks like it continues with the loop regardless of the change...do you have any idea why, or how to get around it? Thanks. Oh and stupid question but just to clarify, is what I'm trying to do called a recursive loop - user1159454 2012-04-03 23:20
Try version 2 .. - adayzdone 2012-04-03 23:46
Thanks! This helpe - user1159454 2012-04-04 00:07