AppleScript_ The Definitive Guide - Matt Neuburg [129]
Another way to see that the Finder's file class is not AppleScript's file class is to try handing an AppleScript file object to the Finder. You can't do it:
set f to a reference to file "feathers:Users:mattneub:myFile"
tell application "Finder"
get owner of f
-- error: Can't make «class sown» of file "feathers:Users:mattneub:myFile"
into type reference
end tell
What that error tells you is that you're not even targeting the Finder. The variable f is a file object reference belonging to your script, not an object in the Finder at all.
Similarly, the Finder's alias file class is not an alias object.
List
A list is a collection corresponding roughly to what many other languages would call an array—it's an ordered set of values. These values are its items. Each item can be of any datatype (including a list). Lists are returned by scriptable applications from element specifiers such as every and boolean tests. They are useful for passing as parameters to commands and handlers because they can contain any number of items. AppleScript provides some operators for testing the contents of a list and for concatenating lists to form a new list (see Chapter 15).
A literal list is delimited by curly braces. Its contents can be literal values, variable names, or any other expressions that AppleScript can evaluate meaningfully; they are separated by commas. The literal empty list is just a pair of curly braces:
set empty to {}
set pep to {"Mannie", "Moe"}
set pep3 to "Jack"
empty & pep & {pep3} -- {"Mannie", "Moe", "Jack"}
You can assign a list of values to a literal list of variable names or other references as a shorthand for performing multiple assignments. The assignments are performed pairwise in order: item 1 to item 1, item 2 to item 2, and so on. If the list of values is too long, the extra values are ignored; if it's too short, there's a runtime error. (See "Assignment and Retrieval" in Chapter 7 and "Assignment of Multiple Attributes" in Chapter 11.) For example:
tell application "Finder"
set L to {name of folder 1, name of folder 2}
set {oldname1, oldname2} to L
set {name of folder 1, name of folder 2} to {"f1", "f2"}
end tell
When you use set (as opposed to copy) to set a variable to a value that is a list , you set the variable by reference. This means that the list is not copied; the variable's name becomes a new name for the list, in addition to any names for the list that may already exist. The same is true when a list is passed as a parameter to a handler. This special treatment is in common between lists, records, dates, and script objects, the four datatypes that can be mutated in place. (See "Set by Reference" in Chapter 7 and "Pass by Reference" in Chapter 9.) For example:
set L1 to {"Mannie", "Moe"}
set L2 to L1
set end of L1 to "Jack"
item 3 of L2 -- "Jack"
In a literal list, a variable representing a list, record, date, or script object is itself set by reference . For example:
set L1 to {"Mannie", "Moe"}
set L2 to {L1, "Pep"}
set end of L1 to "Jack"
item 3 of item 1 of L2 -- "Jack"
A list can be mutated in place in two ways: you can replace individual items, and you can add a new item to the beginning or end of a list. The reason is that a list is stored internally as a data structure called a vector . In this data structure, the items of the list are accessible with equal efficiency; if a list has 100 items, it doesn't matter whether you refer to item 1 or item 100—AppleScript can access the item instantly. By the same token, setting an existing item of a list to a new value is efficient, because all that happens is that the new value is copied to the location in memory where the old value used to be.
set L to {"Mannie", "Moe"}
set item 1 of L to "Larry"
L -- {"Larry", "Moe"}
Also, setting the beginning or end of a list (as a way of appending to the list) is efficient; nothing moves in memory except the new value, and the list is told it is one item longer than it was. So:
set L to {"Moe"}
set end of L to "Jack"
set