Basics of AppleScript

Modifying Lists

Script [6.4.1]:

set seasons to {"summer", "spring", "winter"}
set item 2 of seasons to "monsoon"
get seasons

Explanation: Here a list named seasons is created. It contains summer, spring and winter. However I want to modify the 2nd item. So with the help of second statement, I modified spring to monsoon. Finally I used the get command to print the new modified list.

Figure 6.4.1

Figure 6.4.1 Modified List

Script [6.4.2]:

set buy to {"phone", "usb", "pc"}
set 2nd item of buy to "pen drive"
get buy

Explanation: Here a list named buy is created. It contains phone, usb and pc. However I want to modify the 2nd item. So with the help of second statement, I modified usb to pen drive. Finally I used the get command to print the new modified list. This is another way of modifying a list. The only difference is in item 2 and 2nd item.

Figure 6.4.2

Figure 6.4.2 Modified Lists (Different Method)

Script [6.4.3]:

set musicList to {"songs", "lyrics", "artists"}
set first item of musicList to "albums"
set last item of musicList to "playlists"
get musicList

Explanation: This is another method to modify the first and last item of the list by simply using the command first item or last item. Here the list is named musicList. It contains songs, lyrics and artists. The second and third statement sets the first and last item of the list to albums and playlists respectively. Finally the get command is used to print the list.

Figure 6.4.3

Figure 6.4.3 Modifying First & Last Item of the List