0

Im am trying to get the NsUrl / NSData for an NSPromise image dragged from Safari.

The Apple documentation for drag and drop seems quite unclear. It says I should assume there is a property called Droplocation but doesn't say: - what it is - where it comes from - and how to obtain it.

NSView doesn't have a drop Location property- so it would be useful to know where we get it from before we are asked to assume it exists.

Can anyone help me fathom where I can get the dropLocation in the snippet below from my NSView?

Link to the documentation: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/DragandDrop/Tasks/DraggingFiles.html

NSURL *dropLocation; // Assume this exists

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
    NSPasteboard *pboard = [sender draggingPasteboard];

    if ( [[pboard types] containsObject:NSFilesPromisePboardType] ) {
        NSArray *filenames = [sender
                namesOfPromisedFilesDroppedAtDestination:dropLocation];
        // Perform operation using the files’ names, but without the
        // files actually existing yet
    }
    return YES;
}

Any help with getting a working version of the code above?

Cœur
  • 37,241
  • 25
  • 195
  • 267
UKDataGeek
  • 6,338
  • 9
  • 46
  • 63
  • 1
    `dropLocation` is a variable, you create the url. You tell the dragging source where to write the files. The documentation of `namesOfPromisedFilesDroppedAtDestination` says 'Sets the drop location for promised files and returns the names of the files that the receiver promises to create there.' – Willeke Jul 23 '16 at 17:01
  • That just seems to return Unknown Jpg, and when I try convert that NSURL to data, to upload - it just returns as nil which seems to indicate that there is nothing there. – UKDataGeek Jul 24 '16 at 09:12
  • This answer might help: [How can my OS X app accept drag-and-drop of picture files from Photos.app?](http://stackoverflow.com/a/30110526/4244136). Or don't use promised files, Safari provides other types too. – Willeke Jul 24 '16 at 13:43
  • @Willeke thanks for that - but it seemed very complex to do something simple. Apple could do with simplifying the api – UKDataGeek Jul 24 '16 at 21:08

1 Answers1

0

I was able to get it working thanks to help from comments.

Here is the code that worked for me:

if let pastetype = pasteboard.types {

            let tempDir = NSURL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)

            if pastetype.contains("NSPromiseContentsPboardType") {

                //NSPromise
            let destination = sender.namesOfPromisedFilesDroppedAtDestination(tempDir)

                let tempdestination = tempDir.URLByAppendingPathComponent(destination![0])
                let userInfo:[String:NSURL] = ["fileUrl":tempdestination]

                //I send the resulting file to my viewcontroller using notification. 
            NSNotificationCenter.defaultCenter().postNotificationName(imageDroppedNotification, object: self, userInfo: userInfo)

            }
        }
UKDataGeek
  • 6,338
  • 9
  • 46
  • 63