Save/Copy a File from Bundle to Desktop Using Nssavepanel

Save/Copy a file from Bundle to Desktop using NSSavePanel

You should use Bundle.main.url to get your existing file URL, then get the destination URL with the panel, then copy the file. The panel doesn't do anything to files, it just gets their URL.

Example:

// the panel is automatically displayed in the user's language if your project is localized
let savePanel = NSSavePanel()

let bundleFile = Bundle.main.url(forResource: "MyCustom", withExtension: "zip")!

// this is a preferred method to get the desktop URL
savePanel.directoryURL = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first!

savePanel.message = "My custom message."
savePanel.nameFieldStringValue = "MyFile"
savePanel.showsHiddenFiles = false
savePanel.showsTagField = false
savePanel.canCreateDirectories = true
savePanel.allowsOtherFileTypes = false
savePanel.isExtensionHidden = true

if let url = savePanel.url, savePanel.runModal() == NSFileHandlingPanelOKButton {
print("Now copying", bundleFile.path, "to", url.path)
// Do the actual copy:
do {
try FileManager().copyItem(at: bundleFile, to: url)
} catch {
print(error.localizedDescription)
}
} else {
print("canceled")
}

Also, note that the panel being expanded or not is a user selection, you can't force it from your app.

Using a NSSavePanel from Qt for a native save dialog

I also observed the issue #1 (Qt 4.8.0).
It can be fixed by using QApplication::processEvents () before showing the dialog.

QApplication::processEvents ();
NSInteger result = [panel runModal];

Get File URL Path from Default NSSavePanel

NSSavePanel *panel = [NSSavePanel savePanel];

[panel setMessage:@"Please select a path to create a new database."]; // Message inside modal window
[panel setAllowedFileTypes:[[NSArray alloc] initWithObjects:@"sqlite3", @"sqlite", @"db", nil]];
[panel setAllowsOtherFileTypes:YES];
[panel setExtensionHidden:NO];
[panel setCanCreateDirectories:YES];
[panel setTitle:@"Create a database"]; // Window title
[panel setNameFieldStringValue:@"Untitled.sqlite3"];

[panel beginWithCompletionHandler:^(NSInteger result){
if (result == NSFileHandlingPanelOKButton) {
path = [[panel URL] path];
url = [panel URL];
}
}];

NSSavePanel name not user editable

Setting the application's ActivationPolicy will make it work.

// Import statements... (import Quartz)

NSApplication.shared.setActivationPolicy(.accessory)

// Functions and so on... (func helper..)

Saving to directory in sandboxed app using NSOpenPanel doesn't work, but drag/drop does

As expected, it looks like I was using NSOpenPanel wrong. /p>

Was storing URL from delegate call in panel(_ sender: AnyObject, validate url: URL). Should have been calling the panel's URL properties from the callback in begin(completionHandler handler: (Int) -> Swift.Void).



Related Topics



Leave a reply



Submit