Join Scott Simpson for an in-depth discussion in this video Using "repeat" to create loops, part of Learning AppleScript.
Now that we have a list, let's take a look at using the items that are in that list one by one. For this I'll use the repeat keyword. There's two basic ways of going about this. First, let's take a look at the way that might be a little bit more familiar to programmers. I'll type repeat with i, from one to the count of the images. Repeat sets up a block so, I'll need to say, end repeat at the end of it. What that says is, starting with one, go through the list, and set the value of I to one, two, three, all the way up to the count of the number of items in the list.
Of course, you could replace. The count of the images with a number, if you wanted to count to a particular number. To illustrate this, I'll create a string, and then we'll add a value to that string each time through the loop. I'll set my output to I see. And then inside the repeat block, I'll set my output to my output so it uses the existing value. And then a space and the value of i.
And then a dash just to keep things little bit neat and organized and then item i of the Images. What this will do? It start out with a string 'ic' and then each trip around the loop, it will add a space, then the value of i and then a dash, and then the value at position i. I'll click run. That's handy if I want to use the position of items in the list. But if I just want the items themselves, I can write it differently. I'll delete the repeat block and write a different one. ' repeat with an Image in the Images, and then of course end repeat.
Inside that block I'll set my output To my output and space and the value, an image. What this does is sets the value of an image to each successive value of the list every time around. So I'll click run and with the lot I was typing that gets me what I'm after. Now there's another way that you can use repeat as well, leaving off the keyword width. And that creates an infinite loop, and if I run this you can see nothing interesting happens because its not really doing anything and there's nothing to tell it to stop programmatically.
So, I'll hit the stop button up here. You can break out of a loop using the return key word. I'll add that in here, and then when I click run, with return in here, this loop doesn't even go around once. It steps in, it says, set myVar to one, and then it returns, right back out. Return stops the execution of the loop, and tells AppleScript to move out one level. That is, if you have three nested loops, and a return statement that triggers inside the innermost loop, AppleScript will step out of that loop and continue at the second level instead.
- What is AppleScript?
- Using comments, variables, text, and numbers
- Working with files and folders
- Using conditionals and creating loops
- Building functions
- Controlling other programs with AppleScript
- Preventing and catching errors
- Distributing your AppleScript application