Why does this not work in Forecast Perspective?

This script works in all Perspectives except Forecast. It crashes at the noted point. Why? What should I change?

<pre
property pNameList : {“Forecast”, “Active”, “Waiting For”, “Bills”}
repeat with PItem in pNameList
set TreeList to {}
set ActionList to {}
tell application “OmniFocus”
tell front document window of default document to set its perspective name to PItem
tell content of front document window of default document
set TreeList to (value of every leaf)
end tell
repeat with ListItem in TreeList
– --> crashes at the next line
set end of ActionList to {tc:name of context of ListItem, tp:name of containing project of ListItem, tn:name of ListItem}
end repeat
end tell
end repeat

(sigh)

This should not be that difficult, but somehow it is.

All I want is a list of tasks from the Forecast view so that I can populate a GeekTool display. I can get the list I need from every other Perspective. Used to be, in OF1, I could create a “Due” perspective and handle what I needed. It seems Forecast closes off nearly any options.

In the meantime, I have “kludged” this in place of using “Forecast” mode. It misses action groups completely, something I would also like to fix.

tell application “OmniFocus”

-- get list

tell front document window of default document to set its perspective name to "Contexts"

tell content of front document window of default document
	set TreeList to (value of every leaf)
end tell

-- transfer the action items into readable form

repeat with ListItem in TreeList
	
	if due date of ListItem is not missing value then
		set theOFDue to due date of ListItem
		tell application "Finder" to set theDueDays to (theOFDue - theDateToday) / (60 * 60 * 24)
		if (theDueDays ≤ pDueSoonDays) then
			set theDueDays to theDueDays as integer
			if (theDueDays < 0) then
				set theDueDays to pOverDueFlag & " " & theDueDays
				set the beginning of ActionList to {td:(theDueDays) & " ", tc:name of context of ListItem, tp:name of containing project of ListItem, tn:name of ListItem}
			else
				set end of ActionList to {td:(theDueDays) & " ", tc:name of context of ListItem, tp:name of containing project of ListItem, tn:name of ListItem}
			end if
		end if
	end if
	
end repeat

end tell

Help???

I found my answer. I had to check ListItem for a “missing value” in the context. Otherwise, the trying to get the name of context of ListItem crashes.

It would be much better to have the identifier “missing value” return as a real return value rather than crashing a get or set statement.