In the Nav Services world one could specify kNavDontConfirmReplacement
as an option to create a NavDialogRef
that would not ask the user to confirm the replacement of a file when saving with a file name that already exists. How do I specify an equivalent behavior with the Cocoa NSSavePanel
?

- 35,429
- 19
- 126
- 178
-
Could you explain why you want to do this? – Mike Abdullah Dec 19 '09 at 11:01
-
I want to replace it with my own confirm-overwrite dialog. – fbrereto Dec 19 '09 at 15:38
-
1Could hijack the undocumented API `- (BOOL)_overwriteExistingFileCheck:(id)arg1;` and/or `- (BOOL)_shouldAlertForFileOverwrite:(id)arg1;` and return `NO`. Won't work for a sandboxed application, though. – Eljay Sep 26 '19 at 11:53
-
@Eljay you are a champion! – fbrereto Sep 26 '19 at 20:52
3 Answers
Here's how it can be done:
- Add a delegate to handle NSSavePanel callbacks
- Override
- (NSString*)panel:(id)sender userEnteredFilename:(NSString*)filename confirmed:(BOOL)okFlag
in your delegate - In the delegate:
- If
okFlag
isfalse
, returnfilename
- Otherwise, retain
filename
as anNSString*
in your delegate - Return some unique string that is highly unlikely to be the name of an actual file
- If
- When
NSSavePanel
returns to your code, pull the value of filename from your delegate method, and discard whatever filenameNSSavePanel
told you (which should be your unique string).
Since userEnteredFilename:
is called by the OS before the confirm-replace check is made it gives you a chance to get what the user specified without letting the OS in on the secret. The unique string will assure that the confirm-replace dialog is not popped accidentally.
Gross but efficacious.

- 35,429
- 19
- 126
- 178
-
Beware that filename may contain slashes. Save panel’s default behavior seems to be to convert them to colons. – George Jul 10 '15 at 04:31
-
This seems to go contrarty to the human interface guidelines not to break the users expectation. There must be a way to do this the correct ( ie less gross ) way. Very slick idea btw – UKDataGeek Jul 29 '16 at 23:32
No, there is no easy way to do this with NSSavePanel. In theory you could extend NSSavePanel with a category and override certain private methods. I took a quick look though and there is nothing simple about it.

- 11,484
- 2
- 36
- 42
-
Some discussion of doing that using overrides here: http://www.cocoadev.com/index.pl?DisableAlertBoxInNSSavePanel – Ken Aspeslagh Dec 21 '09 at 15:07
Your customers is going to expect the exact confirmation alert when faced with a NSSavePanel, so don't customize it.
I'm not sure what kind of customized confirm-overwrite dialog you are planning, but might I suggest you use a NSOpenPanel instead, and customize this dialog box with a "Create New File" button? (I believe you can do this via setAccessoryView API.)
For example, if you are asking your customer to choose a file to append new data to, the NSOpenPanel will work quite well; and if the customer want to save the new data to a new file (instead of appending to an existing file), the "Create New File" button is just an additional click.

- 824
- 1
- 8
- 13