Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6634833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:58:36+00:00 2026-05-25T22:58:36+00:00

Does anybody have a suggestion on how to go about dragging a file to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:58:36+00:00Added an answer on May 25, 2026 at 10:58 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anybody have a suggestion to use Admob's iOS ads without using the iPhone
Does anybody have a suggestion on how to create a gui that can change
Does anybody have a suggestion on how to create a Wizard in ASP.NET MVC?
Does anybody have experience build app with PhoneGap's Build Service? I followed the steps
Does anybody have any experience with as3-spod? I downloaded the source code from github
does anybody have any experience writing up a offline data storage & access app
Does anybody have a good analogy (or, failing that, a good resource) for describing
does anybody have any idea what's does this mean: onLoad=MyOnLoad? thanks
Does anybody have any advice on working in a Date Driven Development environment? Essentially,
Does anybody have experience programming for both the Intel Math Kernel Library and the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.