Chapter 7. Working with Variables and Properties

IN THIS CHAPTER

  • What are variables?

  • What are properties?

  • Creating three simple scripts

In Chapters 5 and 6, you got an overview of AppleScript and its history, a good look at AppleScript Editor, and an introduction to objects and dictionaries. In this chapter, you're going to learn the details you need to know to start creating your first scripts.

Also in Chapter 5, you learned about the basic data types. In this chapter, you're going to build on this knowledge by learning how to use these data types with properties and variables. At first glance, properties and variables seem pretty similar, as they each allow you to store some kind of value, but they are different in key ways.

What Are Variables?

If you're a programmer, you know that a variable is a simple way to store some kind of value so that you can use it again. That value might be a number, a list, or some text (hence the importance of knowing something about supported data types).

For example, you might want to store a number (like your age) or a text string (your name) or a list of items (such as a grocery list), and then reuse it numerous times during your script. Or you might want to find something out (like how many lines exist in all the text files in a certain directory) and then store that value in a variable. The next time you need to know how many lines there are, you don't have to run the command over (unless you need to); you can just look up the value stored in the variable. ...

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.