3
votes

In my mac app I'm using NSSavePanel - but it's behaving very strange. Sometimes I can't change the default name of the file.

I'm using it like this:

NSSavePanel *savePanel = [NSSavePanel savePanel];
[savePanel setAllowedFileTypes:@[@"jpg"]];
[savePanel setLevel:CGShieldingWindowLevel()];

if([savePanel runModal] == NSFileHandlingPanelOKButton)
{
    //saving file
}

I can't find why sometimes it lets me change the file name and other times no, I can save a file but a changing name in save panel is blocked

3

3 Answers

0
votes

Have you tried providing a default name?

[savePanel setNameFieldStringValue:@"New File.txt"];
0
votes

Swift 4

let panel = NSSavePanel()
panel.nameFieldStringValue = "filename.txt" // <-- user editable prompt 

if panel.runModal() == NSApplication.ModalResponse.OK, let url = panel.url {
    // use url path here
}
0
votes

This is so strange but I would like to share my solution. As @Rupert Pupkin described, I already used nameFieldStringValue but there was strange behaviour of NSSavePanel, it was not changing the name sometimes, and sometimes was changing. So I see I did not describe level of my NSSavePanel

savePanel.nameFieldStringValue = "YourFileName"
savePanel.level = NSWindow.Level.modalPanel

This two lines of code with together worked for me.

My current NSSavePanel code is like that:

let savePanel = NSSavePanel()
savePanel.title = "Download File"
savePanel.prompt = "Download"
savePanel.message = "Choose a directory to save your scanned document"
savePanel.nameFieldStringValue = "myfile"
savePanel.level = NSWindow.Level.modalPanel

savePanel.showsTagField = false
savePanel.isExtensionHidden = false
savePanel.canCreateDirectories = true

Hope that helps!