Does anybody have a suggestion on how to go about dragging a file to a small target. What I really need is the files metadata. I don’t need to display the file itself, only it’s contents(these are custom files that are more like directories). I have seen examples for dragging to an NSView but what I think I need is an example of dragging to a simple, small textView in my NSObject class. Then I need to get the files contents so I can parse it.
Does Cocoa require that all drag and drop be done via Views?
Any Help very much appreciated
So to add to what I posted earlier;
I followed the example at http://juliuspaintings.co.uk/cgi-bin/paint_css/animatedPaint/072-NSView-drag-and-drop.pl for drag and drop images. It works great for both browsers and the Finder for images, but it will not work for other file types.
appended 10/4/2011
As stated above, I followed the drag and drop example at the link above for dropping TIFF based images. It works as it should for dragging images into a custom view both from the web or from the Finder. What I don’t understand is what I need to do to get it to work for simple text files and even more importantly, custom files. I have read the drag and drop information on the Mac Dev site but still don’t understand it enough to make the necessary modifications.
Here is my code:
//myNSView.h
#import <Cocoa/Cocoa.h>
@interface MyNSView : NSView
{
NSImage *nsImageObj;
}
@property(assign) NSImage *nsImageObj;
-(IBAction)reset:(id)sender;
@end
//myNSV.m
#import "MyNSView.h"
@implementation MyNSView
@synthesize nsImageObj;
- (id)initWithFrame:(NSRect)frame
{
if(!(self = [super initWithFrame:frame]))
{
NSLog(@"Error: MyNSView initWithFrame");
return self;
}//end if
self.nsImageObj = nil;
[self registerForDraggedTypes:[NSArray arrayWithObjects: NSTIFFPboardType, NSFilenamesPboardType, nil]];
return self;
}//end initWithFrame
- (void)drawRect:(NSRect)dirtyRect
{
if(self.nsImageObj == nil){
[[NSColor blackColor]set];
NSRectFill(dirtyRect);
}//end if
NSRect zOurBounds = [self bounds];
[super drawRect:dirtyRect];
[self.nsImageObj compositeToPoint:(zOurBounds.origin) operation:NSCompositeSourceOver];
}
-(IBAction)reset:(id)sender{
NSLog(@"reset Button Pressed");
nsImageObj = nil;
NSLog(@"check Image %@", self.nsImageObj);
[[NSColor blackColor]set];
[self setNeedsDisplay:YES];
}
-(NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender{
//is the sender looking for NSDragOperationGeneric
if((NSDragOperationGeneric & [sender draggingSourceOperationMask]) == NSDragOperationGeneric)
return NSDragOperationGeneric;
else
return NSDragOperationNone;
}//end draggingEntered
-(BOOL) prepareForDragOperation:(id<NSDraggingInfo>)sender{
return YES;
}// end prepareForDragOperation
-(BOOL)performDragOperation:(id<NSDraggingInfo>)sender{
//initialize pasteboard
NSPasteboard *zPasteboard = [sender draggingPasteboard];
//initialize image file types, addedd some extra
NSArray *zImageTypesArray =[NSArray arrayWithObjects:NSPasteboardTypeTIFF,NSFilenamesPboardType, nil];
NSString *zDesiredType = [zPasteboard availableTypeFromArray: zImageTypesArray];
if([zDesiredType isEqualToString:NSPasteboardTypeTIFF]){
NSData *zPasteboardData = [zPasteboard dataForType:zDesiredType];
//make sure we have data
if(zPasteboardData == nil){
NSLog(@"Error: MyNsView zPasteBoardData == nil");
return NO;
}//end if nil
self.nsImageObj = [[NSImage alloc] initWithData: zPasteboardData];
[self setNeedsDisplay:YES];
return YES;
}//end outer if
//if desired types is a string of filenames
if([zDesiredType isEqualToString:NSFilenamesPboardType]){
NSArray *zFileNamesAry = [zPasteboard propertyListForType:@"NSFilenamesPboardType"];
NSString *zPath = [zFileNamesAry objectAtIndex:0];
NSImage *zNewImage = [[NSImage alloc] initWithContentsOfFile:zPath];
if(zNewImage == nil){
NSLog(@"Error: MyNSView performDragOperation zNewImage = nil");
return NO;
}
//else everything is good in here
self.nsImageObj = zNewImage;
[self setNeedsDisplay:YES];
return YES;
}//end outer if
//if we get here than there was an unknown error return no
NSLog(@"Error Unknown in MYNSView performDragOperation");
return NO;
}
-(void)concludeDragOperation:(id<NSDraggingInfo>)sender{
[self setNeedsDisplay:YES];
}
@end
I am hoping some one can point out what I need to learn to get this figured out. Maybe its my confusion with Pasteboards, maybe its another component I’m not even aware of yet. As always, I really appreciate the help.
Thanks
First, an NSTextView is a subclass of NSView (distantly). Second, to receive drop events, yes, you really do need a view. Third, what you’re having trouble with is the type of information on the dragging pasteboard.
Set up a view and register for NSFilenamesPboardType as described in the Drag and Drop Programming Topics guide. When you get your file names, use NSFileManager or use the Spotlight metadata API to get whatever information about the file that you need.