The Omni Group
These forums are now read-only. Please visit our new forums to participate in discussion. A new account will be required to post in the new forums. For more info on the switch, see this post. Thank you!

Go Back   The Omni Group Forums > OmniPlan > OmniPlan Extras
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
Sample of AppleScript to add to or modify custom data Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
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.
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



All times are GMT -8. The time now is 11:34 AM.


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