|
AppleScript Tutorial 4 - Class |
|
2004-03-30 |
http://www.barefeetware.com /applescript/tutorial/04/ |
|
Description |
An application is made up of objects. Each object belongs to a particular class, such as document, text, oval, menu or cell. |
|
AppleScript |
The home page of this AppleScript tutorial series. |
|
Previous Tutorial |
If you have not already completed tutorial 3, go back to learn how to open an applications AppleScript dictionary, and understand how its organized. |
|
Next Tutorial |
||
Under construction |
Note: I am in the middle of revising this whole tutorial for Mac OS X, and am part way through this page. I should be finished within a few days. |
|
Classifying ObjectsThis tutorial should answer these questions for you:
- What is an object and what is a class?
- How can I determine the class of a particular object?
SubclassAppleScript is an object oriented language. This means that AppleScript deals with objects, changing their properties and sending commands (events) to them.Exercise 1: Classifying Objects in a Drawing Document
Any identifiable particular "thing" is an object. Each object is classified as a particular class or, in other words, type of object. An object is a specific identifiable thing, whereas a class is the term you would use to describe all objects of the same kind. For instance, the thing I am sitting on is a chair. That tells you its class, but not which specific object. But if I told you that it is the tallest chair in the front office of my building, and gave you my address, that would uniquely identify this particular object. Or, my vehicle is of the class motorbike, but to identify which particular object it is, I could tell you the license plate details.
And each object might be classifiable in more than one way. That is, each object may belong to more than just one class. For instance, my chair is also furniture. So it belongs to the class chair as well as to the class furniture. My motorbike belongs to the classes motorbike, vehicle, transport, fun things to use on a Saturday and so on.
Each object in an application may be classed, for instance, as one of document, menu, oval, word, movie, a database record and so on. The dictionary lists all of these classes.
Lets consider the drawing document in Figure 1 and examine AppleWorks dictionary to determine the class of each object.Exercise 2: Writing a Script to Get the Class
- In AppleWorks, create a new drawing document and draw some objects, similar to Figure 1.
![]()
Figure 1: Objects in a Drawing
- If not already open, open AppleWorks AppleScript dictionary (eg by drag-and-dropping the AppleWorks icon onto the script editor icon).
- Browse through the list of suites (in the left panel of Script Editors dictionary window) to find AppleWorks Graphics Suite. Expand the graphics suite and the Class subsection. See Figure 2.
![]()
Figure 2: AppleWorks Graphics Suite
- Locate the class that most accurately describes each object that you drew. For instance, in Figure 1, each of the left three objects is classed as oval, the right two are classed as rectangle.
- Keep the drawing open for the next exercise.
Now well check your classification of each object, by getting AppleScript to give us the class of the selection. Dont worry at this stage about how the script itself works were just using it to display the class of an object.
- Select one of the objects in your drawing.
- In the script editors File menu, select New Script. A new blank script window should appear.
- Type the script from Figure 3 into the script window. Dont try to apply bold formatting or indents, just type the three lines as shown.
tell application "AppleWorks 6"
get class of selection
end tellFigure 3: Script to Get the Class of the Selection
- Click the Check Syntax button, or hit the Enter key on the keyboard. If you typed the script correctly, it should format as shown in Figure 4. If the script editor instead displays an error message, correct your script and try again.
![]()
Figure 4: Script Typed into a New Script Window
- Run the script, by clicking the Run button or hitting Command-R on the keyboard. A Results window should appear in the script editor, containing the word oval, rectangle or whatever is the class of your selected object. (If you are using AppleWorks version 5, this script will not run.)
Supplementary ExercisesEach object belongs to one class that best describes it. But sometimes that class is a subclass of another class.Exercise 3: Subclasses of Graphic Object
For instance, the circle in Figure 1 is precisely classed as an oval, but can more generally be classed as a graphic object. Thats because oval is a subclass of graphic object. Rectangle is also a subclass of graphic object. So it is true to say that all of the objects in Figure 1 are classed as graphic objects, but are more precisely subclassed as ovals and rectangles.
The dictionary tells us whether a class is a subclass of another by listing an inheritance property. We will discuss inheritance in depth in an upcoming tutorial.
What classes in the AppleWorks Graphics Suite are subclasses of the graphic object class? Lets see.
- In the AppleWorks dictionary, scroll down to the AppleWorks Graphics Suite.
- Select each class (in italics in the left pane) and note whether it lists <inheritance> graphic object -- subclass of graphic object as a property. See Figure 5. You should see that every class of object that you can draw (eg oval, rectangle, line, text frame) is a subclass of graphic object.
![]()
Figure 5: Text Frame is a Subclass of Graphic Object
ConclusionTry these supplementary tasks:
- Select some text in an AppleWorks word processing document. Run the script in Figure 3.
- Select a cell in an AppleWorks spreadsheet document. Run the script in Figure 3.
- Check the dictionary to identify the subclasses of cell and text.
© 1998 - 2004 BareFeetWareYouve identified the class of several objects. Class alone, however, is not enough to fully specify an object. In the next tutorial, well use the object model to identify objects as particular elements in a container.