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:
- Delete button has didSave=YES (even though it did not save) and the document fileURL nil
- Cancel button has didSave=NO and document fileURL nil
- 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.
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.