Random Error -1728 when running script

I randomly get an error message when trying to run my script:
error “OmniFocus got an error: Can’t get content of window 1.” number -1728 from content of window 1

It doesn’t happen every time, just seems to randomly happen where it will get stuck at line 11: " set oTrees to selected trees of content"

any idea of what is going on here?

    tell application "OmniFocus"
	tell the default document to tell the front document window
		set perspective name to "Today"
		tell first document of application "OmniFocus"
			tell content of first document window
				set selected trees to every leaf
			end tell
		end tell
	end tell
	tell front window of application "OmniFocus"
		set oTrees to selected trees of content
		set lngTrees to count of oTrees
		if lngTrees < 1 then
			set oTrees to selected trees of sidebar
		end if
		
		if lngTrees < 1 then
			return
		end if
		
		if (lngTrees > 0) then
			repeat with iTree from 1 to lngTrees
				set SelectedItemInMainView to selected trees of content
				set theSelection to value of (item iTree of oTrees)
				try
					if context of theSelection is not missing value then
						set theList to the name of the context of theSelection
					else
						set theList to "Inbox"
					end if
				on error
					display dialog
					"There was an error getting the context for the task" & name of theSelection
				end try
				if flagged of theSelection is true then
					set theFlag to "3"
				else
					set theFlag to "0"
				end if
				if due date of theSelection is not missing value then
					my createReminder(theList, name of theSelection, "omnifocus:///task/" & id of theSelection as text, due date of theSelection, theFlag, theSelection)
				else
					my createReminder(theList, name of theSelection, "omnifocus:///task/" & id of theSelection as text, missing value, theFlag, theSelection)
				end if
			end repeat
		end if
	end tell
end tell

on createReminder(theList, theTask, theNote, theDate, theFlag, mySelection)
	if theDate is not missing value then
		tell application "Reminders"
			set reminderCompare to (every reminder whose name is theTask and body is theNote and due date is theDate and priority is theFlag)
			set reminderCount to the count of reminderCompare
			if reminderCount is equal to 0 then
				tell application "Reminders" to tell list theList of default account to make new reminder with properties {name:theTask, body:theNote, due date:theDate, priority:theFlag}
			else if first item of reminderCompare is completed then
				tell application "OmniFocus" to set completed of mySelection to true
			end if
		end tell
	else
		tell application "Reminders"
			set reminderCompare to (every reminder whose name is theTask and body is theNote and priority is theFlag)
			set reminderCount to the count of reminderCompare
			if reminderCount is equal to 0 then
				tell application "Reminders" to tell list theList of default account to make new reminder with properties {name:theTask, body:theNote, priority:theFlag}
			else if first item of reminderCompare is completed then
				tell application "OmniFocus" to set completed of mySelection to true
			end if
		end tell
	end if
end createReminder