Prolonging an already long thread, but I wanted to share some lessons learned and an improved method of getting a record’s labels.
Just a brief comment about limitations of three methods recently described:
(1) my method of converting a record to its text representation, then delimiting the text with the text representation of the record’s property values:
- this fails when a property value that is a reference is resolved into its underlying value in the process of converting to text
(2) kel1’s method of saving a record to the clipboard, then parsing the clipboard’s “list” property:
- returns barred labels without the bars
(3) Shane Stanley’s method using NSDictionary
- requires access to ASObjC in one form or another; fails to return the label of record properties whose value = missing value (presumably because of the way Cocoa handles missing value in an NSDictionary); and like kel1’s method, returns barred labels without the bars
The following method utilizes kel1’s method along with a “run script” construct to determine if a label is barred or not. It returns the record’s labels and values as lists:
set theRecord to {a:1, |b b|:2, c:3, |âœŽâœ“ï ƒï „ï£¿|:4}
set the clipboard to theRecord
tell (the clipboard)'s list
set {recordLabels, recordValues} to {{}, {}}
repeat with i from 1 to (length - 1) by 2
set {labelWithoutBars, labelWithBars, currValue} to {item i, "|" & item i & "|", item (i + 1)}
try
tell me to run script "{" & labelWithoutBars & ":null}"
set {end of recordLabels, end of recordValues} to {labelWithoutBars, currValue}
on error
try
tell me to run script "{" & labelWithBars & ":null}"
set {end of recordLabels, end of recordValues} to {labelWithBars, currValue}
on error m number n
error "Could not get the record's labels: (" & n & ") " & m
end try
end try
end repeat
end tell
{recordLabels, recordValues} --> {{"a", "|b b|", "c", "|âœŽâœ“ï ƒï „ï£¿|"}, {1, 2, 3, 4}}
One limitation of this method is that a record cannot be saved to the clipboard when one or more of its items’ values is an application reference, such as a Finder file reference or a System Events property list, etc. A way around this limitation is to wrap such a reference in a script object property and set the value of the record item to the script object, which then allows the record to be saved to the clipboard.