2011-06-14 17 views
1

10.6中的新粘贴板api似乎在您将头部缠绕在UTI上时运行良好,但我遇到了可以't破解:如果你声明多个数据类型与文件拖动一起会怎么样?包含文件,rtfd和自定义类型的粘贴板上的多种类型(Mac OS X 10.6)

查看新的粘贴板的工作方式,使用setString,setData,setPropertyList或writeObjects将数据放在上面。前3个要求您提前指定UTI,以便接收方可以选择所需的表示。

最后一个 - writeObjects - 需要一个NSPasteboardWriting兼容对象数组,例如便利类NSPasteboardItem。

问题是Finder会将添加到粘贴板的任何url解释为文字url,所以不是拖动文件,而是创建url到文件。

没有办法(我可以找到)为URL创建一个NSPasteboardItem。这留下了(来自标题):

APPKIT_EXTERN NSString *NSFilenamesPboardType; //Deprecated 
// Use -writeObjects: to write file URLs to the pasteboard 

但是,如果您将URL与NSPasteboard项混合,则结果不起作用。

NSPasteboardItem *noteItem = [[[NSPasteboardItem alloc] init] autorelease]; 
[noteItem setString:theString forType:NSPasteboardTypeString]; 

//Here is the problem: you can only have one or the other, not both. 
[pasteboard writeObjects:[NSArray arrayWithObjects:noteItem, nil]]; //A 
[pasteboard writeObjects:[NSArray arrayWithObject:fileURL]]; //B 
// A or B will work but not both 
[pasteboard writeObjects:[NSArray arrayWithObjects: 
fileURL, noteItem, nil]]; //Will not work 

我认为这是一个很好的例子,如果有人可以写一些东西,可以完成这两个一起。

下面是测试:

  • 拖动文本编辑应插入文本

  • 拖放到搜索应该添加一个文件。

+0

您的示例的最后一行:'[pasteboard writeObjects:[NSArray arrayWithObjects: fileURL,arrayWithObjects:noteItem,nil]];'甚至没有有效的代码('arrayWithObjects:noteItem'在参数列表中间);是一个错字? – 2011-06-15 05:01:01

+0

我正面临同样的问题 - 你有没有找到解决方案@ sg1? – Frederik 2014-11-18 16:32:36

回答