ページ

2008年1月17日木曜日

Finderへドロップ

このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク

今度は立場を替えて View上の画像を Finderへドロップしてファイルを作成してみる。



ペーストボードのタイプは NSFilesPromisePboardType を使う。


- (void)mouseDown:(NSEvent *)theEvent {

NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
[pboard declareTypes:[NSArray arrayWithObject:NSFilesPromisePboardType] owner:self];
[pboard setPropertyList:[NSArray arrayWithObject:@"jpg"] forType:NSFilesPromisePboardType];
NSImage *image = [self image];
[image setSize:[self frame].size];
[self dragImage:[self image] at:NSMakePoint(0, 0) offset:NSMakeSize(0, 0)
event:theEvent pasteboard:pboard source:self slideBack:YES];

return;
}


すると画像をドロップしたタイミングで Finderからファイルの書き出し指示がくる。
これが namesOfPromisedFilesDroppedAtDestination:


- (NSArray *)namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination
{
id dst_path = [[dropDestination relativePath] stringByAppendingPathComponent:@"ocha.jpg"];
id bundle = [NSBundle mainBundle];
id src_path = [bundle pathForResource:@"ocha" ofType:@"jpg"];
NSLog(@"src=%@, dst=%@", src_path, dst_path);
BOOL result = [[NSFileManager defaultManager] copyPath:src_path toPath:dst_path handler:nil];
NSLog(@"rsult=%d", result);
return [NSArray arrayWithObject:@"ocha.jpg"];
}


今回はバンドル内のファイルを指定されたパスへコピーしている。
ファイルがない場合は指定のパスへ直接書き出せば良い。