Hi t.spoon.
The problem with the original script (apart from the fact that it no longer opens correctly in Script Editor!) is this line:
if x is in foo's okAddresses then
It tends to get written this way because it works with simple objects like strings and numbers. But the correct formulation when using ‘is in’ or ‘contains’ with a list of items is:
if {x} is in foo's okAddresses then
Notice the braces round ‘x’. The reason for them is that the code’s notionally looking for a section of the list, not, as we think of it, for an item in the list.
set tabGoods to {{|style|:"g185", colorCode:"51", colorName:"black"}, {|style|:"g185", colorCode:"51", colorName:"black"}}
tabGoods contains {|style|:"g185", colorCode:"51", colorName:"black"}
--> false
tabGoods contains {{|style|:"g185", colorCode:"51", colorName:"black"}}
--> true
tabGoods contains {{|style|:"g185", colorCode:"51", colorName:"black"}, {|style|:"g185", colorCode:"51", colorName:"black"}}
--> true
The same’s notionally true with text:
"Hell" is in "Hello"
--> true ” not because "Hello"'s a container containing "Hell", but because "Hell"'s a subsection of it.
I think the reason you can get away without the braces when checking for a text or number in the list is that in AppleScript, a single item is automatically coercible to a list containing that item, so we don’t have to think about it. But when the item’s already a list or a record, the coercion to list takes on a different meaning. In these cases, we have to be explicit with the braces. But using braces is actually correct in any case.
Hope this makes sense.
Edit: Yes. I was right about items being coerced to lists. Here’s a short demo:
set tabGoods to {"g185", "51", "black", "g185", "51", "black"} -- Now a list of texts.
"g185" is in tabGoods
--> true, because "g185" is automatically coerced to {"g185"} (a text to list coercion) for the check.
{|style|:"g185", colorCode:"51", colorName:"black"} is in tabGoods
--> true, because the record is coerced to {"g185", "51", "black"} (a record to list coercion) for the check.
--> This is just to demonstrate that the coercion takes place. A record to list coercion should never be relied upon to produce a list with items in a particular order.