pv, here are some answers for you:
- Extraneous parentheses in items
The first thing to understand is the meaning of the term “item” in SenseTalk. There are two types of items: items in a list and items in text. If you use the generic term “item” (instead of specifying “list item” or “text item”), then SenseTalk assumes you mean “list item” if the value is a list, and “text item” otherwise.
In your case, the value of foo.bar is a property list (which is different from an ordinary list), so “text item” is assumed. Foo.bar’s value as text is <<(foo1:“bar1”, foo2:“bar2”, foo3:“bar3”)>> and “text item” by default means items separated by commas, so the three “items” in this text are:
<<(foo1:“bar1”>>
<foo2:“bar2”>
<foo3:“bar3”)>
I hope that’s all clear.
Since what you’re working with here is a property list, you can step through its entries like this:
repeat with each key of bar of foo
put "Key=" & it && "Value=" & foo.(it)
end repeat
- Exception when using “add”
This issue is a little bit similar. The “add” command (like the “+” operator) performs arithmetic addition, so it’s expecting to see numbers (or equal-length lists of numbers to add their corresponding items). To add a set of properties to another object or property list, you need to use the “add properties” command.
In your example, the command might look like this:
add properties of ((my mybar) of (my foo)) to me
(Note: I’ve added an additional pair of parentheses that is also needed for this to be interpreted correctly. Make two copies of the above command, one without the outer parentheses, and the syntax colorization will show you that they are both valid but have very different meanings. The moral: SenseTalk can be subtle sometimes – syntax colorization is your friend!)
- testFoo2 undefined after “put … into me”
This one is interesting: You are essentially self-destructing!
When you call “new scratch” in the first line of your initial handler, a new object is created, with scratch as its helper. This object is then sent an “initialize” message.
Within your initialize handler, the value of “me” is this new object. When you say “put x into me” (whatever x is) you replace the value of “me” with x. In your case, “x” is a property list without any helpers. When the initialize handler ends, this new value of me is returned and put into foobar in your initial handler.
For those of you “following along at home”, you might try adding this as the second line of the initial handler and then try running the script with and without commenting out the “put” line in initialize:
put foobar && "Helped by:" && foobar's helpers
This is also a great place to take advantage of Eggplant’s debugging capabilities to step through the code one line at a time and examine things along the way using put statements in the Do box.
Thanks for the interesting questions, and happy scripting!