Pages temp doc will not delete

Thanks for looking at this for me. I failed to fix this last year and now it’s come back to haunt me
My script is meant to generate a number of PDF docs by creating from a pages template, substituting images and text into placeholders, exporting the doc to PDF, saving a local pages copy, repeated x times

Using an applescript
I create a new pages doc from a template.
a document - “untitled.pages” appears.
the script then adds images to placeholders in the doc.
export a pdf copy of the now modified pages document with it’s own filename - works fine every time
save the original “untitled.pages” document with a new file name - works fine every time

I then tell pages “close every document without saving”

when i close pages i get the dialog box asking me if i want to save “untitled.pages” and the script hangs waiting for a decision.
or if I quit pages, when I open it again, “untitled.pages” appears with a dialogue box as above.

I need to know how to close/delete “untitled.pages” OR how to save and then close it.

here is the relevant snippet

tell application "Pages"
			tell thisDocument
				set thisPlaceHolderImage to image 1 -- gets placeholder location in pages template 
				set file name of thisPlaceHolderImage to (path to desktop folder as text) & pdfFileName as alias
				export thisDocument to file bpassDoc as PDF -- this makes and saves the output file
				save thisDocument in file bpassTemp as pages
				log "Temporary pages doc closed."
			end tell
			tell application "Pages"
				close every document without saving
				log " Pages documents closed."
			end tell
		end tell 

Thanks for reading this - I don’t mind being shown that I am dim…

Max

Model: iMac mid 2011
Browser: Safari 605.1.15
Operating System: macOS 10.14

Here, under 10.13.6, I am unable to reproduce your problem.
I just added two parentheses to the instruction:

set file name of thisPlaceHolderImage to ((path to desktop folder as text) & pdfFileName) as alias

The newly created document is not named “untitled.pages” but “untitled
The extension is only added to saved documents.

I tested with:

set bpassDoc to (path to desktop as text) & "export.pdf"
set bpassTemp to (path to desktop as text) & "temporary.pages"
set pdfFileName to "2019-04-12 16-43.pdf" # name of a file available on the desktop

tell application "Pages"
	set untitled_Loc to localized string "Untitled" from table "Pages"
	--> "Sans titre"
	set thisDocument to document untitled_Loc
	tell thisDocument
		
		set thisPlaceHolderImage to image 1 -- gets placeholder location in pages template 
		set file name of thisPlaceHolderImage to ((path to desktop folder as text) & pdfFileName) as alias
		
		export thisDocument to file bpassDoc as PDF -- this makes and saves the output file
		save thisDocument in file bpassTemp as pages
		log "Temporary pages doc closed."
		close it without saving
	end tell
	
	log (get count of documents)
	--> 0
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 22 avril 2019 17:11:29

It works for me too, using Pages 8.0 in Mojave. Is the problem due to something else happening in the script?

I wouldn’t double up the application and document references:

set bpassDoc to (path to desktop as text) & "export.pdf"
set bpassTemp to (path to desktop as text) & "temporary.pages"
set pdfFileName to "143_SM30-3-19.jpg"

tell application "Pages"
	set thisDocument to (make new document with properties {document template:template "Modern Report"})
	
	tell thisDocument
		set thisPlaceHolderImage to image 1
		set file name of thisPlaceHolderImage to ((path to desktop as text) & pdfFileName) as alias
		export to file bpassDoc as PDF -- thisDocument is the default target in this tell statement.
		save in file bpassTemp as pages -- Ditto.
		-- close without saving -- Ditto.
	end tell
	
	close every document without saving -- Already telling application "Pages"
end tell

Nigel, Yvan,

Thanks for answering. Both your proposals work well and carry out the main function, producing the PDF and Pages copies. The export(.PDF) and filed copy(pages) has always worked perfectly.
The focus here is the “untitled” pages window that is still open after the above two documents have been created.

The script will generate up to 120 individual 1 page .PDF files. So, when the repeat loop makes another copy of the template that becomes “Untitled 2” which will not close.
As the script progresses you will see that the pages app has dozens of unsaved “Untitled” documents in memory and cascading down the screen. This will happen even if i quit pages and when the script activates the app again, the untitled 's are still there.

Close without saving, close every document, set focus to the window and close it, quit without saving all do the same thing. I get a pages dialogue box asking Delete, Cancel, Save.

I’ll review what I have been doing here and try your constructions. They are usually better than mine anyway! I think i will build another sub script doing just this job to try to get to the bottom of this. I will report what happens

Thanks for taking the time to help.

Max

It seems that I wasn’t clear enough.
Here, the document is correctly closed.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 22 avril 2019 20:03:37

Dear Yvan,

I am still getting inconsistencies but may have found the problem.

I have had a number of odd events, sometimes when a new document is catered from the template , the ‘new’ document is “Untitled x” where x is anything from 1 to 12. This number can vary up or down. It was not just “Untitled”.

By right clicking on the pages icon on the toolbar I found a list of “Untitled x” documents that I thought had been deleted. These were all unsaved and opened when selected. I deleted all these manually and changed my script to follow your advice and Nigels.

When running the script i found that this list was growing again, ALTHOUGH I could not find the documents with a search. Some would not open when selected on the toolbar icon. Then I noticed that if i left a few minutes between runs, the x number would move again…

You have probably guessed…I pointed all the files to a non-icloud folder and am now unable to reproduce the problem.

The “Untitled x” documents are no longer being cached. The ‘new’ document created in each loop is always “Untitled” with no number and the list on the toolbar icon has no entries.

I will take the time to reproduce the problem again and then run the fix. This is in order to prove what was wrong. I will report again after that in case somebody else needs to know.

Thank you for the help you (and others) offer. This was not my first time here and you have helped a lot in the past.

Max

Yvan, Nigel,

I said i would respond, but forgot. The fixes both work, but only most of the time.

Every now and again there are new untitled docs being held by the Bags app.

In the end, I just wrote a cleanup script which would close pages and my script, then it would open pages and using mouse clicks delete each unsaved untitled file one after the other.

I was never able to pin down what was happening nor why it was intermittent.

Thanks as ever

Max