Matt Neuburg wrote:
> >A little dip into Obj-C Cocoa solves AppleScript's limitation on accessing
> >the contents of records via the property reference form only (e.g. for a
> >record {label1:"foo", label2:"bar"} you can only use 'label1 of thatRecord'
> >and not something like 'property of thatRecord whose label is "label1"' to
> >talk to it).
>
>So does using LateNight's free List & Record Tools osax:
>
><http://www.latenightsw.com/freeware/RecordTools/index.html>
Both invaluable in certain situations, i.e. when a not-very-clever third-party osax, application or 'call method' hands your script a record containing arbitrary keys and you have to get at that data in AppleScript. There are some limitations to be aware of though:
- Because you're passing data via Apple events, the values returned are copies, not originals. This doesn't matter for immutable values like numbers and strings, but is significant when dealing with mutable types such as lists, records and script objects. Example:
set rec to {a:1}
set user property "a" in rec to 4
rec --> {a:1} -- note: original object remains unmodified; you have to throw it away and replace with the one returned by the command
Script objects in particular are a real PITA to handle this way, as it's not just that object but its entire context that gets cloned - which is both expensive and wreaks total havoc with OO designs.
- While AppleScript can pack basic datatypes and references to the host application's object model into AEs, it throw a coercion error if you ask it to pack references to other applications' object models. Example:
set rec to {a:document 1 of application "TextEdit"}
get user property "a" in rec
--> Error: Can't make document 1 of application "TextEdit" into the expected type.
FWIW, AppleMods' Record library <http://applemods.sourceforge.net/mods/Data/Record.php> provides a third, completely vanilla, option which avoids the above problems. The listToRec, addProp, getVal and setVal commands are reasonably sound; they rely on code generation via 'run script' so aren't particularly fast and I think the 'run script' command leaks memory, but they won't muck with your data. The recToList() command is very hackish though, so best avoided if you can help it.
Anyway, the thing to remember is that AppleScript's record type is comparable to C structs, and NOT equivalent to NSDictionaries, Perl hashes, Python dictionaries, etc. (Unfortunately, this is a not uncommon misconception - even appearing in at least one published AppleScript book!) AS records and C structs are used to define complex rigid data structures at compile-time [1], whereas NSDictionaries, etc. are dynamic key-value data stores where key-value pairs can be added, retrieved and removed at run-time using arbitrary keys defined at run-time. The omission of a native dictionary type from AS is unfortunate, but that's AS for you.
Also, while it's tempting to use records as ersatz dynamic key-value data stores, it's really unnecessary and there are better alternatives anyway:
- Satimage's XMLLib, while not really intended as a dictionary replacement, does make a reasonably efficient alternative (the osax retains the data itself, which is much more efficient than passing big data structures from AS to osax each time). Plus you get serialization support for free. On the downside, being an osax, it also has the same issues as the 'call method' and 'List & Record Tools' osax.
- AppleMods' Types library provides robust and reasonably efficient vanilla AssociativeList and Dict objects. (You could also roll your own, of course, but why bother when it's already been done for you?:) The only significant issue is with the Dict objects containing more than a few thousand items start to bog down when adding new items [2], but for most purposes it's more than adequate (and if it isn't, you should probably be using a Real Language anyway:p).
HTH
has
[1] Though AS does at least allow you to concatenate two or more existing records to create a new one at run-time.
[2] As usual this is largely AppleScript's fault, though there are ways around it if it's a problem.
--
http://freespace.virgin.net/hamish.sanderson/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-studio mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/applescript-studio/email@hidden
This email sent to email@hidden