Combining two records when one is incomplete

I have a situation that looks like this:

A: {var1:"", var2:"", var3:"", var4:"", var5:""}
B: {var2:"foo", var5:"bar"}

Record A has no values but contains all possible properties for a dataset. Record B has values but is incomplete (and will have different properties on each occasion).

I simply need to add the values of B to A. However, because it is not possible to loop through a record like one would a list, the only I way see of doing it is like this:

  set var1 of a to var1 of b
end try
  set var2 of a to var2 of b
end try

… and so on for every property of A.

That is fine for a short record. However, my actual use case involves a record with 60+ possible properties, which makes this brute force approach inelegant to say the least.

If I could get a list of every property of B then I could loop through by using run script to reference each property in turn. However, I’m not sure if that’s possible with vanilla AppleScript. If I could get the source of B as plain text then I could simply get the properties by regex, but I’m not sure how that can be done either.

Any advice as ever gratefully received!

Philip. I think you can just add them together:

set recordA to {var1:"", var2:"", var3:"", var4:"", var5:""}
set recordB to {var2:"foo", var5:"bar"}
set newRecord to recordB & recordA --> {var2:"foo", var5:"bar", var1:"", var3:"", var4:""}

The AppleScript Language Guide documents this:

The concatenation of two records joins the properties of the left-hand record to the properties of the right-hand record. If both records contain properties with the same name, the value of the property from the left-hand record appears in the result.

FWIW, if the above approach is slow, the use of ASObjC might be considered.

1 Like

Ah ha, I didn’t know that, thanks! There’s always more to learn.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.