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
Tutorial

 

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 application’s AppleScript dictionary, and understand how it’s 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.

     


Objectives
This tutorial should answer these questions for you:
    1. What is an “object” and what is a “class”?
    2. How can I determine the class of a particular object?

Classifying Objects
AppleScript is an “object oriented” language. This means that AppleScript deals with objects, changing their properties and sending commands (events) to them.

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.

Exercise 1: Classifying Objects in a Drawing Document
Let’s consider the drawing document in Figure 1 and examine AppleWorks’ dictionary to determine the class of each object.

    1. In AppleWorks, create a new drawing document and draw some objects, similar to Figure 1.

           
       

       

       
           
       

      Figure 1: Objects in a Drawing

       



    2. If not already open, open AppleWorks’ AppleScript dictionary (eg by drag-and-dropping the AppleWorks icon onto the script editor icon).
    3. Browse through the list of suites (in the left panel of Script Editor’s dictionary window) to find “AppleWorks Graphics Suite”. Expand the graphics suite and the “Class” subsection. See Figure 2.

           
       

       

       
           
       

      Figure 2: AppleWorks Graphics Suite

       



    4. 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”.
    5. Keep the drawing open for the next exercise.

Exercise 2: Writing a Script to Get the Class
Now we’ll check your classification of each object, by getting AppleScript to give us the class of the selection. Don’t worry at this stage about how the script itself works – we’re just using it to display the class of an object.

    1. Select one of the objects in your drawing.
    2. In the script editor’s File menu, select “New Script”. A new blank script window should appear.
    3. Type the script from Figure 3 into the script window. Don’t try to apply bold formatting or indents, just type the three lines as shown.

           
        tell application "AppleWorks 6"
         get class of selection
      end tell
       
           
       

      Figure 3: Script to Get the Class of the Selection

       



    4. 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

       



    5. 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.)

Subclass
Each object belongs to one class that best describes it. But sometimes that class is a subclass of another class.

For instance, the circle in Figure 1 is precisely classed as an “oval”, but can more generally be classed as a “graphic object”. That’s 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.

Exercise 3: Subclasses of Graphic Object
What classes in the AppleWorks Graphics Suite are subclasses of the graphic object class? Let’s see.

    1. In the AppleWorks dictionary, scroll down to the “AppleWorks Graphics Suite”.
    2. 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

       



Supplementary Exercises
Try these supplementary tasks:
    1. Select some text in an AppleWorks word processing document. Run the script in Figure 3.
    2. Select a cell in an AppleWorks spreadsheet document. Run the script in Figure 3.
    3. Check the dictionary to identify the subclasses of “cell” and “text”.

Conclusion
You’ve identified the class of several objects. Class alone, however, is not enough to fully specify an object. In the next tutorial, we’ll use the object model to identify objects as particular elements in a container.


© 1998 - 2004 BareFeetWare
Please email us any queries about this page.