Are records converted to string, passed as application argument, then converted back to records for use in general?
Or is it more typical to write a single-line CSV file, pass the filename as application argument, then create the record from its extracted contents.
Actually, string is not something a record can be coerced into directly — only a list. In doing so, the label is permanently lost. Lists of course, can be coerced into other data types.
Class Reference - record
If I can’t structure my return values as a comma-separated list, I tend to use NSJSONSerialization since most of my scripts are already ASObjC. Here’s an example:
use framework "AppKit"
set myRecord to {intItem:7, listItem:{{itemKey:"item2", value:"some value"}, {itemKey:"item2", value:{itemKey:"subitem2", value:"nested value"}}}}
set jsonData to current application's NSJSONSerialization's dataWithJSONObject:myRecord options:0 |error|:(missing value)
set jsonString to (current application's NSString's alloc()'s initWithData:jsonData encoding:(current application's NSUTF8StringEncoding)) as text
return jsonString
If you don’t want to use ASObjC, and you don’t want to rework the wheel, you could look into this app: JSON Helper for AppleScript, which you’d use like so:
tell application "JSON Helper"
set myRecord to {intItem:7, listItem:{{itemKey:"item2", value:"some value"}, {itemKey:"item2", value:{itemKey:"subitem2", value:"nested value"}}}}
make JSON from myRecord with pretty printing
end tell
The app is likely faster since it can run in the background rather than re-initializing for each script run, but you might not want another background item, plus it’s third-party. Another way is to use do shell script
to run some Terminal-based JSON encoder, but that’s the slowest of the options. You send the output of all of these to another script that uses the reverse operation to decode the string back into an AppleScript class.
I don’t think there’s really a ‘canonical’ way to do this, so go with whichever variant works best for you use case.
We recommend using JSON for such purposes, but stringifying AppleScript records is a real practice.
http://piyocast.com/as/archives/754
This script convert list or record or record in list into string if it does not contain application’s reserverd words.