DekGenius.com
Team LiB   Previous Section   Next Section
list

Allowed coercion

string, if the data type of each item in the list can legally be coerced to a string

A single-item list (such as {44}) can be coerced to any data type that the item could be coerced to if it were not in a list. For example, {44} could be cast or coerced to a string, because 44 is an integer, and AppleScript permits that data type to be coerced to a string.

Syntax

set theList to {"Mercury","Mars",pi,3.14} as list

Description

An AppleScript list is close to what other languages such as Perl or Java would call an array. In AppleScript, you can store items of any data type in a list, even other lists. You can mix data types among list items (store strings, numbers, and other objects in the same list). The items as a group are surrounded by curly braces and separated by commas. For example:

set theList to {"Mercury","Mars",pi,3.14}

includes two strings, the pi predefined variable (which AppleScript will evaluate to about 3.14159265359), and a real number.

A list is a data type that you will encounter often in AppleScript. Several AppleScript commands return lists, such as getting every item in a container (e.g., get every folder of desktop) You can use the following properties with a list:

class

Always returns list. Test a return value or variable to find out if it is a list by using this property, as in:

class of theList
length

Returns the number of items in a list, as in:

length of theList
rest

Returns a value of type list containing every item but the first one.

reverse

Returns a value of type list containing all the items of the original list but in reverse order (e.g., reverse of theList—returns {3.14, pi, "Mars", "Mercury"}).

Examples

You can use several different reference styles to grab individual items from a list, such as the first, last, or middle reference methods (e.g., first item of theListMercury). You can use integers as reference methods, such as:

1000th item of theList

(if the list included at least 1000 items). You can also access subgroups within a list by referring to them as a range. For example:

items 1 thru 3 of theList

returns a list containing the first, second, and third entries in the theList variable. You can refer to any list value by referencing its list position, as in item 3 of theList. Lists are not "zero indexed" by default in AppleScript. The first position in an AppleScript list is occupied by item 1.

AppleScript lists are very supple; you can "concatenate" two lists to make a bigger single list. The following code uses the concatenation operator (&) to combine two lists:

set theList to {"Mercury", "Mars", pi, 3.14} as list
set secondList to {"Neptune", 2000, "NutShell"}
set comList to theList & secondList 
-- results in {"Mercury", "Mars", 3.14159265359, 3.14, "Neptune", 2000, 
"NutShell"}

If you concatenate a list data type with a string, integer, real, or boolean data type, AppleScript adds the value to the end of the original list. For instance:

set theList to theList & "Let me in"

results in a new list with the "Let me in" string added to the end of it. This makes it very simple to dynamically add data to an existing list, whether the data are other lists, strings, numbers, or different classes.

You get a different result when you try to concatenate a list of values to a string. AppleScript first coerces theList to a string. All of the list items are jammed together and separated by AppleScript's default text item delimiters, which is the empty string (""). Thus when list items are converted to a string the result is often unreadable—a string of characters with no spaces separating them. This example is one solution to prettying up a string that was formerly a list:

(* save a reference to AppleScript's default text item delimiter, which is an 
empty string, "" *)
set defaultDelim to text item delimiters
set text item delimiters to return
tell application "Finder"
   set folList to (name of (items of desktop whose kind is "folder"))¬
as list
   set folList to folList as string
end tell
set text item delimiters to defaultDelim
display dialog folList

The best solution to coercing a list to a string is to temporarily change AppleScript's text item delimiters. The prior example first saves the default text item delimiters in a variable, so we can return AppleScript to its default string behavior after the script runs. Then the text item delimiters value is changed to the return predefined variable, which, when used in this manner, is a return character (displaying the string following it on the next line). The script then tells the Finder to get a list of all the names of the desktop's folders and coerce the list to a string. Since I changed the text item delimiters to a return character, this will create a string that lists each folder name on a separate line. This string, or at least a portion of it (depending on how cluttered the desktop is with folders), is displayed to the user using the display dialog scripting addition. Finally, the script also resets the text item delimiters to the AppleScript default (an empty string, "").

    Team LiB   Previous Section   Next Section