Test if application has windows on current space Scripting different desktops in recent macOS versions is difficult, but one scenario I wanted to accomplish is to open a document in Safari in a specific window:


If a Safari window is open in the current space, open a new tab in that window.
Otherwise, make a new window in the current space and open it there.


How can this be accomplished?
For some strange reason, in System Events, the windows of a process only include windows on the active space. Using this tidbit, I was able to cobble together a solution to this.

on count_windows_on_current_space(process_name)
    tell application "System Events"
        tell process process_name
            return count of windows
        end tell
    end tell
end count_windows_on_current_space

tell application "Safari"
    if my count_windows_on_current_space("Safari") = 0 then
        make new document
    end if
    open location "https://cgamesplay.com/"
end tell