Adobe Indesign CC 2019 documents vs windows vs tabs?

Hi,

I have a script that can move data from one unformatted table in one document to a formatted table in a second document. It works ok, but if I have a third document, then the script fails. I’m dealing with it with a clause that checks if there are 2 documents or not at the beginning of the script but I would like to be able to have a third document open for reference sometime so I want to modify the script. Don’t laugh, it’s an old script from Shane which I have adapted a bit.

--Table Data replace
--Use this script to replace data in a table already designed in ID CS4.
--You need two document opened, one with a table formatted and the other document with the table with the same number of columns and rows without formating. Select the part you want to replace in the first and the part you want to replace with in the other document and you're good to go.
--By Shane Stanley, from the ID Magazine issue 2.
--fixed number of rows and columns check that didn't work on 2013-03-25 by Jeff Lambert

tell application "Adobe InDesign CC 2019"
	if number of document is not 2 then
		beep
		activate
		display dialog "This script requires two documents open, with equivalent table areas selected." buttons {"OK"}
		return
	end if
	set columnCount1 to count of columns of selection of document 1
	set rowCount1 to count of rows of selection of document 1
	set columnCount2 to count of columns of selection of document 2
	set rowCount2 to count of rows of selection of document 2
	if (rowCount1 is not equal to rowCount2) then
		beep
		activate
		display dialog "The number of rows don't match." & return & " You have " & rowCount1 & " rows for the document in front vs " & return & rowCount2 & " rows for the document in back." buttons {"OK"}
		return
	else if (columnCount1 is not equal to columnCount2) then
		beep
		activate
		display dialog "The number of columns don't match." & return & " You have " & columnCount1 & " columns for the document in front vs " & return & columnCount2 & " columns for the document in back." buttons {"OK"}
		return
	end if
	display dialog "Which document contains the formatted table?" buttons {"Cancel", "Front", "Back"} default button 2
	if button returned of result = "Back" then
		set dataDocument to document 1
		set formatDocument to document 2
	else
		set dataDocument to document 2
		set formatDocument to document 1
	end if
	set ourData to contents of selection of dataDocument
	set contents of selection of formatDocument to ourData
end tell

Here’s the way I would like to work on this. Having 3 documents open, Document1 in window 1, Document 2 and 3 in a second window. Document 1 would be the active document, and document 3 would be in a hidden tab in window 2.

I don’t want to have a dialog that asks which document is the front and the back from a list, it would be too long since I have hundreds of tables to do this way. Having a dialog with two buttons is enough and it defaults to the front-most which is how I usually work, I just hit enter and I’m good to go.
So, how can I deal with the top document in each window? I could have 4 documents or more if possible but still be able to run the script and the only document the script has to work with would be the top ones from each window, not the other tabs which are hidden. If there are more then two windows, it could just throw an error, that would be fine.
Thanks for any help!