at_yasu's blog

ロード的なことを

Finderからファイルを受け付ける

よくアプリケーションで、デスクトップやFinderからファイルを Drag And Drop している。Cocoaアプリで、FinderからDrag And Drop を受け付ける方法。


まだ浅くしか突っ込んでいないけど、ドラッグされてきた Object は、NSPasteboard という型で管理されている。特にFinderから来た物は、ファイルが絶対パスになって配列で渡される。配列で来るのは複数のファイルを一度に処理する為。

実際、コーディングするとこうなる。

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender

{

    NSPasteboard *pboard = [sender draggingPasteboard];

 

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

        NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];

        int numberOfFiles = [files count];

        // Perform operation using the list of files

    }

    return YES;

}

参考: http://developer.apple.com/documentation/Cocoa/Conceptual/DragandDrop/index.html