Drag file from a NSCollectionView to the desktop

Discussion in 'Mac Programming' started by fernandovalente, Aug 2, 2011.

  1. fernandovalente macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #1
    Hey,

    I'm trying to create an app that will have a NSCollectionView. I need to be to drag an item from the collection view to another app/desktop/Finder window.

    I've implemented the dragging methods, but I'm struggling on this one:

    Code:
    -(BOOL)collectionView:(NSCollectionView *)collectionVie writeItemsAtIndexes:(NSIndexSet *)indexes toPasteboard:(NSPasteboard *)pasteboard
    I tried this code

    Code:
    [pasteboard readFileContentsType:nil toFile:location];
    [pasteboard setString:str forType:NSPasteboardTypeString];
    But when I drag the item to the desktop, just a text clip is copied. If I remove the second like, nothing is copied. What an I doing wrong?
     
  2. Sydde, Aug 3, 2011
    Last edited: Aug 3, 2011

    Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #2
    I suggest you read the docs and guides relating to Dragging Source Protocol. You are missing several steps in this process. You have to:
    1. Declare the type of pasteboard data (in this case, file promises)
    2. Convert the string to NSData
    3. Write the NSData to the pasteboard for the declared type
    (the second and third step can be done "lazily", when the pasteboard requests it)

    EDIT: Oh, I think I also left out the part where your dragging source has to supply file name(s) for the recipient of the data, which is usually before step #2 above.
     
  3. fernandovalente thread starter macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #3
    This is what worked for me:

    Code:
    [pasteboard setPropertyList:[NSArray arrayWithObject:location] forType:NSFilenamesPboardType];
     

Share This Page