6.2. Displaying ListsOnce 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.
6.2.1. Getting the Selected ItemWhen 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.
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 & "."
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).
|