Frederik Jacques
Frederik Jacques

Reputation: 113

How to get filenames when you drag'n'drop files from Finder on your app

I'm trying to let my NSView accept files which you can drag and drop from Finder.

I've already checked the documentation (https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DragandDrop/Tasks/acceptingdrags.html#//apple_ref/doc/uid/20000993-BABHHIHC), but the only thing that works is accepting a drop of a String.

As the docs say, I have registered the types which can be dropped

func commonInit(){

    let allowedDropTypes = [NSFilenamesPboardType]

    registerForDraggedTypes( allowedDropTypes )

    Swift.print( registeredDraggedTypes )

}

If I print out the registeredDraggedTypes, I get NSFilenamesPboardType.

I've also implemented draggingEntered(sender)

override func draggingEntered(sender: NSDraggingInfo) -> NSDragOperation {

    Swift.print("Dragging entered")

    return sender.draggingSourceOperationMask()

}

And prepareForDragOperation(sender) and performDragOperation(sender)

override func prepareForDragOperation(sender: NSDraggingInfo) -> Bool {

    Swift.print("Prepare for drag operation")

    return true

}

override func performDragOperation(sender: NSDraggingInfo) -> Bool {

    Swift.print("Perform for drag operation")

    return true

}

If I run my app, I can't drop any files from Finder on my app.

When I add NSStringPboardType to the array of allowed types, I can drag a piece of text on the app. If I then try to add NSPDFPboardType to the array and drag a pdf on the app, it also doesn't work.

I've also tried working with the UTI public.file-url, but to no avail.

Also, app sandboxing is turned off.

I hope somebody can help me out :-)

Upvotes: 2

Views: 790

Answers (2)

Frederik Jacques
Frederik Jacques

Reputation: 113

I found out what the problem was.

My NSView had NSImageViews as subviews and these were blocking the drag operation. I've added an invisible NSView subview at the top and registered the drag operation on that view. Now everything works fine.

Upvotes: 3

Jelle De Laender
Jelle De Laender

Reputation: 577

Can you try to allow/add the filetypes you want in the Info plist of your project?

example info plist file

I think you need to define the type of files you allow your application to open/handle, although not 100% sure if this is related to drag/drop. Worth a try at least.

Upvotes: 0

Related Questions