why it do that?

So I had essentially the follow command plastered throughout my (very long - and increasingly hard to follow) script.

set theList to {"Apple", "Pear"}
set theList to item 2 of theList
set theList to theList & "Orange"
set theList to item 2 of theList
return theList

ect. ect.

of course I diligently pour’d through EVERY OTHER section of my code before (finally) coming to the conclusion that the problem must be in the above (specifically when I would add items to the list).

This fix’d it

set theList to {"Apple", "Pear"}
-- added "as list" to the end
set theList to item 2 of theList as list
-- and instead set new list item to "the end of"
set the end of theList to "Orange"
set theList to item 2 of theList
return theList

It seems stupid now, but the annoying thing about all of this was that the script ran w/o error before I discovered the problem - except for the fact that instead of returning “orange” in my example it would give me “e” (the result was much uglier in my actual script, ouch).

set theList to {"PearOrange"}
set theList to item 2 of theList
return theList

Since my orginal command was doing nothing more then turing my list into a string when I was attempting instead to add an item to the list, I would have thought that asking for item 2 of that “list” would have caused the script editor to clue me in.

or was it just trying to teach me a lesson here?

It seems that item can be substituted for character. (Personally, I don’t like that behavior, but I suppose it’s acceptable for a weakly-typed language.)

I consider this a serious bug, weakly typed language or not, because it’s inconsistent:

set I to item 2 of "ABCDE" --> B

set k to every item of "ABCDE" --> fails on my G5

set I to items of {"ABCDE"} --> "ABCDE"

set J to item 2 of {"Moi"} --> fails on my G5

set theList to {"PearOrange"}
set theList to item 2 of theList -- fails on my G5

I don’t think so.
the significance of Item is always an item of a list.

AppleScript may coerce silently

set k to every item of "ABCDE"


set k to every item of characters of "ABCDE"

but I wouldn’t imply that :wink:

the result of item x of is always the class of the list item, not a list.
In this case it’s the string “Pear”

It works on my G5, but mine’s running Tiger. Presumably, this particular behaviours’s changed in Leopard.

I’ve always understood ‘item’ to have an alternative meaning as “generic element” ” or “that which is counted when you can sensibly use ‘count’ or ‘length of’ without specifying the class of element to count.”

count {1, "cow", true, {a:March}, false} --> 5 whatever elements ('items').
count {1, "cow", true, {a:March}, false} each item --> Specifically 5 'items' (takes longer).
count {1, "cow", true, {a:March}, false} each boolean --> 2 'booleans'.

items of "ABCDE" --> {"A", "B", "C", "D", "E"} (Characters are the basic elements of texts.)
count "abcde" --> 5
count "abcde" each paragraph --> 1

items of {a:1, b:"Aardvark", c:3} --> {1, "Aardvark", 3} (A record can have items!)
-- However, not 'item 3 of {a:1, b:"Aardvark", c:3}', as records aren't ordered.
count {a:1, b:"Aardvark", c:3} --> 3
count {a:1, b:"Aardvark", c:3} each integer --> 2

tell application "Finder"
	items of myFolder
	count myFolder
end tell

I agree, this is a better definition, however it’s always an element of a list

a record is actually a list, it cannot accessed by index, because it’s ordered by labels

this is also a list, it’s been treated as items of the contents of myFolder

That’s an implied coercion. (Support for coercing a record to a list is documented).

Apparently there’s no implied coercion there.

item 2 of ({a:1, b:"Aardvark", c:3} as list)
--> "Aardvark"

I think there may be some language confusion here. In AppleScript, a list is a list and a record is a record ” unless of course they’re empty, in which case their class is returned as list but they’re actually something in between:

class of {}
--> list

{1} & {b:"Aardvark", c:3}
--> {1, "Aardvark", 3} (Record coerced to list for concatenation to list.)

-- But:
{} & {b:"Aardvark", c:3}
--> {b:"Aardvark", c:3} (Record not coerced to list. {} treated as empty record.)

‘Contents of myFolder’ is the same as ‘myFolder’, since the latter’s a Finder reference and the ‘contents’ operator is for dereferencing AppleScript references. The Finder itself doesn’t offer a ‘contents’ property, though I seem to remember the term could be used to return folder items in OS 8 and possibly OS 9. The Finder’s folders do have ‘item’ elements. These are returned as an AppleScript list, but aren’t necessarily stored that way within the Finder itself.

It implies a coercion to me too; but whether or not it’s actually an implicit coercion, I’ve no idea. :wink:

The point of my previous post was just the observed correlation between situations where the term ‘item’ could be used and situations where ‘count’ or ‘length of’ could be used without an element specifier to return the number of things to which ‘item’ referred. I would however dispute Stefan’s assertion that “the significance of Item is always an item of a list.” (But I’ve no intention of prolonging the argument beyond this post!) An ‘item’ is an element of any class that’s covered by the term ‘item’ in the relevant dictionary ” if you see what I mean. The container may be a list, a record, text, a Finder ‘container’, or an object in any other application where the term ‘item’ is implemented as a usable term. (Some application dictionaries show an ‘item’ as a source of inherited properties, but not as a usable object in its own right.)

I’m assuming it is (just like I’m assuming AppleScript is weakly-typed). :stuck_out_tongue:


Where did each come from? I know that every should work, but I don’t recall seeing each used for this before.

-- `count booleans of .` would also work
count every boolean of {1, "cow", true, {a:March}, false}

Believe it or not, it’s 'count’s element parameter label, as shown in application dictionaries and in AppleScript’s dictionary (when displayed in Smile). :slight_smile: It’s mentioned in the 1999 ASLG, but not in the new one. You don’t see it used very often nowadays, but I like to flaunt it around occasionally. :wink:

This is the first time I’ve actually wanted to see the old one. :rolleyes: