This does not work with windows on different desktops.
Neither Keyboard Maestro nor AppleScript UI-Scripting can see windows in different desktops.
So this for instance will not work:
tell application "System Events"
tell application process "TextEdit"
set winNameList to name of every window
end tell
end tell
An AppleScriptable app like TextEdit will let you get every window no matter what desktop it's in.
tell application "TextEdit"
set winNameList to name of every window
end tell
But not all apps are AppleScriptable...
This is dismally bad UI by Apple – especially considering how long desktops have been available on macOS 10+.
The guy with the most success managing desktops with Keyboard Maestro is @_jims. See this thread:
MACROS: Desktop Spaces • Macros for Navigation and Window Management, v1.1 (Superseded)
I'd just ask Jim directly if his system can do what you want.
If not I think you're stuck with a utility like Witch or something like it.
-Chris