1

I've made a Mac App with a custom view that is a destination for dragging and dropping files. My view has registered as a destination and I've implemented the drag operation methods. Everything works fine when I build and run the App from XCode, but when I open the .app from Finder dragging files does not work and I see the following error in Console:

Canceling drag because exception 'NSInvalidArgumentException' (reason 'launch path not accessible') was raised during a dragging session

Anyone know what the means or why it's happening? Here is the relevant code:

#import "DragDestinationView.h"

@implementation DragDestinationView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // register for dragging types
      [self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
      highlight = NO;
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
    [super drawRect:dirtyRect];

  if ( highlight ) {
    //highlight by overlaying a gray border
    [[NSColor whiteColor] setFill];
    NSRectFill(dirtyRect);
    [self setWantsLayer:YES];
    self.layer.masksToBounds = YES;
    self.layer.borderWidth = 10.0f;
    [self.layer setBorderColor:[[NSColor grayColor] CGColor]];
  }
  else {
    [self.layer setBorderColor:[[NSColor clearColor] CGColor]];
  }
}

#pragma mark - Destination Operations

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
  NSPasteboard *pboard = [sender draggingPasteboard];

  if ([[pboard types] containsObject:NSFilenamesPboardType]) {

    // check if it's a bin
    NSString* filePath = [[NSURL URLFromPasteboard: [sender draggingPasteboard]] absoluteString];
    if ([[[filePath substringFromIndex:[filePath length] - 4] lowercaseString] isEqualToString:@".bin"]) {
      // it's a bin
      highlight = YES;
      [self setNeedsDisplay:YES];

      return NSDragOperationCopy;
    }
  }
  return NSDragOperationNone;
}

- (void)draggingExited:(id <NSDraggingInfo>)sender
{
  //remove highlight of the drop zone
  highlight=NO;

  [self setNeedsDisplay: YES];
}

- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
  //finished with the drag so remove any highlighting
  highlight=NO;

  [self setNeedsDisplay: YES];

  return YES;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
  if ( [sender draggingSource] != self ) {
    NSString* filePath = [[NSURL URLFromPasteboard: [sender draggingPasteboard]] absoluteString];
    filePath = [filePath substringFromIndex:7];
    filePath = [filePath stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"file path = %@",filePath);

    // send notification
    [[NSNotificationCenter defaultCenter] postNotificationName:@"filePathString" object:filePath];
  }
  return YES;
}


@end

Note: The draggingEntered: method returns NSDragOperationCopy because I like the arrow cursor with the green circle and plus that tells the user they can drag a file here. All I'm actually doing with the dropped file is grabbing its path location.

user924037
  • 141
  • 1
  • 6

1 Answers1

2

Turns out this was not a problem with the drag and drop operation, it had to do with setting the launch path of an NSTask. The solution for a "launch path not accessible" error is located here if you want to execute a binary in the /usr/bin directory and here if you want to execute a binary in your app's Resources folder.

Community
  • 1
  • 1
user924037
  • 141
  • 1
  • 6