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
How to Present a UIcollectionview in Swiftui with UIviewcontrollerrepresentable
Enum of String Type Vs Struct with Static Constant
iOS Swift, Aws Cognito User Pool, Unable to Refresh Access Token
Produce Sounds of Different Frequencies in Swift
Swift 2.0 Replicate Objc_Association_Retain
Swift System Version Checking on Ubuntu
Where Do I Register a Valuetransformer in Swift
Uidatepicker Show Only Sunday's Date Only
How to Get Reliable Timing for My Audio App
How to Create a Pulse Effect on an Skspritenode
How to Fix Cannot Find 'Firebaseapp' in Scope
How to Create an iOS Liveview in Xcode 8/Swift 3
Using Getters and Setters to Modify Values W/O Subclassing in Swift
Using Multiple Hosting Controllers in Swiftui on Watchos
How to Make Nsbutton Title at Bottom and Centered
Converting Scanlocation from Utf16 Units to Character Index in Nsscanner (Swift)