I am working on a macOS application that involves creating a folder in a temporary directory.
How can I allow users to drag a folder icon off of the Cocoa app and onto their desktop (or into Finder and other apps) so that dropping it will actually paste the folder created by the app?
I have so far been able to write the finished folder that I want to save to a temporary directory. How do I place a folder icon on the application that can be dragged into the Finder? Thanks!
The complete code I'm currently using is this:
class draggableFolder: NSImageView {
override func mouseDown(with theEvent: NSEvent) {
let pasteboardItem = NSPasteboardItem()
// pasteboardItem.availableType(from: [NSPasteboard.PasteboardType.fileURL])
pasteboardItem.setDataProvider(self, forTypes: [kUTTypeURL as NSPasteboard.PasteboardType])
let draggingItem = NSDraggingItem(pasteboardWriter: pasteboardItem)
draggingItem.setDraggingFrame(self.bounds, contents:self.image)
beginDraggingSession(with: [draggingItem], event: theEvent, source: self as NSDraggingSource)
}
}
extension draggableFolder: NSDraggingSource {
func draggingSession(_ session: NSDraggingSession, sourceOperationMaskFor context: NSDraggingContext) -> NSDragOperation {
return NSDragOperation.generic
}
}
extension draggableFolder: NSPasteboardItemDataProvider {
func pasteboard(_ pasteboard: NSPasteboard?, item: NSPasteboardItem, provideDataForType type:
NSPasteboard.PasteboardType) {
print("dataprovider")
if let pasteboard = pasteboard, type.rawValue == String(describing: kUTTypeURL) {
let folder = currentIconsetURL
print("dataprovider2")
print(NSURL.init(fileURLWithPath: currentIconsetURL))
pasteboard.clearContents()
pasteboard.declareTypes([NSPasteboard.PasteboardType.fileNameType(forPathExtension: "appiconset")], owner: nil)
pasteboard.writeObjects([(URL.init(fileURLWithPath: currentIconsetURL).absoluteURL as NSURL) as NSPasteboardWriting])
}
}
}
NSFilePromiseProvider
, the documentation is missing but you can read about it in AppKit Release Notes (macOS 10.12 and Earlier), Dragging File Promise Source. – Lillie