Publishers of technology books, eBooks, and videos for creative people

# Finding Scriptable Objects with AppleScript

• Print
This chapter is from the book

## Chapter Summary

In this chapter, you learned how to locate items in the Finder by referencing their numeric location in their parent container or by searching for items based on the value of their properties.

In upcoming chapters, you'll put these principles to use when you write scripts that perform Finder actions based on queries.

The following are the important concepts from this chapter regarding creating queries.

### Index or Property

Items in a container can be located by numeric location (index) or by the values of their properties.

### Index

Every item in a container is represented by a numeric value corresponding to its location in the list of items in the parent container. This value is the item's index.

```   item 4 of folder "Documents" of home
```

### Positive or Negative Index

Items in a list of items can be referenced incrementally using positive numeric values counting from left to right or using negative numeric values counting from right to left. For example, a list of four items when indexed from left to right would be as follows:

```   {item 1, item 2, item 3, item 4}
```

The same list of items referenced in the same order but counting from right to left would be as follows:

```   {item –4, item –3, item –2, item –1}
```

Item -1 is always the last item in a list.

So, in the following list of planets, Venus has an index value of either 2 or –8, depending on whether it is referenced from the left or right of the list (and assuming you think Pluto is a planet and all those other Kuiper belt objects aren't):

```   {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus",
"Neptune", "Pluto"}
```

### Range

Index values can be used to describe a subset of items in a larger list of items. Ranges can be described with either positive or negative indexes or a combination of both methods:

#### Script 4.35.

```   items 2 thru 4 of {"Mercury", "Venus", "Earth", "Mars", "Jupiter",
"Saturn", "Uranus", "Neptune", "Pluto"}
--> returns: {"Venus", "Earth", "Mars"}
```

#### Script 4.36.

```   items 2 thru –6 of {"Mercury", "Venus", "Earth", "Mars", "Jupiter",
"Saturn", "Uranus", "Neptune", "Pluto"}
--> returns: {"Venus", "Earth", "Mars"}
```

### Descriptive Index

Because AppleScript was designed to be written in a "conversational" manner, you can use more descriptive names for index values.

AppleScript supports location word indexes for the numeric values 1 through 10, like so:

```   first, second, third, fourth, fifth, sixth, seventh, eighth, ninth,
and tenth
```

You can also use descriptive numeric suffixes for any index value, like so:

```   1st, 23rd, 4064th
```

Items can be referenced in terms of synonyms for their place in the list:

```   front, first, back, last, middle
```

### Relative Location

The terms before and after can be used in relative location references:

```   item after the front item
item before the last item
item after the middle item
```

### Random

Items can be referenced at random by using the term some:

```   Some item of {"Venus", "Earth", "Mars"}
```

### Property

Items can be referenced by comparing the value of their properties to a desired set of values. For example:

```   every paragraph whose first character is "A"
```

or for example:

```   the first document file whose name contains "Smith Project"
```

A query using a property reference begins with a term indicating how many or which items are to be referenced.

This is followed by the term whose or where.

This is then followed by a comparison operator determining the manner in which the item's property value is compared.

The desired property value completes the query.

Here are variations of each component of the property reference query.

Location indicator:

```   the first document file whose name contains "Smith Project"
the last document file whose name contains "Smith Project"
every document file whose name contains "Smith Project"
```

Comparison operator:

```   every document file whose name begins with "Smith Project"
every document file whose name ends with "Smith Project"
every document file whose name contains "Smith Project"
every document file whose name does not contain "Smith Project"
```

Property value:

```   every document file whose name contains "Smith Project"
every document file whose name contains "Revised"
```

### Compound Property Queries

The scope of queries can be made more specific or broader by using the term and to add search parameters and the term or to specify optional search parameters:

```   every document file whose name contains "Revised" and ¬
kind is "JPEG image"

every document file whose kind is "JPEG image" or ¬
name extension is "jpg"
```

Both terms can be combined to create complex search queries:

```   every document file whose (kind is "JPEG image" or ¬
name extension is "jpg") and ¬
name contains "Smith Project"
```