DekGenius.com
Team LiB   Previous Section   Next Section

Chapter 5. Reference Forms

This chapter describes the AppleScript reference forms, or the ways that you can specify or refer to one or more objects in AppleScript code. First we will describe the ten different reference forms, then the rest of the chapter provides a reference to the actual AppleScript reserved words (e.g., every, thru, whose) that you can use to identify or refer to objects in your code.

Here are the ten different reference forms:

Arbitrary Element

Using the reserved word some, AppleScript code can grab a random object in a container. Here is an example:

tell application "Finder"
    (* get a random image file from a desktop folder *)
    set randomImage to some file of folder "jpegs"
end tell

See the section on some.

Every Element

This type of reference form specifies every object of a certain class type in a container, such as:

tell application "Finder"
    set allFiles to every file in folder "today" (*  returns a list
    of file objects *)
end tell

See the section on every.

Filter

The Filter reference form specifies objects based on certain attributes, such as all files whose name ends with .txt. The where and whose reserved words are used in Filter references. See the whose section.

ID

The ID reference form can be used to grab an object based on the value of its ID property (if it has an ID property, that is.) The ID reference form is expressed in code with the AppleScript id reserved word. See the id section.

Index

The popular Index reference form specifies an object based on its numbered or indexed position in a container. The following example shows two ways to get the first file on a disk:

telll application "Finder"
    get file 1 of disk "backup"
    get first file of disk "backup"
end tell

See the sections on first and last.

Middle Element

The Middle Element reference form is designed to get the middle object of a certain class type in a container or the middle item of a list. See the section on middle.

Name

The Name reference form identifies an object by its name, as in "application 'Finder'". See the name section.

Property

Using the Property reference form, your script can grab the value of a property and store it in a variable, for instance. The property may derive from an application, a date object, a script object, or a record value. Here are three examples of using the Property reference form.

(* get the Finder's largest free block property *)
tell application "Finder" 
    set freeMemory to largest free block
end tell
(* returns the month property of a date object *)
set mon to the month of (current date)
(* gets the lastName property of a record object *)
set prezName to lastName of¬
{firstName: "Abraham", lastName: "Lincoln"}
Range

The Range reference form specifies a subset of objects within a container. The return value is a list, or the code raises an error if the container does not contain the specified range of objects. See the every...from...to section for some examples.

Relative

The Relative reference form describes an object based on its position compared with another object, such as in this example:

tell application " Finder"
    get the folder before the last folder in the startup disk
end tell

See the sections on after, back, before, and beginning.

Table 5-1 shows the reserved words that you can use to specify objects in AppleScript code, but not all of the English language synonyms that you can use with these forms. The synonyms are included under the Synonyms heading for each reserved word's section. The reference form is identified in parentheses.

Table 5-1. Reserved Words for Use with AppleScript Reference Forms

after (Relative)

id (ID)

back (Relative)

last (Index)

before (Relative)

middle (Middle Element)

beginning (Relative)

name (Name)

first (Index)

some (Arbitrary Element)

every (Every Element)

whose (Filter)

every...from...to... (Range)

    Team LiB   Previous Section   Next Section