AppleScript Tutorial 5 - Elements




Each object in an application exist inside another object. Each class that can contain other objects lists its elements in the dictionary.



The index of the AppleScript tutorial series.


This tutorial should answer these questions:
    1. What is the “object model”?
    2. How can I refer to a specific object?

Elements in Containers
Many (not all) classes of object may contain other classes of objects. In this relationship, a container contains elements.

For example, in Figure 1 (from the previous tutorial) the document’s drawing area contains some graphic objects. The drawing area is the container and the graphic objects are the elements. Each graphic object (such as an oval or rectangle) is an element in the drawing area. But an oval or rectangle cannot have any elements of its own.




Figure 1: Graphic Object Elements in a Drawing Area Container


Some elements may themselves be containers of other elements. For example, a paragraph contains words and a word contains characters. The characters are elements in the word, the words are elements in the paragraph, the paragraphs are themselves elements in the text body of the document or a text frame.

A spreadsheet contains cell elements. A menu contains menu items. A database can contain fields, reports, layouts, sort orders, search criteria, records and requests. A record and request both contain fields.

For each class, the script editor lists any possible elements. In the dictionary, the description of a class includes the “Elements” title if any elements can exist within that class.

Exercise 1: Database Elements
Let’s examine the elements of the database class.
    1. Open the AppleWorks AppleScript dictionary in the script editor.
    2. Select the “database layer” class (in AppleWorks 5, it is “database”), within the “AppleWorks Database Suite” (see Figure 2). The database layer class lists elements including layout, field and record.




      Figure 2: Database Elements in Script Editor


    3. Cross-reference “layout” by selecting it in the left pane. Note that it does not list any elements of its own.
    4. Look up “record” (ie select “record” in the left pane). Record does have a single element, namely “field”.

Object Model
The structure of elements within other elements (such as field in record in database), forms the basis of an application’s object model. The object model describes the relationship between the various objects within an application.

Some script editors can graphically display parts of the object model. Script Debugger and Scripter display the hierarchy of elements in a container (see Figure 3 and Figure 4). These tools can help you visualize the object model of an application’s classes. The same information is shown by Apple’s Script Editor in text form (see Figure 2), but requires you to manually cross reference elements within elements.

Exercise 2: Graphical View of the Object Model in Script Debugger
This exercise requires Script Debugger. You can download a demo version via the link on the script editors page. Otherwise, you can just read this exercise without completing the tasks.
    1. Drag and drop the AppleWorks icon onto Script Debugger.
    2. Click the “Object Model” tab.
    3. In the “Root Class” pop-up menu, select “database layer” (for AppleWorks 5, select “database”).
    4. In the diagram that appears, click the “+” icon in the “record” box to expand it to show the elements in “record”. See Figure 3.




      Figure 3: Database Elements in Script Debugger


Exercise 3: Graphical View of the Object Model in Scripter
This exercise requires Scripter. You can download a demo version via the link on the script editors page. Otherwise, you can just read this exercise without completing the tasks.
    1. Drag and drop the AppleWorks icon onto Scripter.
    2. Hold down the Control key on the keyboard and double-click the AppleWorks icon appears in Scripter’s application bar.
    3. In the Object Map window that appears, click the expansion triangle button next to “database layer” (for AppleWorks 5, it is “database”).
    4. Click the expansion triangle button next to “record” to reveal the element within it. See Figure 4.




      Figure 4: Database Elements in Scripter


When writing a script to refer to an object, you must identify it in its hierarchy structure of containers and elements. You do this by using the term in. The general syntax for this is:

element in container

such as:

record 2 in database
field 4 in record 2

You can use the term “of” instead of “in”, but this tutorial series uses “in” for elements and “of” for properties to clarify the difference. More about this later.

Reference Forms
Since a container can contain any number of elements, you must identify the specific element that you are referencing. For instance, it is not sufficient to say “graphic object in drawing area”, “cell in spreadsheet” or “record in database” because there can be multiple graphic objects, cells or records.

Usually, the dictionary tells you exactly how to reference an element. Most of the element listings are followed by “by name, by numeric index” (see Figure 2). These are the allowed reference forms for that element.

Figure 2 implies that you can specify an element in a database by including its name or number. Here are some examples:

record 4 in database
report "Taxation" in database
report 2 in database
layout 3 in database

And for other suites:

graphic object 3 in drawing area
oval 2 in drawing area
cell "A3" in spreadsheet
row 4 in column 3
menu item "Save" in menu "File"

AppleScript understands synonyms for certain numeric indexes. For example,“1st”, “first” and “front” refer to the first element. Therefore, the following examples are equivalent to each other:

record 1
first record
front record
1st record

We will discuss other reference forms, such as “as a range of elements”, “satisfying a test” and other numeric reference forms (such as “back”, “middle” and “last”) in future tutorials.

Every object is ultimately an element in its application. So the following AppleScript code refers to record 4 in the database of the front document in AppleWorks:

record 4 in database of front document in application "AppleWorks"

Exercise 4: Selecting a Specific Cell
Cells in a spreadsheet may be referenced by name (eg “B3”) or as a row number in a column number. Here you’ll try both reference forms. For now, don’t be concerned with the “tell” statements or “of front document”.

    1. In AppleWorks, make a new spreadsheet document.
    2. In the script editor, make a new script document (ie in the File menu, select “New Script”).
    3. Enter the script from Figure 5 into the script document.

        tell application "AppleWorks 6"
         select cell "B3" in spreadsheet of front document
      end tell

      Figure 5: Script to Select a Specific Cell


    4. Run the script (eg by selecting the “Run” menu item).
    5. In AppleWorks, check which cell was selected.
    6. Change the script to be that shown in Figure 6.

        tell application "AppleWorks 6"
         select row 4 in column 2 in spreadsheet of front document
      end tell

      Figure 6: Script to Select a Row in a Column


    7. In AppleWorks, check that cell B4 (ie row 4 in column 2) was selected.

Supplementary Exercises
Try these supplementary tasks:
    1. Examine the object model of the elements in the “text” class, “cell” class and “menu” class.
    2. Write a script to “select menu item "Copy" in menu "Edit"”. You’ll need to select something in AppleWorks before running the script.

So far, we’ve covered several classes and examined how some classes can be elements in others. We can identify an object by using a name or numeric reference form and quoting its container(s). Next we’ll change some of these objects’ properties.

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