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.
Elements in ContainersThis tutorial should answer these questions:
- What is the object model?
- How can I refer to a specific object?
Object ModelMany (not all) classes of object may contain other classes of objects. In this relationship, a container contains elements.Exercise 1: Database Elements
For example, in Figure 1 (from the previous tutorial) the documents 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.
Lets examine the elements of the database class.
- Open the AppleWorks AppleScript dictionary in the script editor.
- 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
- Cross-reference layout by selecting it in the left pane. Note that it does not list any elements of its own.
- Look up record (ie select record in the left pane). Record does have a single element, namely field.
Exercise 3: Graphical View of the Object Model in ScripterThe structure of elements within other elements (such as field in record in database), forms the basis of an applications object model. The object model describes the relationship between the various objects within an application.Exercise 2: Graphical View of the Object Model in Script Debugger
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 applications classes. The same information is shown by Apples Script Editor in text form (see Figure 2), but requires you to manually cross reference elements within elements.
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.
- Drag and drop the AppleWorks icon onto Script Debugger.
- Click the Object Model tab.
- In the Root Class pop-up menu, select database layer (for AppleWorks 5, select database).
- 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
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.In
- Drag and drop the AppleWorks icon onto Scripter.
- Hold down the Control key on the keyboard and double-click the AppleWorks icon appears in Scripters application bar.
- In the Object Map window that appears, click the expansion triangle button next to database layer (for AppleWorks 5, it is database).
- Click the expansion triangle button next to record to reveal the element within it. See Figure 4.
Figure 4: Database Elements in Scripter
Reference FormsWhen 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
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.
Supplementary ExercisesSince 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.Exercise 4: Selecting a Specific Cell
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:
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"
Cells in a spreadsheet may be referenced by name (eg B3) or as a row number in a column number. Here youll try both reference forms. For now, dont be concerned with the tell statements or of front document.
- In AppleWorks, make a new spreadsheet document.
- In the script editor, make a new script document (ie in the File menu, select New Script).
- Enter the script from Figure 5 into the script document.
tell application "AppleWorks 6"
select cell "B3" in spreadsheet of front document
Figure 5: Script to Select a Specific Cell
- Run the script (eg by selecting the Run menu item).
- In AppleWorks, check which cell was selected.
- 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
Figure 6: Script to Select a Row in a Column
- In AppleWorks, check that cell B4 (ie row 4 in column 2) was selected.
ConclusionTry these supplementary tasks:
- Examine the object model of the elements in the text class, cell class and menu class.
- Write a script to select menu item "Copy" in menu "Edit". Youll need to select something in AppleWorks before running the script.
© 1998 - 2000 BareFeetWareSo far, weve 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 well change some of these objects properties.