blog icon indicating copy to clipboard operation
blog copied to clipboard

How to show save panel in AppKit

Open onmyway133 opened this issue 5 years ago • 0 comments

Enable Read/Write for User Selected File under Sandbox to avoid bridge absent error

func showSave(
    name: String,
    window: NSWindow
) async -> URL? {
    let panel = NSSavePanel()
    panel.directoryURL = FileManager.default.homeDirectoryForCurrentUser
    panel.nameFieldStringValue = name

    let response = await panel.beginSheetModal(for: window)
    if response == .OK {
        return panel.url
    } else {
        return nil
    }
}

To save multiple files, use NSOpenPanel

let panel = NSOpenPanel()
panel.canChooseFiles = false
panel.allowsMultipleSelection = false
panel.canChooseDirectories = true
panel.directoryURL = FileManager.default.homeDirectoryForCurrentUser

Read more

  • https://www.raywenderlich.com/666-filemanager-class-tutorial-for-macos-getting-started-with-the-file-system
  • https://stackoverflow.com/questions/47954418/using-cocoa-nssavepanel-in-sandbox-causes-assertion-failure
  • https://stackoverflow.com/questions/18417432/how-to-show-alert-pop-up-in-in-cocoa

onmyway133 avatar Sep 07 '19 19:09 onmyway133