从OS X中的剪贴板获取文件路径

时间:2013-04-01 18:08:11

标签: macos cocoa clipboard nspasteboard

我希望我的应用程序能够将文件的完整路径复制到剪贴板之前。

我试过了:

NSPasteboard *p = [NSPasteboard generalPasteboard];
NSDictionary *options = [NSDictionary dictionary];
NSString *path = [[p readObjectsForClasses:[NSArray arrayWithObjects:[NSString class], nil] options:options] objectAtIndex:0];

这只返回文件名,而不是路径。

你能帮助我吗?

1 个答案:

答案 0 :(得分:8)

试试这个:

NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSArray *classes = [NSArray arrayWithObject:[NSURL class]];

NSDictionary *options = [NSDictionary dictionaryWithObject:
                         [NSNumber numberWithBool:YES] forKey:NSPasteboardURLReadingFileURLsOnlyKey];

NSArray *fileURLs =
[pasteboard readObjectsForClasses:classes options:options];

直接来自Apple的Pasteboard Programming Guide