Delete button in default NSSavePanel for new document

I just noticed that when closing a new document with edits in MacOS Sonoma that it skips the Save/Don't Save/Cancel panel and goes directly to default NSSavePanel with Delete/Cancel/Save buttons. The problem is that when I click "Delete" nothing happens. It should have simple solution, but I could not find anything. How does one respond to the "Delete" button?

My undocumented (as far as I can tell) hack was to implement

document:didSave:contextinfo

selector for runModalSavePanelForSaveOperation. It appears that in this method for a new document:

  1. Delete button has didSave=YES (even though it did not save) and the document fileURL nil
  2. Cancel button has didSave=NO and document fileURL nil
  3. Save button has didSave=YES and document filieURL to saved file

I can handle Delete button this way, but since it is not a documented method, it make me uncomfortable. For example what happens is user clicks "Save", but the save has an error?

As an aside, since Apple is now working with ChatGPT, I thought it might provide some help. I asked it how I can respond to "Delete" button in MacOS Sonoma and it said to implement deleteDocument: in your NSDocument subclass.

I pointed out to ChatGPT that deleteDocument: does not exist. It said "you are correct" and you should instead check the returned result from runModalSavePanelForSaveOperation and look for "stop" action.

I pointed out to ChatGPT that runModalSavePanelForSaveOperation is void and does not return a result, it said again, "you are correct." It gave a third option which basically said to override runModalSavePanelForSaveOperation and build your own save panel from scratch. I didn't know if I should trust this answer. I reverted to my hack and wrote this post.

Also ChatGPT never apologized for wasting my time with the wrong answers.

Answered by DTS Engineer in 791069022

How does one respond to the "Delete" button?

If you look at the documentation for NSSavePanel, you'll see that there are 3 functions for displaying it, depending on how you want it to behave.

All 3 functions provide a ModalResponse result value, either as the function return or as a parameter to a completion handler. You can use this value to recognize which button was pressed.

when closing a new document with edits in MacOS Sonoma that it skips the Save/Don't Save/Cancel panel and goes directly to default NSSavePanel with Delete/Cancel/Save buttons

If it strikes you as a difference from what you're used to, it's likely because the setting in System Settings -> Desktop & Dock -> Windows -> "Ask to keep changes when closing documents" got changed at some point.

How does one respond to the "Delete" button?

If you look at the documentation for NSSavePanel, you'll see that there are 3 functions for displaying it, depending on how you want it to behave.

All 3 functions provide a ModalResponse result value, either as the function return or as a parameter to a completion handler. You can use this value to recognize which button was pressed.

I read through NSSavePanel, but I don't see any access to the default panel object that gets created by runModalSavePanelForSaveOperation to get the result?

I just found that my problem was caused by a recent change to my code where I wanted to know when the default save panel was done (for scripting options). I did that by using the documented selector of

document:didSave:contextinfo

It works in Ventura. In Somoma the selector always gets called after panel is done (for delete, cancel, or save), but it seems to prevents standard actions from happen such as closing a deleted file or even closing a file that was saved after clicking a close box. If I removed calls to that selector all is working as expected.

Seems I need a new way to hear when default panel is done? It is easy for saves, but how to hear if the panel save was cancelled?

Delete button in default NSSavePanel for new document
 
 
Q