The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   OmniPlan Extras (http://forums.omnigroup.com/forumdisplay.php?f=45)
-   -   Sample of AppleScript to add to or modify custom data (http://forums.omnigroup.com/showthread.php?t=31026)

ghoetker 2013-11-09 11:35 AM

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.

sthainzl 2013-11-10 11:52 AM

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

ghoetker 2013-11-10 06:29 PM

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 10:47 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.