家伙!有谁知道如何实现这一目标?我的意思是:如果我想把一个字符串放到剪贴板上我做
NSPasteboard *pboard = [NSPasteboard generalPasteboard];
[pboard declareTypes:[NSArray arrayWithObject:NSPasteboardTypeString] owner:self];
[pboard setString:@"aString" forType:NSPasteboardTypeString];
但如果我想将文件放入剪贴板怎么办?我想找到一种方法来使用文件路径作为参数,然后能够通过上下文菜单或CMD-V将该文件粘贴到Finder中的任何位置。
有人能帮助我吗?
非常感谢!
答案 0 :(得分:5)
快速将它们放在一起,但是使用CMD + V和Contextual Paste进行测试。
NSURL *object = [[NSURL alloc] initFileURLWithPath:@"/Users/username/Desktop/main.png"];
NSPasteboard *pb = [NSPasteboard generalPasteboard];
[pb clearContents];
NSArray *objectsToCopy = [[NSArray alloc] initWithObjects:object, nil];
BOOL pasted = [pb writeObjects:objectsToCopy];
if(pasted) // paste was successful
NSLog(@"pasted");
[object release];
[objectsToCopy release];
[pb release];
答案 1 :(得分:5)
在小时,小时和小时搜索之后,我终于得到了一个简短的答案......
NSArray *fileList = [NSArray arrayWithObjects:filePath1, filePath2, nil];
NSPasteboard *pboard = [NSPasteboard generalPasteboard];
[pboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:nil];
[pboard setPropertyList:fileList forType:NSFilenamesPboardType];
答案 2 :(得分:0)
我没有尝试过这个,但据我理解文档,你所要做的就是(至少在OS X 10.6+中)是这样的:
NSURL *fileURL = [NSURL fileURLWithString:...];
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard writeObjects:[NSArray arrayWithObject:fileURL]];
并从粘贴板中读取URL:
NSArray *urls = [pasteboard readObjectsForClasses:[NSArray arrayWithObject[NSURL class]]
options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:NSPasteboardURLReadingFileURLsOnlyKey]];
if ([urls count] == 1) {
NSURL *myFileURL = [urls objectAtIndex:0];
...
}