Can't exit loop!

Hi!

Is it possible to exit a "repeat x times" loop prematurely with the use of for example an if-statement? I can't seem to get it to work. The loop will still run x number of times,
even if the if-statment is true and triggers an "exit repeat" command.
Here is an example of the code:

repeat 5 times
play some item of tracks_1
delay (get duration of (current track))
if not (greenTime) then
exit repeat
end if
end repeat

Anyone have any ideas why this isn't working?
Thanks a lot!

Erik

Mac OS X (10.5.2)

Posted on May 19, 2008 11:46 AM

Reply
3 replies

May 19, 2008 3:47 PM in response to Slerker

Your basic code is valid -exit repeat will exit the current loop.

However, there are two problems with your script that may be impacting it.

The first is the use of 'delay'. This will essentially pause the entire script for the duration - in this case until the end of each play of the track. As a result you won't be able to stop mid-song, only at the end of a track. That may be OK, but it might also be confusing you if you expect it to stop mid-way.

The other issue is your condition:

if not (greenTime) then


Where are you setting this? There's nowhere within the loop that sets this variable therefore it will always have whatever value it had before the loop started. Again, that might be what you want, but I'm guessing you are expecting to change greenTime at some point to trigger a premature exit, but this will never happen. You would need to set greenTime somewhere within the loop in order for it to have some effect.

So the big question remains as to exactly how you expect this script to work. What is 'greenTime' based on? Is it likely to change during the script's execution? or is it preset?

May 20, 2008 2:04 AM in response to Camelot

Hi!

You are correct Camelot, I falsely presumed that the variable (greenTime) would auto-update over time (it is a variable checking current hour to see if it is time to change playlist), but it doesn't of course. I realized that too this morning. Since I have to put it in the loop to make it refresh, I'll just skip having a variable all together and run an hour check in manual code instead (the point of the variable beeing not having to re-enter the time requirements everywhere in the script should I need to change it, but I can live with that).

Thanks a lot for your help!
/Erik

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Can't exit loop!

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.