Topics

Drag & Drop, a lifelong curse

Steve Mills
 

OK, so after "modernizing" my app to add multiple drag items (flavor NSPasteboardTypeFileURL) to the drag clipboard instead of just one with multiple flavors (NSFilenamesPboardType and NSURLPboardType), it's come to my attention that files can no longer be dragged from my app to any other app, e.g., dragging to an app in the Dock results in nothing.

I see this in Xcode's console for each item when I call beginDraggingSessionWithItems:event:source:

Sandbox extension data required immediately for flavor public.file-url, but failed to obtain.

The app is sandboxed, has entitlements for com.apple.security.files.bookmarks-app-scope and com.apple.security.files.user-selected.read-write. Folders urls are granted permission by the user, then urls for individual files within those folders can be added to the drag.

So does dragging files from one app to another need some other flavor added? If so, how do I add additional flavors when using the multiple-item scheme instead of using a single drag image with multiple flavors?

I swear, D&D (and pasteboards in general) is always the most complicated thing, no matter how many changes Apple has put it through.

--
Steve Mills
Drummer, Mac geek

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

Shane Stanley
 

On 12 Dec 2019, at 9:25 am, Steve Mills via Groups.Io <sjmills=mac.com@groups.io> wrote:

So does dragging files from one app to another need some other flavor added?
Maybe I'm missing something, but can't you just use this:

[[NSPasteboard generalPasteboard] clearContents];
[[NSPasteboard generalPasteboard] writeObjects:@[url]];

--
Shane Stanley <sstanley@...>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>

Steve Mills
 

On Dec 12, 2019, at 05:46:27, Shane Stanley <sstanley@...> wrote:

On 12 Dec 2019, at 9:25 am, Steve Mills via Groups.Io <sjmills=mac.com@groups.io> wrote:

So does dragging files from one app to another need some other flavor added?
Maybe I'm missing something, but can't you just use this:

[[NSPasteboard generalPasteboard] clearContents];
[[NSPasteboard generalPasteboard] writeObjects:@[url]];
Does that give you the multiple images thing where different destinations can rearrange the images in stacks, grids, or whatever? My drag source is a collection view or a table view, so the initial drag image[s] is a carbon copy of the selected items' thumbnails or table rows.

--
Steve Mills
Drummer, Mac geek

Shane Stanley
 

On 13 Dec 2019, at 1:17 am, Steve Mills via Groups.Io <sjmills=mac.com@groups.io> wrote:

Does that give you the multiple images thing where different destinations can rearrange the images in stacks, grids, or whatever?
I suspect you can answer that quicker than me.

--
Shane Stanley <sstanley@...>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>

Steve Mills
 

On Dec 12, 2019, at 16:23:46, Shane Stanley <sstanley@...> wrote:

On 13 Dec 2019, at 1:17 am, Steve Mills via Groups.Io <sjmills=mac.com@groups.io> wrote:

Does that give you the multiple images thing where different destinations can rearrange the images in stacks, grids, or whatever?
I suspect you can answer that quicker than me.
I just thought you might know. It's too complex for me to rejigger it all just to find that answer, and I have it working now, so I don't care. :)

--
Steve Mills
Drummer, Mac geek