Section 6.6. Inputting Lists


6.6. Inputting Lists

When you're dealing with lots of information in your AppleScripts, you'll often want to display lists onscreenand you've already learned how to, using choose from list. Just as often, however, you'll want to input liststyping in your list of favorite colors, for example. This section shows you how to create such lists, formed entirely by what you type into dialog boxes.

6.6.1. Adding Items One at a Time

The simplest way to add items to a list is to display a dialog box for each new item. For instance, the following script prompts you to enter 10 of your favorite colors for a list, one dialog box at a time:

set favoriteColors to {} --This list will store all the colors you enter repeat 10 times --You could substitute any number you want here     set newColor to text returned of (display dialog "Enter a new color:" ¬         default answer "")     set the end of favoriteColors to newColor --Append newColor to the list end repeat --Display the list of all the colors you've entered choose from list favoriteColors

Each time the repeat statement runs, the script prompts you for a new color to add to the list. (Once you input a color and press OK, your script automatically appends the color you entered to the end of the favoriteColors list, using the AppleScript command set the end.)

Once you've entered 10 colors, the script finishes by presenting a list of them. Now you, too, can astound your friends with your Mac's mastery of your favorite colors!

This method works fine if you want to add just a few items to a list, but you'll get quite annoyed if you have to enter, say, 50 items into separate dialog boxes. Plus, the sight of dialog box after dialog box is known to cause drowsiness in mice.

6.6.2. Adding Multiple Items at Once

A better option, if you want to add lots of things to your list, is to present a single dialog box and enter everything there. That method is a little harder to program but a lot easier to use.

The trick is to have your script sweep through your text, picking out the individual items and turning them into a list. You'll enter the items separated by a comma and then a space; your script uses the comma-space pattern as a delimitersomething that separates items in a list.

In AppleScript, you have to set delimiters yourself, like this:

set AppleScript's text item delimiters to ", "

However, AppleScript's text item delimiters are a system-wide setting; if you change them in your script, they'll change in all the other scripts running on your Mac as well. That's why it's good practice to store the existing text item delimiters before you change them, and then to restore the old text item delimiters at the end of your script:

set oldDels to AppleScript's text item delimiters set AppleScript's text item delimiters to ", " --Do whatever set AppleScript's text item delimiters to oldDels

Having done that, now you can split up a string into a list:

set oldDels to AppleScript's text item delimiters set AppleScript's text item delimiters to ", " set fruits to (every text item of "Kiwi, Passion, Papaya") --fruits is now a list: {"Kiwi", "Passion", "Papaya"} set AppleScript's text item delimiters to oldDels

Now that you know how to use text delimiters, you can input multiple list items in a single dialog box, like this:

set enteredText to the text returned of (display dialog ¬     "Enter all your favorite shapes, separated by commas and spaces.):" ¬     default answer "") --enteredText is now a string, with list items separated by comma-space pairs set oldDels to AppleScript's text item delimiters set AppleScript's text item delimiters to ", " --AppleScript now knows to split strings up at comma-space pairs set shapeList to (every text item of enteredText) --shapeList now contains every item from enteredText, stored in a list choose from list shapeList --Display the list in a dialog box set AppleScript's text item delimiters to oldDels

When you run the script, you'll see something like Figure 6-7.



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