Basics of AppleScript

Extracting Items From Lists

Script [6.5.1]:

set bag to {"books", "assignments"}
set engineering to the last item of bag

Explanation: Here a list named bag contains books and assignments. What if you want to obtain last item of list and you don’t know the size of list?? It’s simple. Just create a new variable and set it to the last item of listName. Here engineering is the variable which will contain the last item of bag that is assignments.

Figure 6.5.1

Figure 6.5.1 Getting Last Item From List

If the second statement was,

set engineering to the last item of bag

then output would be

Figure 6.5.1-2

Figure 6.5.1-2 Getting First Item From List

Script [6.5.2]:

set newspaper to {"articles", "author", "advertisements"}
set itemValue to item -1 of newspaper

Explanation: AppleScript allows us to retrieve value of items in the list using the keyword item -n where n is the number from 1 to the length of list.

Note: Last item is item -1. Subsequently item -2 is the item before last item and so on.

In the above example the list is named newspapers and contains articles, author and advertisements. The above Script will retrieve the last item and store it in itemValue variable.

Figure 6.5.2

Figure 6.5.2 Retrieving Last Item of List

If the second statement of Script was,

set itemValue to item -1 of newspaper

then output would be,

Figure 6.5.2-2

Figure 6.5.2-2 Retrieving Items From List