I'm make a Finder extension which can create file at selected folder.I have already set the "Permission/Access" of "User Selected File" to "Read/Write". But the panel doesn't display.
override func menu(for menuKind: FIMenuKind) -> NSMenu {
// Produce a menu for the extension.
let menu = NSMenu(title: "")
menu.addItem(withTitle: "Example Menu Item", action: #selector(sampleAction(_:)), keyEquivalent: "")
return menu
}
@IBAction func sampleAction(_ sender: AnyObject?) {
let savePanel = NSSavePanel()
savePanel.canCreateDirectories = true
savePanel.showsTagField = false
savePanel.nameFieldStringValue = "newfile.txt"
savePanel.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.modalPanelWindow)))
savePanel.begin { (result) in
if result.rawValue == NSApplication.ModalResponse.OK.rawValue {
//write file
}
}
}
And I set FIFinderSyncController.default().directoryURLs = [URL(fileURLWithPath: "/")]
I found some Finder extension App like 'Easy New File' can open save panel. How they achieve it?
Add NSApp.activate(ignoringOtherApps: true)
and DispatchQueue.main.async
.
NSApp.activate(ignoringOtherApps: true)
DispatchQueue.main.async {
let savePanel = NSSavePanel()
savePanel.directoryURL = URL(fileURLWithPath: target.path)
savePanel.canCreateDirectories = true
savePanel.showsTagField = false
savePanel.nameFieldStringValue = "newfile".localized
savePanel.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.modalPanelWindow)))
if (savePanel.runModal() == NSApplication.ModalResponse.OK),let url = savePanel.url{
//wirte file to url
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.