Section 6.1. Common List Commands


6.1. Common List Commands

Having a list is convenient for storing information, but that won't do much good if you can't access the information. Luckily, AppleScript gives you several ways to get to the data in your list:

  • You can use the item keyword to access a specific item in your list, like this:

set lowCarbFoods to {"Lettuce", "Celery", "Water"} set mySnack to item 2 of lowCarbFoods

  • In this example, item 2 of lowCarbFoods would be "Celery"and so would the mySnack variable. (The items in a list each get assigned a number, starting with 1 for the first item, so "Celery" would be item 2 in the previous list.)

  • If you'd prefer, you can specify a certain item in your list by using numerical adjectives: first, third, eighth, and so on. For instance, writing item 2 of lowCarbFoods is the same as writing the second item of lowCarbFoods.

  • You can access multiple items in your list by using the items keyword (notice the s at the end). Using that keyword, you can create a smaller list that contains a portion of a bigger list, like this:

set greetings to {"Hey", "Hello", "Howdy", "Yo", "Hi"} set myFavorites to items 2 through 4 of greetings --myFavorites is now {"Hello", "Howdy", "Yo"}

If you're too tired to type out through, you can use AppleScript's accepted misspelling: thru.

  • If you want to create a new list with noncontiguous items from an old list, you have to surround in curly brackets the specific items that you want to include:

set greetings to {"Hey", "Hello", "Howdy", "Yo", "Hi"} set myFavorites to {item 4 of greetings, item 1 of greetings} --myFavorites is now {"Yo", "Hey"}

  • Finally, you can determine how many items are in a list by using the count command:

set importantFolders to {alias ":Applications:", alias ":System:"} set numItems to (count importantFolders) --numItems now contains 2, since there are 2 items in the importantFolders list

For more on the count command, see Sidebar 4.3.



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