How to tell if an app has an open window in the current desktop

If I have an app with an open window in the current desktop, it doesn't have to be frontmost, I can access that window in AppleScript with

tell application "<ChosenApp>"
	set doc to front document
	get name of doc
end tell

But if there is no open window of that app in the current desktop, then the above script will return the name of another document in the same app. (It seems to give the first one that was opened in the current session, but I haven't tested that.)

That's the distinction that I'm trying to tease apart. Sometimes that first-opened doc is the on on the current Desktop, so that's the right name to return. But on another Desktop, I only want a name returned if it's the name of a doc that's actually on that Desktop.

Any ideas?

I found this thread:

It looks like it takes advantage of the fact that System Events can only see windows in the current Space and creates a function that counts them.

I'll be trying it.

I had no luck with this. It seems like count of windows will now return the number of all open windows regardless of the Space they belong to..

Have you found other working solutions to this?

The following will return the count of unminimized Finder windows in the Mission Control Desktop Space:

tell application "System Events"
	-- Within "System Events" restricts count to the active Desktop Space
	tell process "Finder"
		return count of (windows whose value of attribute "AXMinimized" is false)
	end tell
end tell

Or all Finder windows in the space:

tell application "System Events"
	-- Within "System Events" restricts count to the active Desktop Space
	tell process "Finder"
		return count of windows
	end tell
end tell

Note that if the Finder is hidden, the above are not affected.