Re: A couple of things I would like to know how to do...
Re: A couple of things I would like to know how to do...
- Subject: Re: A couple of things I would like to know how to do...
- From: Jeff LaMarche <email@hidden>
- Date: Thu, 31 Oct 2002 21:07:08 -0800
On Thursday, October 31, 2002, at 01:44 PM, Jerry LeVan wrote:
>
I am still "in newbie mode in " Cocoa....There are a couple of tasks
>
that I
>
would like to be able to do.
>
>
1) Have the Finder launch my application by dragging a text file onto
>
the
>
application. ( even one created by vi) and somehow notify the
>
application of
>
the dragged file name.
If you add both the .txt extension and text four-letter code (don't
remember if it's 'TEXT' or 'text') with your application as "Editor",
then build and install your application to either the primary
Applications folder, or ~/Applications/, then you should be able to
drag text files onto your application icon. That's all free
functionality if you set it up right.
>
2) Drag a file onto a textField in my application and have the full
>
path
>
name of the file written into the textField.
I haven't worked much with drag and drop, but the drag pasteboard can
hold many types of data. When you drag a file, one of them is a file
type that corresponds to the file, and the Cocoa documentation on
Receiving drops has some example code that appears to do what you want.
You probably need to subclass NSTextField or whatever class you're
currently using, and override the draggingEntered method (this code is
sample code hacked on the fly to do what you want, so no warranties
that it will actually compile):
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
NSPasteboard *pboard;
NSDragOperation sourceDragMask;
sourceDragMask = [sender draggingSourceOperationMask];
pboard = [sender draggingPasteboard];
if ( [[pboard types] containsObject:NSFilenamesPboardType] )
{
if (sourceDragMask & NSDragOperationLink)
return NSDragOperationLink;
else if (sourceDragMask & NSDragOperationCopy)
return NSDragOperationCopy;
}
return NSDragOperationNone;
}
By doing that, you've told it that you want to receive filename(s) in a
drop. You also need to override performDragOperation to receive the
drop, something like (again, quick untested hack):
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
NSPasteboard *pboard;
NSDragOperation sourceDragMask;
sourceDragMask = [sender draggingSourceOperationMask];
pboard = [sender draggingPasteboard];
if ( [[pboard types] containsObject:NSFilenamesPboardType] )
{
NSString *file = [pboard stringForType:NSFilenamesPboardType];
if (sourceDragMask & NSDragOperationLink)
[self setStringValue:file];
}
return YES;
}
Hopefully that points you in the right direction...
>
I can't find any info on 1) and for 2) the info on dragging
>
operations is a
>
bit opaque to me.
Poke around a bit. 2) can be found by going to NSPasteBoard and
following the link to Dragging Operations documentation. The first one
is a little harder to find; I'm sure it's documented, but I can't
recall offhand where it is. I believe I also asked the list about that
one when I was first starting =)
>
Any pointers to documentation and/or examples would be appreciated.
I think the Hillegass book has a pretty good description of the drag
and drop architecture, which may help you wrap your head around it
conceptually.
Jeff
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.