Chapter 9. Conditionals and Loops

IN THIS CHAPTER

  • What are conditional tests and loops?

  • Conditional tests

  • Repeating loops

  • Writing scripts

In the last two chapters, you learned how to work with both variables and operators. You've built a few scripts, some of which go way beyond what you've already been formally introduced to. In this chapter, you're going to learn about conditional tests and loops. If you're already a programmer, these concepts will be easy for you to learn. If you're not a programmer, don't worry; you'll learn it very quickly.

What Are Conditional Tests and Loops?

A conditional test is very simple: It's an expression that tests to see if something (usually a value or variable) in your code passes some kind of test. Once the test is passed, the code can set a value, perform an action, or do something with that information. If the test isn't passed, then you might have a simple default solution, or a more detailed response.

In other words, conditional tests are all about decisions, and your use of them is basically teaching AppleScript how to make good decisions — hopefully, the kinds of decisions you would make as a human doing them manually.

For example, you might have a randomly selected number created by AppleScript. You could write a conditional test to check to see if that number is higher than 10. If it is higher than 10, then you might display a dialog. If it isn't higher than 10, then you might just have the Mac beep or something.

That's a fairly simple conditional ...

Get Apple® Automator with AppleScript® Bible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.