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.
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.