Finding Scriptable Objects with AppleScript
- Location or Property
- Finding Items by Location
- Finding Items by Property
- Chapter Summary
- What's Next?
- Location or Property
- Finding Items by Location
- Finding Items by Property
- Chapter Summary
- What's Next?
This chapter is the second in a series of five chapters about identifying, finding, and manipulating scriptable objects in applications.
Finding specific scriptable objects is a task common to most scripts. For example, a script might locate all the TIFF image files in a given folder that were modified today. Or a script might locate all the occurrences of a specific phrase in a story and replace them with another phrase. Finding scriptable objects is a common task and something that AppleScript and scriptable applications do exceptionally well. In this chapter, we'll cover the techniques used to locate specific scriptable objects.
Location or Property
Imagine you're standing in front of a group of people seated in a small auditorium that contains five rows of seats separated into two groups by an aisle that runs down the center of the room to where you are standing.
Each row contains five seats, with five on each side of the center aisle, and every seat contains a person. Your job is to choose five of the assembled people to take with you on a vacation to Mexico.
Figure 4.1 The full auditorium.
You could make the selection process easy by choosing to take all the people seated in the first row to your left:
Figure 4.2 Everybody in the first row on the left.
Or you could choose to take the people seated in the second row to your right:
Figure 4.3 Everybody in the second row on the right.
Or you could choose to take the people seated in the last row on the right:
Figure 4.4 Everybody in the last row on the right.
Or even the row before the last row on the left:
Figure 4.5 Everybody in the row before the last row on the left.
You might choose to take the last person in each row on your left:
Figure 4.6 The last person in every row on the left.
Or you might choose to take the first person in each row on your right:
Figure 4.7 The first person in every row on the right.
Or you might choose to take the every other person in the entire third row:
Figure 4.8 Every other person in the third row.
Or you might choose to take the every other person in the entire fourth row starting with the second person in that row:
Figure 4.9 Every other person in the fourth row, starting with the second person.
You could even use a geometric pattern for selecting by choosing the first person in the first row, the second person in the second row, and so on.
Figure 4.10 The first person in the first row, second person in the second row, and so on.
Or you could just take the first five random people chosen by drawing tickets containing their seat numbers out of a hat.
Figure 4.11 Five people chosen at random.
Now, if you don't mind traveling with strangers who have nothing in common with you, choosing people by where they are seated might be an acceptable solution. However, if you'd like to share your exotic vacation with those more like yourself, you could try another approach.
For example, if age is an important consideration to you, you could ask everyone whose age is within five years of yours to stand up and then choose five people from amongst those standing.
Figure 4.12 Everybody whose age is within five years of yours.
If gender is your most important consideration, you could ask all men or women to stand up and then choose five people from among those standing.
You could even make your selection very specific by asking all people of a specific gender whose age is within five years of yours and who like to dance to salsa music to stand up and then choose five people from among those standing.
As you see from this scenario, there are two ways to find items: by location and by property.
- Location in this example is a person's place in the order in which people were seated.
- Property is a person's age, gender, and so on.
You will use these two techniques in most of the scripts you write that locate and process scriptable objects.