Sample of AppleScript to add to or modify custom data
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[/CODE] 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[/CODE] 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. |
Thanks for sharing …
I tried this, but it only works for some (out of the 50) tasks that are created in one run?! Also, the data does not show up in the inspector.
Do you confirm that, or is it just here? Thanks! Stefan |
Hmmm. It has always worked for me, although it is very slow to show up in the main display. There seems to be a lag in how often that updates (I've had luck toggling display of the changed custom data field on and off). It has, however, always shown up in the inspector more or less immediately.
I'll see if I can duplicate non-performance. Any leads on when it does and does not work? |
All times are GMT -8. The time now is 01:01 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.