Hacky way to get # windows for a program that doesn't have a dictionary?

I’m trying to close all open Notion windows. Notion doesn’t have a dictionary, so I’m 1) sending a cmd-W, 2) activating Finder, 3) activating Notion again to get the next window under focus, 4) repeat.

It’s obviously very hacky. Also I can’t determine when all the windows have been closed, so I just loop through 5 times.

Chrome/Brave doesn’t have great dictionary support, but Applescripts can pick up the number of windows:

tell application "Brave Browser"
	log (count of windows)
end tell

The same does not work in Notion:

tell application "Notion"
	log (count of windows)
end tell

-- output: Notion got an error: every window doesn’t understand the “count” message

Is there a better way to do this?

See if this works…

tell application "System Events"
   tell its process "Notion"
      count of windows
      
   end tell
end tell

You may need to set it’s frontmost to true

tell application "System Events"
   tell its process "Notion"
      set its frontmost to true
   end tell
end tell

(Note, I don’t have Notion installed but this works with other unscriptable apps).

If I recall you can also use System Events to close windows, as long as they don’t need saving.