Re: Drag and Drop from an app to the Finder
Re: Drag and Drop from an app to the Finder
- Subject: Re: Drag and Drop from an app to the Finder
- From: Charles Srstka <email@hidden>
- Date: Tue, 25 Sep 2001 17:27:13 -0500
On Tuesday, September 25, 2001, at 01:05 PM, Mark Piccirelli wrote:
>
Charles --
>
>
On Sunday, September 23, 2001, at 02:13 AM, Charles Srstka wrote:
>
>
> 1. I have no idea what these different pasteboard types are or how I
>
> should be writing to them. Specifically I wonder about the
>
> CorePasteboard types. It seems that CorePasteboardFlavorType
>
> 0x68667320 is the one that allows the drag-and-drop to work. However,
>
> when CorePasteboardFlavorType 0x68667320 is in the list,
>
> NSFilenamesPboardType and CorePasteboardFlavorType 0x6675726c must
>
> both also be in the list. If CorePasteboardFlavorType 0x68667320 is
>
> present without the other two, the Finder will crash when I try to
>
> drag files to it. What are all these different pasteboard types, and
>
> how should I use them?
>
>
Cocoa automatically converts data your app adds to the pasteboard to
>
types that Carbon apps will understand, and these items are the
>
result. CorePasteboardFlavorType 0x68667320 corresponds to the
>
traditional Mac OS 'hfs ' type (fire up gdb and type "p
>
(char[4])0x68667320"). CorePasteboardFlavorType 0x6675726c is 'furl',
>
which is documented at
>
<http://developer.apple.com/technotes/tn/tn2022.html>.
>
>
Your Cocoa app should never have to add CorePasteboardFlavorType data
>
to a pasteboard. Put an NSFilenamesPboardType item on the pasteboard,
>
and Cocoa will convert it to the 'hfs ' and 'furl' type items that the
>
Finder needs. 'hfs ' items are only added to the pasteboard for files
>
that exist at the moment you add the NSFilenamesPboardType data to the
>
pasteboard. 'furl' items are added to the pasteboard regardless of
>
whether or not the listed files actually exist. I suspect that the
>
Finder needs both 'hfs ' and 'furl' items for dragged files.
>
>
Your Cocoa app should always ignore the CorePasteboardFlavorType items
>
when they're encountered on a pasteboard.
Thanks for the reply! Sorry for the accidental reply to your e-mail
address instead of to the list - I didn't realize that it would do that
until it was too late. And then I sent this with the wrong e-mail
address, causing the moderators to e-mail me about it. Sorry to all for
my clumsiness!
Anyway...
Good to know that I don't need to write to these other types. I think I
understand the problem a little better now. However, I'm not sure yet of
how to fix it. After a little more experimenting, here's what I think is
going on:
In my - outlineView: writeItems: toPasteboard: method, I can use
[pboard declareTypes:[NSArray arrayWithObject:@"NSFilenamesPboardType"]
owner:self];
and this will work as long as I immediately run a [pboard
setPropertyList: forType:] AND that file exists. In this case, the
program immediately does the conversion to 'furl' and 'hfs ' and then
tells the Finder that we have all three types. However, in my program,
the file does not exist yet, and I need to be able to write the path to
the pasteboard later on in my - pasteboard: provideDataForType: method,
in which I also create the files. In this case, if I only declare the
NSFilenamesPboardType and don't write to it yet, the program tells the
Finder that I only have one type, and the Finder thinks it cannot handle
the drop because it doesn't have all the types it needs, and it flings
that icon back into my outline view without even looking at it. And of
course, if the drag and drop action is not completed, the pasteboard:
provideDataForType: method will never be run, the files will not be
created, and the paths will not be written to the pasteboard.
I can fool the Finder into accepting the drag by specifying all five
types in the declareTypes: owner: call (Oddly enough, with certain
combinations of types, I can make the Finder crash and relaunch, or I
can make my program quit with a segfault. It seems to work with all
five, though). This causes the program to work! Unfortunately, it
behaves rather oddly - if you launch the program, use drag and drop at
all, then quit, it quits with a segfault instead of a normal Status 0,
which doesn't affect the operation of the program but would be annoying
to a user who sees "The application has unexpectedly quit" every time
they close the program. Strangely enough, I can fix this problem by
putting the entire contents of the - pasteboard: provideDataForType:
method inside an if ([type isEqualToString:@"CorePasteboardFlavorType
0x68667320"] || [type isEqualToString:@"NSFilenamesPboardType"])
statement, and the program then works. I have no idea why this is,
though, and if I use certain other combinations of types, I can get
things to crash. The program seems to be behaving acceptably now, but I
really don't want my program to crash users' apps!
I have also run up against another problem. Sometimes the files I need
to move are owned by root, in which case the Finder won't move them. I
have some methods that will move these files without too much trouble,
accepting destination paths from the Save As... box instead of drag and
drop. Is there any way to get the Finder just to give me the path that I
dragged the file to so that I can just run that through methods I
already have? I suppose that I could get the Finder to move it by
changing its owner before the move, and changing it back afterwards, but
that doesn't seem as elegant.
Thanks again for the assistance! If I can just get past this one
stumbling block, I really think I can make this program into a useful
product!
>
>
> 2. I also have a problem with dragging multiple files to the Finder at
>
> once. When I drag two files from my program to the Finder, both get
>
> created, but only the first on gets moved to the place I dragged it
>
> to. I have no idea why this occurs.
>
>
This was a bug in the automatic pasteboard item conversion. It's been
>
fixed for Mac OS 10.1.
Good to hear. Hopefully this part of my problem will be solved by next
week, then. :-)
>
>
> 3. I have noticed that I can drag files from my program to any Classic
>
> or Carbon application, but that it never works when dragging to a
>
> Cocoa application. While not absolutely necessary, it would be nice if
>
> this functionality worked, because it would make the program very
>
> flexible.
>
>
I'm not sure what's going wrong with this in your case. You are
>
supposed to be able to drag multiple files from Carbon apps to Cocoa
>
apps. Dragging from the Finder to Project Builder works in 10.0.x, for
>
instance, I'm pretty sure.
It was something embarrasingly simple. I fixed that part of the problem.
#1 is still getting in my way, though.
>
>
-- Mark
>
_______________________________________________
>
cocoa-dev mailing list
>
email@hidden
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev