Numbers templates used from Applescript is leaving unsaved sessions open
I'm not 100% sure if this is an Applescript thing or an iWork thing but I suspect it has to do with the way iWork apps save state when shutdown with unsaved documents open, so I put the question here.
I'm using a 2017 MacBook Pro running Monterey 12.4.
I'm accessing Numbers 12.1 from ScriptEditor 2.11 running AppleScript 2.8.
I have an AppleScript that opens the Numbers app, creates a new document from a simple numbers template, it populates some of the new spreadsheet, exports the spreadsheet to PDF, closes the document without saving (I only need the PDF and want to discard the numbers document) and then it quits numbers.
Everything seems to work just fine but, if I then manually start Numbers I see that the untitled.numbers document that was created by the script, is still active and, to get rid of it I need to close it and select delete from the "do you want to save..." popup. If I run the script a number of times then I end up with untitled.numbers, untitled 2.numbers etc. One for each time I run the script.
How can I stop these extraneous untitled.numbers files from hanging around?
Here is what I think is the relevant code in it's original form:
tell application "Numbers"
activate
...
make new document with properties {document template:template templateName}
...
export document 1 to file pdfName as PDF -- This works fine.
close document 1 saving no -- close the new spreadsheet without saving
...
quit saving no
end tell
I tried a couple of things around the call to "close" to try and get rid of the untitled.numbers files as follows:
export document 1 to file pdfName as PDF
save document 1 in file tempFile -- attempt to get rid of untitled.numbers by saving to a temporary file. The file is created but it doesn't get rid of the untitled.numbers file.
close document 1 saving no -- close the new spreadsheet without saving
delete document 1 -- attempt to get rid of untitled.numbers but probably only deleting the document object handle. It doesn't help.
It's my first ever AppleScript so there is hopefully an obvious, rookie error.
I'd be extremely grateful for any help.
MacBook Pro 15″, macOS 12.4