Last time, we looked at AppleScript Dictionaries and learned how to read them. We left off reviewing the dictionary “definition” of the say command. The second line of the say command looked like this:
say text: the text to be spoken.
We learned that the word “text” is an argument or parameter of the say command. The word “text” is also a class. A class is a special category that shares characteristics or properties with other members in the class. AppleScript provides several built-in classes including, as we saw in the optional parameters for the say command, boolean. (A boolean is a logical value that evaluates to true or false). AppleScript also includes common classes such as list, alias, date, integer, and record. We will look at each of these as we come across them in our scripts. For now, let’s look closer at the text class.
In AppleScript, text is an ordered series of Unicode characters. As noted above, a class has certain properties. You can access these properties in your script to check for errors or get more information about your class. The text class (and every other class) includes a “class” property. The class property tells you what class your object is. Let’s look at this. Open up Script Editor and type the following (be sure to include the quotation marks):
class of “This is some text.”
Hit the “Run” button or Command+R. In the “Result” window at the bottom of Script Editor (click on the “Result” tab if it’s not already selected), you should see the word “text”. So what happened? We asked AppleScript to tell us the “class” property of some text. This may seem simple, but what you won’t always know the class of the thing you’re asking about. We’ll see this when we talk about variables. Knowing the class of an object is important because each class object responds differently to your commands. Note that you “declare” or indicate a text class by including the quotation marks around the characters in your text object. In some programming languages, you have to declare the type of object before you use it, but not in AppleScript.
Another property of the text class is “length”, which returns the number of Unicode characters in the text object. In our example above, the return value is 18.
You can also change or “coerce” one class to another class by using the keyword “as”. This is useful if you need to supply one class as a parameter to a command, but you have a different class. For example, if you ask a person for their age and they give you a number, but you want to say the number, then you need to coerce the number object to a text object. (Remember, the say command requires text as a parameter.) We can coerce our text object in our example above to a list object using the words “as list”. Try it. In Script Editor, type:
“This is some text.” as list
In the Result pane, you should see: {”This is some text.”}. The brackets {} indicate a list object. Not all classes can be coerced to other classes. For example, you cannot coerce a text object to a record object. If you attempt it, AppleScript will raise an error. Speaking of errors, that will be the subject of our next lesson. After that, we’ll start writing some scripts and investigate more concepts in AppleScript.
Leave a Reply