Re: Drag & Drop, a lifelong curse


Steve Mills
 

After just trying all kinds of random crap, I got it to work. Turns out, if you call [url pasteboardPropertyListForType:NSPasteboardTypeFileURL], you can't use that with [pbItem setPropertyList:thing forType:NSPasteboardTypeFileURL]. I don't see anything in the docs that says you can't. You ask for a propertyList, so you assume you can use it as a propertyList with another method that has propertyList in the name. Nope, I guess logic was optional the day they invented this part of NSPasteboard and NSPasteboardItem. The docs mention that "the pasteboard will automatically convert these items to the correct data format required for the pasteboard", which makes it sound like it will just work with -setPropertyList:forType:. Nope.

Instead, I had to do this:

NSPasteboardItem* result = [NSPasteboardItem new];
NSString* urlType;

if(@available(macOS 10.13, *))
urlType = NSPasteboardTypeFileURL;
else
urlType = NSURLPboardType;

id probablyAStringMaybeData = [url pasteboardPropertyListForType:urlType];

if([probablyAStringMaybeData isKindOfClass:[NSString class]])
[result setString:probablyAStringMaybeData forType:urlType];
else if([probablyAStringMaybeData isKindOfClass:[NSData class]])
[result setData:probablyAStringMaybeData forType:urlType];
else
[result setPropertyList:probablyAStringMaybeData forType:urlType];

--
Steve Mills
Drummer, Mac geek

Join cocoa@apple-dev.groups.io to automatically receive all group messages.