9

I'm trying to show NSSavePanel (or any "Save File Dialog") on Mac OSX. I'm building COCOA application in XCode Version 9.3 (9E145) in Swift 4 (or 4.2? I'm not sure exactly).

I've tried everything...

Like this?

    let savePanel = NSSavePanel()
    savePanel.begin { (result) in
        if result.rawValue == NSApplication.ModalResponse.OK.rawValue {


        }
    }

this?

    let savePanel = NSSavePanel()
    savePanel.canCreateDirectories = true
    savePanel.showsTagField = false
    savePanel.nameFieldStringValue = "result.csv"
    savePanel.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.modalPanelWindow)))
    savePanel.begin { (result) in
        if result.rawValue == NSApplication.ModalResponse.OK.rawValue {


        }
    }

many many other ways...

What am I missing? Thanks!

Ish Thomas
  • 2,270
  • 2
  • 27
  • 57
  • 2
    Not related to your question but why not simply `savePanel.level = .modalPanel`and `savePanel.begin { if $0 == .OK { print("success") } }` – Leo Dabus Sep 21 '18 at 05:30
  • 1
    @LeoDabus Thanks! – Ish Thomas Sep 22 '18 at 01:35
  • this might help. It is NSOpenPanel but you can easily adapt for saving. https://stackoverflow.com/questions/28008262/detailed-instruction-on-use-of-nsopenpanel/28015428#28015428 – Leo Dabus Sep 22 '18 at 01:38

1 Answers1

18

Is your application sandboxed? (Project > Capabilities > App Sandbox)

If so, ensure that you change "File Access" for "User Selected File" to Read/Write.

When I do that, your first snippet works fine for me.

enter image description here

TheNextman
  • 12,428
  • 2
  • 36
  • 75