option to close InDesign frame window

Hi guys,
Is there a way to check if, in Adobe InDesign is more or less than one document open?
I want to close the frame of the application only if I close the last open document.
Is something like that possible?

Thanks

Is there at least a way to click on a specific section on the screen?
I used to use the click command in Applescript, but it seems no longer working.

I just found a way to solve my problem. Kind of.
I created this script:

tell application "Adobe InDesign 2022"
	activate
	if (count documents) = 0 then
		tell application "System Events" to keystroke "h" using command down
	end if
end tell

But when I put it in BTT, it didn't perform how I wanted it. Actually, I'm no longer able to use the command+w to close the document window.
Do I do something wrong?
Here are my settings:

:slightly_frowning_face: no one?

your script hides (but does not close) the window if 0 documents are open, but doesn’t do anything otherwise. I think that’s not what you wanted or did I misunderstand?

In the first place, I wanted to close the Frame. But you only can close the Frame by actually clicking the red button. Because of that, I was looking for another way to solve this.
Now I created this script to check if I close the last document, then the Frame should hide.
And I tested it in BTT, and it doesn't let me use command+w, even when I don't have any script in there. Like that:

configuring it like this you tell BTT "if cmd +w is pressed while indesign is active: do nothing" so it's normal that it won't do anything when you press cmd+w now.

You would need to add code that also closes the window to your script otherwise it will only do something if your condition is true ( if (count documents) = 0 )

Ah, really?
I did that because I already did that here, it worked. So I thought it would also woking with InDesign.

Yes this works - there you tell BTT to trigger cmd+q when pressing cmd+w. But in your Indesign configuration you tell BTT it to execute your script when pressing cmd+w (and nothing else).

Ah, I see.
Unfortunately I'm not very familiar with Apple Script. I know just some commands and try to search the web, and mixing codes and hope for the best. :sweat_smile:
I changed my script to that, but I'm not sure if it would work.
How would you change it?
And by the way, is there a way to click on a specific area with Apple Script?
I tried the click command, but it didn't work. It seems there are exceptions for this command.
In safari, for example, works fine.

tell application "Adobe InDesign 2022"
	activate
	if (count documents) = 0 then
		tell application "System Events" to keystroke "h" using command down
	else
		tell application "Script Editor" to quit
	end if
end tell

I think your new script should work (but replace

tell application "Script Editor" to quit
with
tell application Adobe InDesign 2022 to quit

Ok, my script didn't work.
And when I replace the two lines, it quit InDesign, even when documents are still open. What I don't want.
Maybe I don't need to quit the script but only stop it?

:partying_face: :grinning_face_with_smiling_eyes: I did it. I finally found a solution.
I changed the script to that, and now it works how it should.
Thanks for pushing me in the right direction.
That is how I changed it:

tell application "Adobe InDesign 2022"
	activate
	if (count documents) ≤ 1 then
		tell application "System Events" to keystroke "w" using command down
		tell application "System Events" to keystroke "h" using command down
	else
		if (count documents) ≥ 2 then
			tell application "System Events" to keystroke "w" using command down
		end if
	end if
end tell

Thanks again