Funky file URLs from drag & drop

  • Why am I getting URLs from a file-drag operation that look like this?

    /Users/rmann/Library/Developer/Xcode/DerivedData/DataOverlay-bcjfnacanylbcychqjbfqjytdptq/Build/Products/Debug/file:/localhost/Users/rmann/Desktop/SpaceX_CCDEV2.jpg

    They should just be:

    /Users/rmann/Desktop/SpaceX_CCDEV2.jpg

    Here's my code:

    - (BOOL)
    performDragOperation: (id<NSDraggingInfo>) inSender
    {
    NSMutableArray* files = [NSMutableArray arrayWithCapacity: inSender.numberOfValidItemsForDrop];

    [inSender enumerateDraggingItemsWithOptions: 0
        forView: self
        classes: [NSArray arrayWithObject: [NSPasteboardItem class]]
        searchOptions: nil
        usingBlock:
        ^(NSDraggingItem* inDraggingItem,
        NSInteger inIdx,
        BOOL* outStop)
        {
        NSPasteboardItem* item = inDraggingItem.item;
        NSString* path = [item stringForType: @"public.file-url"];
        NSURL* url = [NSURL fileURLWithPath: path];
        [files addObject: url];
        }];

    if ([self.delegate respondsToSelector: @selector(timelineView:didReceiveFileURLs:)])
    {
      [self.delegate timelineView: self didReceiveFileURLs: [files copy]];
    }

    return true;
    }

    TIA,
    Rick
  • On May 11, 2012, at 16:58 , Rick Mann wrote:

    > NSString* path = [item stringForType: @"public.file-url"];
    > NSURL* url = [NSURL fileURLWithPath: path];

    You absolutely can't do this. A URL is not a path, not even when accessed as a string.

    (The first method is returning 'file:/localhost/Users/rmann/Desktop/SpaceX_CCDEV2.jpg', which is treated as a relative path name by the second, and so being prefixed with the absolute path to the current directory.)
  • On May 11, 2012, at 17:10 , Quincey Morris wrote:

    > On May 11, 2012, at 16:58 , Rick Mann wrote:
    >
    >> NSString* path = [item stringForType: @"public.file-url"];
    >> NSURL* url = [NSURL fileURLWithPath: path];
    >
    > You absolutely can't do this. A URL is not a path, not even when accessed as a string.

    Oh, of course. Thanks!

    --
    Rick
previous month may 2012 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today