Drag and Drop from an app to the Finder
Drag and Drop from an app to the Finder
- Subject: Drag and Drop from an app to the Finder
- From: Charles Srstka <email@hidden>
- Date: Sun, 23 Sep 2001 04:13:18 -0500
Hello. I am a Cocoa newbie, and I was wondering if anyone on here could
help me on something. I'm writing a Cocoa program in which the user can
drag a file out of an Outline view to the Finder. Due to the nature of
what my program does, the files need to be created at the time of
pasting. What I do is to create the files in a temporary directory, and
then put the pathname in the pasteboard so the Finder will move it from
that location to the new location. I've been having trouble getting this
to work. It works fine when the file already exists at the temp location
and I call NSPasteboard's setPropertyList: forType: immediately after
calling declareTypes: owner: in the outlineView: writeItems:
toPasteboard: delegate method, using NSFilenamesPboardType for the
pasteboard type. However, several problems come up if I try to do
certain things:
If I immediately create the file in the outlineView: writeItems:
toPasteboard: method, it will create the files in the temp directory
even if the user decides against dragging the file and drags it back to
the outline view or the menubar to abort the drag. This is not good for
user experience, especially since a dialog box will sometimes need to
come up when creating the files, so I decided to set the owner and
create the files in the pasteboard: provideDataForType: method. However,
when I did this, I encountered a new problem..
When I created the files in the pasteboard: provideDataForType: method,
and then called setPropertyList: forType: later in that method, the
program no longer allowed me to drag items to the Finder. They flew back
into the outline view after I released the mouse. I could put the
setPropertyList: forType: call back into outlineView: writeItems:
toPasteboard:, but if the files didn't already exist at the time the
paths were written into the pasteboard, it didn't seem to work.
Upon further investigation, I found that if I looked at the pasteboard
after I dragged an icon from an app that had working drag and drop, it
had five types, NSFilenamesPboardType, "Next filename pasteboard type",
"CorePasteboardFlavorType 0x6675726c", "CorePasteboardFlavorType
0x68667320", and "Apple URL pasteboard type". When I did this,
everything finally worked! However, I still have several issues:
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?
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.
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.
If anyone is able to tell me how this works and what I've been doing
wrong, I'd greatly appreciate it. Thanks!