Section 6.2. Displaying Lists


6.2. Displaying Lists

Once you've created a list, you'll often want to present it onscreen. Just like the display dialog command for strings (Section 3.1.1) and the choose file command for files (Section 5.6), AppleScript provides a choose from list command for displaying lists in a dialog box. Try running this script:

set myWishes to {"A car", "A house", "A vacation", "A spouse", "A life"} choose from list myWishes

You should see a dialog box like Figure 6-1, from which you can choose any item in the list.

Figure 6-1. The choose from list command is among the most useful in AppleScript. To select an item, either click it and press OK, or double-click the item.


6.2.1. Getting the Selected Item

When you select something in a choose from list dialog box, AppleScript provides your selection back to the scriptas another list. Say you modified your previous script, like this:

set myWishes to {"A car", "A house", "A vacation", "A spouse", "A life"} set myChoice to (choose from list myWishes)

At the end of the script, the myChoice variable would hold a list containing a single item: whatever you selected from the dialog box. If you want to display your choice in a new dialog box, however, you have to convert myChoice to a string first. That's because the display dialog commandunlike choose from listexpects to be given a string, not a list.

Luckily, it's easy to convert a list into a string. You simply have to use AppleScript's as keyword, like this:

set myWishes to {"A car", "A house", "A vacation", "A spouse", "A life"} set myChoice to (choose from list myWishes) display dialog (myChoice as string)

This sort of operation, where you turn one type of information into another, is known as a coercion.

You can perform coercions on many types of information. For example:

set myString to (10 as string) --10 gets coerced from a number to a string: "10" set someNumber to ("57.2" as number) --"57.2" gets coerced from a string to a number: 57.2 set theString to ({"Pop"} as string) --{"Pop"} gets coerced from a list to a string: "Pop"

Now, since your script coerces myChoice into a string, AppleScript can properly display your selection in a dialog box of its own (Figure 6-2).

But that's pretty boring, don't you think? To spice things up a bit, you can alter the script so it takes the value of myChoice and inserts it into a plain-English phrase:

set myWishes to {"a car", "a house", "a vacation", "a spouse", "a life"} set myChoice to (choose from list myWishes) display dialog "Man, I could really use " & myChoice & "."

As part of altering this script, you'll need to change the uppercase A's in the myWishes list to lowercase a's. That way, when you run the script, the "a" fits in nicely with the sentence you display in your dialog box.

Now when you select an item from the list's dialog box and click OK, AppleScript inserts your choice (myChoice) into the display dialog command's string. You'll see a dialog box with a message like "Man, I could really use a vacation" (replacing "a vacation", of course, if you selected something else you're in need of).

Figure 6-2. It's a good thing you converted the choose from list command's result into a string. Otherwise, when you ran the display dialog command, you would have ended up with an error dialog box instead of this one.




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