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.

How to skip the Save dialog when closing unsaved Preview windows

Is there a keyboard shortcut to skip the Save… dialog I get when I close an unsaved Preview document?


Or, second-best option: Is there a way to click the Delete button in that same Save… dialog using the keyboard? I have tried creating a Shortcut for it, but not being familiar with Automator etc., I failed.


Background:

I often take screenshots for copy-pasting them, e.g. into a Quora comment. I don’t want to save those screenshots on my Mac. So after such a session, I end up with quite some Preview windows with unsaved screenshots, and to close them, I am obliged to first click Close (or type Cmd-W) then click the Delete button in the upcoming dialog for every single one of them.


I would be very happy if there could be a keyboard shortcut to skip that dialog.

E.g., Command-Option-W could close a window without asking if I want to save it.


By the way, force-quitting Preview doesn’t work, as it insists on re-opening those unsaved windows when I open it again.

Mac mini, macOS 15.0

Posted on Oct 21, 2024 12:15 AM

Reply
4 replies

Oct 21, 2024 9:19 AM in response to Niel

Thanks!

This lets Preview forget any open documents, so it’s an important step.


The next step will be to save this script as an app.

Then I can run it without having to open the Script Editor, and also assign a keyboard shortcut to it.

Alas, I get an error message when I try to do that:

The document “close Preview without saving.scpt” could not be exported as “close Preview without saving.app”. (Error -5.000)

Any idea how to fix that?

Or is there a way to assign a shortcut to running a script (.scpt) maybe?

Oct 21, 2024 9:47 AM in response to geke108

Thanks!

This lets Preview forget any open documents, so it’s an important step.


The next step will be to save this script as an app.

Then I can run it without having to open the Script Editor, and also assign a keyboard shortcut to it.

Alas, I get an error message when I try to do that:

The document “close Preview without saving.scpt” could not be exported as “close Preview without saving.app”. (Error -5.000)

Any idea how to fix that?


I managed to create a partial alternative: First I changed the Script Editor settings to show it in the menu bar. Then I copied the script into the folder: main disk > Library > Scripts. Now I can run the script by clicking it in the Script Editor drop-down menu.


Building on that, I tried to assign a keyboard shortcut to the script (in System Settings > Keyboard > Keyboard Shortcuts > App Shortcuts > All Applications) as "close Preview without saving" is now a menu item (I thought).

But that didn’t work.

Oct 21, 2024 11:40 AM in response to geke108

I still want to find out why I can’t export the script as an app, but otherwise, I’ve come up with a working solution:


Emboldened when the first step worked (your script to close Preview without saving) I embarked on a more intricate script that goes through the Preview windows one by one and lets the user choose what to do with each of them: discard (the default) or keep.


The script is initiated from the menu bar, after that I either press Enter to discard a file or click Keep to save it, until all windows are closed.


Thanks for the inspiration!


tell application "Preview"
	activate

	set theDialogText to "Close the front window without saving?"

	set OpenWins to the number of windows

	if OpenWins = 0 then return

	repeat with i from 1 to OpenWins

		display dialog theDialogText buttons {"Cancel", "Keep", "Discard"} default button "Discard" cancel button "Cancel" with icon caution

		if result = {button returned:"Discard"} then
			tell application "Preview" to close window 1 without saving

		else if result = {button returned:"Keep"} then
			try
				close window 1 --> call Preview’s Save… dialog
			on error --> if the user clicks Cancel in Preview’s Save… dialog
				return
			end try

		else if result = {button returned:"Cancel"} then
			return

		end if

	end repeat

	--> The script is interrupted (without complaining about an error) when the user presses ESC or Cmd-period	

end tell


How to skip the Save dialog when closing unsaved Preview windows

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