Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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

Posted on Jul 16, 2022 8:57 AM

Reply
Question marked as Best reply

Posted on Jul 17, 2022 5:46 AM

Hey Badunit,


I have replicated what you have done (altered the script to remove the close and removed the "saving no" from the quit and enabled both system preferences) and the script now generates the delete/cancel/save popup. While not the ideal solution, it does avoid the backlog of open sessions so I'll go with t for now.


I agree that the close seems ineffective and I wonder if this is pointing us towards the underlying issue.


I'll keep digging and if I get to the bottom of it I'll update this thread.


Thanks again for taking the time to look at this.

Similar questions

5 replies
Question marked as Best reply

Jul 17, 2022 5:46 AM in response to Badunit

Hey Badunit,


I have replicated what you have done (altered the script to remove the close and removed the "saving no" from the quit and enabled both system preferences) and the script now generates the delete/cancel/save popup. While not the ideal solution, it does avoid the backlog of open sessions so I'll go with t for now.


I agree that the close seems ineffective and I wonder if this is pointing us towards the underlying issue.


I'll keep digging and if I get to the bottom of it I'll update this thread.


Thanks again for taking the time to look at this.

Jul 16, 2022 7:23 PM in response to one-last-edit-then-bed

I would try a delay between close and quit and see if that makes a difference.


I did not run into the problem with the shortened version of your script shown below so I can only guess at a possible reason and a possible solution.


tell application "Numbers"

activate

make new document

close document 1 saving no

quit saving no

end tell


There is a setting in System Preferences/General called "close windows when quitting an app". If this is not selected, open documents will be reopened when the app is restarted. I tried that but still did not have a problem with the short version of your script I was using in my test so I can't say for sure if it is related. As far as I could tell, the open document was closed before quitting (just as the script says to do) so it all worked as intended for me. I suppose if a document did not close in time, it would reappear the next time Numbers started. So, again, maybe a delay will fix it. Delays are kind of a patch but it is worth a try to see what happens and then you can think of some code to keep checking until the document is closed before quitting. Or select the "close windows" setting in System Preferences and see if that cures it.



Jul 17, 2022 12:55 AM in response to one-last-edit-then-bed

I can't replicate the problem here either (M1 MBP run Monterey 12.4) using this:


tell application "Numbers"
	set newDoc to make new document with properties {document template:template "MyTemplName"}
	export newDoc to POSIX file "/Users/sg3/Desktop/MyNumbersPDF.pdf" as PDF
	close newDoc saving no
	quit
end tell



This is probably not directly related to the behavior you are seeing, but note that the activate line is superfluous. The tell statement already targets Numbers. You would use activate when you need to place the gui focus on the app.


SG



Jul 17, 2022 2:42 AM in response to SGIII

Thanks Badunit and SGill.


Badunit, I tried the System Preferences setting you suggested and also "Ask to keep changes when closing documents" but they made no difference. I also put in a delay (user prompt where I waited about 10 seconds) between the close and the quit and this made no difference.


SGill, thanks for the "activate" info and also for the POSIX path hint.


I stripped down my script and the issue went away. Could it that Numbers records whether or not the newly created spreadsheet has been updated and if not then the new spreadsheet is simply discarded when closed. As in, if you use the GUI to create a new document from a template and then immediately close the new document, Numbers closes it without saving or prompting the user.


I rewrote the script as follows:


This code demonstrates the problem, but if I comment out the line that sets the value of cell A1 then the problem does not occur.


After I run the above code, including the line that changes cell A1, if I open Numbers by clicking on its icon, it opens the spreadsheet that was created by the script. When I try to close the spreadsheet I get the standard prompt:



It's like the script needs to effectively press the Delete button but I don't know how to do make that happen.


Once again, any help would be greatly appreciated.

Jul 17, 2022 5:00 AM in response to one-last-edit-then-bed

I tried your new code and for me it works as you intend it to. However, I was able to get it to have the problem with one other change. With "ask to keep changes" and "close windows when quitting" both unchecked in System Preferences, if I comment out the "close document" line and change "quit saving no" to "quit saving yes" or "quit saving ask" or plain old "quit", I get the problem you are having: Numbers opens with the spreadsheet. If I run the script multiple times, Numbers will open with multiple copies of it.


I also noticed an odd thing related to the line "close document 1 saving yes". It does not matter if I use yes or no, it does not save it (or if it does I do not know where). I expected with it being a new document that has never been saved that "yes" would be bring up the save dialog, same as "ask".


I wish I had an answer for you but I don't.


2015 MacBook Pro running Monterey with all software up to date.

Numbers templates used from Applescript is leaving unsaved sessions open

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.