O'Reilly logo

AppleScript: The Missing Manual by Adam Goldstein

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Joining Lists Together

On Section 4.3 you learned how to use ampersands to link strings together (to concatenate them, in nerd-ese). You can do the same thing for lists, too, which is a great way to join related items under one virtual roof. For instance:

--Example 1:
set combinedList to {"Tomato", "Carrot", "Rutabega"} & {"Broccoli", "Pepper"}
--combinedList is now {"Tomato", "Carrot", "Rutabega", "Broccoli", "Pepper"}

--Example 2:
set bigList to (items 1 thru 3 of combinedList) & ¬
    (items 2 thru 4 of combinedList)
(* bigList is now {"Tomato", "Carrot", "Rutabega", "Carrot", "Rutabega", 
   "Broccoli"} *)

--Example 3:
set luckyNumbers to {1, 6, 28} & 496
--luckyNumbers is now {1, 6, 28, 496}

These three examples each illustrate an important aspect of concatenating lists:

  • Example 1 shows how you can concatenate two separate lists to create one unified list. That would be useful if you wanted to merge a list of your first-grade and second-grade students, for example, into a single all-student master list.

  • Example 2 demonstrates that you can concatenate different sections of the same list. If you wanted to put together a list of only students whose names start with A, E, I, O, and U (for your vowel-themed class party), for instance, you could use concatenation to assemble a new list from these separate subgroups (the list of students whose first names begin with A, concatenated with the list of students whose first names begin with E, and so on).

  • Example 3 shows that you can append a single ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required