Section 10.2. Making a Simple AppleScript Database


10.2. Making a Simple AppleScript Database

AppleScript records can store just about any type of information you want. In the previous scripts, for example, you stored numbers and strings of text. But suppose you had the information from Table 10-1 and wanted to create a database from that.

Table 10-1. A sample of information you can put in a database

Name

Weight (in pounds)

Height (in inches)

Rooster?

Matthew

20

26.5

Yes

Colonel

15

22.3

Yes

Little

3

10.9

No


Here's how you'd encode that information in AppleScript:

set myChicken to {nickname:"Matthew", weightInPounds:20, ¬     heightInInches:26.5, rooster:true} set kfcChicken to {nickname:"Colonel", weightInPounds:15, ¬     heightInInches:22.3, rooster:true} set chickenLittle to {nickname:"Little", weightInPounds:3, ¬     heightInInches:10.9, rooster:false} set myDatabase to {myChicken, kfcChicken, chickenLittle} --Creates a database

As you can see, a simple AppleScript database is no more than a list of individual records. (In fact, even when you work with commercial database programs, you'll find that they're really just clusters of records, too.)

This script stores three separate kinds of data in myDatabase: strings for a chicken's nickname, numbers for a chicken's weightInPounds and heightInInches, and Boolean values (Sidebar 5.5) to specify whether the chick is a rooster. AppleScript keeps each type of information separate, so it won't get confused if you use three, four, or even more kinds of information in your database records.

10.2.1. Searching an AppleScript Database

For most things in AppleScript, you can use the every keyword to narrow down a list of items to a few that match certain criteria (Section 6.3). Unfortunately, this neat trick doesn't work for finding records within an AppleScript database.

To work around this limitation, you have to iterate (Section 6.4) through the database, checking each record individually to see if it matches your criteria. Obviously this is an annoying, slower way of searching, but it's unfortunately your only choice for AppleScript databases.

Here, for example, is how you'd search through that chicken database to find the chickens that weigh more than 10 pounds:

set myChicken to {nickname:"Matthew", weightInPounds:20, ¬     heightInInches:26.5, rooster:true} set kfcChicken to {nickname:"Colonel", weightInPounds:15, ¬     heightInInches:22.3, rooster:true} set chickenLittle to {nickname:"Little", weightInPounds:3, ¬     heightInInches:10.9, rooster:false} set myDatabase to {myChicken, kfcChicken, chickenLittle} --Part 1: set heavyChickens to {} --Part 2: repeat with currentRecord in myDatabase     --Part 3:     if the weightInPounds of currentRecord is greater than 10 then         set heavyChickens to (heavyChickens & the nickname of currentRecord)     end if end repeat --Part 4: choose from list heavyChickens with prompt ¬     "Chickens weighing more than 10 pounds:"

Here's how the new code works:

  • Part 1 creates a new list, heavyChickens, which holds the names of the matching, heavier-than-10-pound chickens later in your script.

  • Part 2 starts a repeat statement, assigning currentRecord to the next record in myDatabase each time the loop repeats. In other words, this section ensures that the script will process every chicken by the time the script is finished.

  • Part 3 checks the current chicken to see if it weighs more than 10 pounds. If the chicken does, the script adds that chicken's nickname to the heavyChickens list. If the chicken weighs less than 10 pounds, though, the repeat statement simply proceeds to examine the next chicken.

  • Part 4 displays a list of all the chickens weighing more than 10 pounds (Figure 10-1), thereby completing your search.

Figure 10-1. The results of your chicken search. If you want to display the actual weight of the chickens (rather than their nicknames), replace the nickname with the weightInPounds inside your script.


Now that you know how to create, modify, and search databases, you can get down to real business: making databases out of useful information.



AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net