AppleScript to manage defer dates and due dates

I’m trying to write an AppleScript to manage defer dates and due dates programmatically. Importantly I want to treat the parent (is that the write word?) of an activity group differently from simple leaves.

The following script extracts properties from each row in a selected block of rows. My intention was to determine whether the item in a particular row was the parent of an Activity Group by using the count of its leaves, so that I could then then set dates for it accordingly. Unfortunately the critical part (commented out here) generates a “Can’t get defer date …” error.

tell application "OmniFocus" to tell content of front window

set theTasks to every selected tree
set ntasks to count of theTasks
set kount to 0
repeat with aTask in theTasks
	set kount to kount + 1
	tell aTask
		
		log ">>>>> item: " & kount
		set theName to its name
		log "theName: " & theName
		set theChildren to its leaves
		set nChildren to the count of theChildren
		log "theChildren: " & nChildren
		
		(*	
		if nChildren > 0 then -- this is an Action Group
			if defer date is not missing value then --
				-- delete defer date
				set defer date to missing value
			end if -- delete defer date
		end if
		*)
		
	end tell --atask	
end repeat

end tell

In the next attempt (see script below), the first set statement is changed to get the value of every selected tree and this does give access to the defer dates - but the commented out part fails to get the number of children, giving the error: “Can’t get every leaf of task id …”.

tell application "OmniFocus" to tell content of front window

set theTasks to value of every selected tree
set ntasks to count of theTasks
set kount to 0
repeat with aTask in theTasks
	set kount to kount + 1
	tell aTask
		
		log ">>>>> item: " & kount
		set theName to its name
		log "theName: " & theName
		
		(*
		set theChildren to its leaves
		set nChildren to the count of theChildren
		log "theChildren: " & nChildren
		*)
		
		if its defer date is not missing value then
			set theStart to (its defer date as date)
			log "theStart: " & theStart
		end if
		
	end tell --tree	
end repeat

end tell

And so, in the hope of improving my meagre understanding, I have two questions for the forum:

  1. Why does specifying “value” change which properties are accessible?
  2. Is there a better strategy for my problem?

I apologise if the scripts demonstrate the problem are unnecessarily long. Please advise if there is a better way to include a script - I couldn’t see any pointers in the Forum.

Thank you.

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