Figured this out and thought it might be useful to someone. The situation is that I want to add or modify custom data for a task or tasks. However, there is already custom data associated with those tasks. So, let's say a task already has a custom data field called "Custom1" set to "Yes". I want to add a new custom data field, "Custom2" set to "No".
The following
Code:
set newCustomData to {custom2:"No"}
tell application "OmniPlan"
tell front document
repeat with eachTask in every task
set custom data of eachTask to newCustomData
end repeat
end tell
end tell
would NOT be good because it would replace all of the custom data for each task with newCustomData, effectively wiping out the existing custom data, meaning the value of Custom1 would now be blank. That's not what we want. Better is this:
Code:
set newCustomData to {custom2:"No"}
tell application "OmniPlan"
tell front document
repeat with eachTask in every task
set oldData to custom data of eachTask --New stuff
set newData to newCustomData & oldData --New stuff
set custom data of eachTask to newData --New stuff
end repeat
end tell
end tell
This code concatenates a record containing the new custom data onto the front of a record containing the current custom data and then writes the combined record to the task, thus preserving all existing custom data.
If you want to modify existing custom data, you can take advantage of the fact that the value of the leftmost record is preferred when the same key exists in both records. That is, {custom2:"No"} & {custom2:"Maybe", custom1:"Yes"} yields {custom2:"No",custom2:"Yes"}. So, the code just above would also serve to update existing an existing custom data field "custom2" to "No", leaving other existing custom data in place.