You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

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

applescript quit save yes & unsaved documents

If I use the "quit save yes" command, most applications will quit without problems. However, if an application has unsaved documents open, then user intervention is required. Is there a way within Applescript to force an application to save an unsaved document to its default location and default title?

Mac Pro, macOS 10.12

Posted on Oct 24, 2019 1:30 AM

Reply

Similar questions

4 replies

Oct 24, 2019 8:53 AM in response to iTBotB

Is it possible to have AppleScript tell a specific application to save unsaved documents in a preset location? generally, yes (although 'the 'default' part is undefined in most cases).


However, is it possible to have AppleScript tell *all* applications? that's a whole different story.


To do this AppleScript needs to interact with the application directly, and the application needs to support the standard 'save' command. Most do, but some apps don't save documents directly so they're going to choke on the 'save' command. Also, the 'save' command requires a location to save - there is no such 'default', so it's up to you to define where to save each document, which means your script needs to have the logic over locations, duplicate/pre-existing filenames, etc.


The logical approach would be to try the 'quit saving yes' and catch the apps that don't quit gracefully, but that doesn't work because the script will wait for the 'quit saving yes' to complete and you can't easily trap the user interaction dialog.


The other approach is to have a specific list of applications that you know will need help and target them directly - that means looking at their list of open documents, determining which ones are new and specifically saving them before issuing the quit command - a lot more work, and something that is not likely to work generically since each application's dictionary for how it handles documents is going to be different.


It's complicated by the fact that the current design standard calls for applications to gracefully save their state and restore 'unsaved' documents for later - most of Apple's apps such as Pages, Numbers, etc. do this. What you're trying to do is back-port this feature into applications that don't support the standard.


So with those caveats, it can likely be done, but it needs a lot more thought to work globally. If you can narrow the scope to a few specific apps you may have more success.




Oct 24, 2019 11:15 AM in response to Camelot

I know most of what you said, but you presented and integrated the information very well, which was very helpful. I'm trying to quit apps prior to a shutdown after a clone backup is complete. I need to "help" the apps that have unsaved documents. By the way, some apps do have default names and locations. Microsoft Word makes the doc name from the initial words of the text and saves in the document folder, which is the default. Excel names the file Workbook1. It's default saving location is the folder containing one of the most recently saved Excel files. TextEdit does the same with Untitled as doc name.


I see two choices. The first is to mix Applescript (to identify running apps that can have unsaved documents) and the Keyboard Maestro macro program (to work through dialog boxes and save the unsaved documents.)


The second choice is to have Applescript tell applications to quit saving yes. Then, Applescript will find the apps still open because of unsaved documents and force quit. That wipes out the unsaved documents, but my unsaved docs are usually just jottings or crude drafts.


I'm going through all this because Sierra, on my Mac, frequently develops problems that cause serious troubles such as browsers that don't work, an inability to open any disk image file, and applications that freeze. A restart fixes the problem—for approximately one to ten days. Shutting down every night should avoid the problems.


Helpful users: I spent many hours troubleshooting. I reinstalled Sierra three times. I can't work in Safe Boot, so I can't run that mode long enough to confirm that the problem is gone. If you have solid evidence for fixing this exact problem, I'll be grateful to get a post. But, I will not spend more hours going through nonspecific troubleshooting techniques, so please don't provide them. Please spend your precious time helping others. (The previous sentence is serious, not sarcastic.)


Oct 24, 2019 10:54 PM in response to iTBotB

I see two choices. The first is to mix Applescript (to identify running apps that can have unsaved documents) and the Keyboard Maestro macro program (to work through dialog boxes and save the unsaved documents.)


The fact you are contemplating Keyboard Maestro is a little concerning. Actions such as saving and quitting should be feasible entirely within AppleScript.


What I think you need to do first is compile a list of target applications and test what each one does when told to quit with new and unsaved documents. From there you can build a workflow with specific actions for each application - some apps may need nothing because they handle it gracefully, others will need complete handholding to save each document (as you said yourself, there's little consistency between apps).


Once you have a handle on each app, it's relatively easy to wrap that in a script that checks for open apps and calls the appropriate handlers. I don't think you're going to find a one-size fits all approach though.

Oct 25, 2019 7:01 AM in response to Camelot

Your belief that everything I need to accomplish can be done in Applescript may be correct, but I doubt it. When Applescript executes the "quit saving yes" command, it stops. It will not proceed until the application has quit. Therefore, Applescript cannot send commands (such as keystroke "return") to the "save previously unsaved document" dialog box. There may be a way to find all the "untitled" documents of a running application before issuing the "quit save yes" command, but I will not spend hours to see if it's possible, especially since some of my apps do not play well with Applescript. Keyboard Maestro and Applescript work very well together. Just before the "quit save yes" command, KM can launch a macro and send Quit. If the appropriate dialog box appears, KM can easily send commands that will save the untitled documents, close the dialog box, quit the application, skip past the "quit save yes" step, and let Applescript move to the next open application. If no unsaved doc dialog box appears, KM will step out of the way and let "quit save yes" run.

applescript quit save yes & unsaved documents

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