Contents
How to insert an item into a list?
The handlers in Listing 21-45 and Listing 21-46 insert an item into a list. Provide the item to insert, the list, and the position where the item should be inserted. Note that position can be specified in relation to the end of the list by using a negative number.
What can a script do with a list?
A script can loop through the items of a list in order to process the items individually. There are many other tasks scripts commonly performed with lists, such as joining and sorting, which usually require custom scripting.
How to loop through a list of items?
A script can also loop through a list of items more directly by dynamically assigning a list item to a variable. In Listing 21-3 and Listing 21-4, a variable— theCurrentListItem in AppleScript and currentArrayItem in JavaScript—represents the item matching the current loop. display dialog theCurrentListItem & ” is an item in the list.”
How to insert items into an array in JavaScript?
In JavaScript, the Array class has built-in methods— unshift (inserts at the beginning), splice (inserts at a specific position), and push (inserts at the end)—for inserting items into a list, requiring less custom scripting than is necessary in AppleScript.
How to sort list of items in JavaScript?
The handler in Listing 21-65 sorts a list of strings or numbers in AppleScript. Listing 21-66 shows how to call the handler in Listing 21-65 . To perform a reverse (descending) sort, use the reverse command, as shown in Listing 21-67 . In JavaScript, the Array object has a sort method, which sorts the array’s items. See Listing 21-68 .
How to determine the position of an item in a list?
Determining the Position of an Item in a List 1 on getPositionOfItemInList (theItem, theList) 2 repeat with a from 1 to count of theList 3 if item a of theList is theItem then return a 4 end repeat 5 return 0 6 end getPositionOfItemInList More
How to increase the number of items in a list?
In these examples, a variable— a in AppleScript and i in JavaScript—represents an integer value from 1 through the number of items in the list. Each loop causes this variable value to increase, and you can use the increment variable to target a specific list item. display dialog theCurrentListItem & ” is item ” & a & ” in the list.”
How to call a handler on a list in JavaScript?
Listing 21-21 shows how to call the handler in Listing 21-20. In AppleScript, list item positions start at 1 —the first item in a list has a position of 1 . In JavaScript, the indexOf () method of the Array object can be called to determine the position of an item in an array, as shown in Listing 21-22.
How to cross reference a list with JavaScript?
The getPositionOfItemInList () AppleScript handler and indexOf () JavaScript method can be used to cross-reference data between corresponding lists. In Listing 21-23 and Listing 21-24, a person is located in a list by name. Next, the person’s phone extension is located in a corresponding list.