粘贴板上的多种类型,包括文件、rtfd 和自定义类型 (Mac OS X 10.6)
一旦您了解了 UTI,10.6 中的新粘贴板 api 似乎就可以很好地工作,但我遇到了一种无法破解的情况:如果您在文件拖动中声明多种数据类型怎么办?
查看新粘贴板的工作方式,您可以使用 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
如果有人可以写一些可以同时完成这两个任务的东西,我会认为这是一个很好的例子。
以下是测试:
拖动到 TextEdit 应插入文本
拖动到 Finder 应添加一个文件。
The new pasteboard api in 10.6 seems to work well once you get your head around UTIs, but I've come across a situation that I can't crack: What if you're declaring multiple data types in conjunction with a file drag?
See the way the new pasteboard works, you put data on it using setString, setData, setPropertyList, or writeObjects. The first 3 require that you specify the UTI in advance so the receiver can select the representation it wants.
The last one - writeObjects - requires an array of NSPasteboardWriting compliant objects, such as the convenience class NSPasteboardItem.
The problem is that the Finder interprets any url added to the pasteboard as a literal url, so rather than dragging a file it creates a url to the file.
There is no way (that I can find) to create an NSPasteboardItem for a URL. That leaves this (from the header):
APPKIT_EXTERN NSString *NSFilenamesPboardType; //Deprecated
// Use -writeObjects: to write file URLs to the pasteboard
However, if you mix a URL with an NSPasteboard item the result doesn't work.
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
I would consider it a great example if someone could write something that would accomplish both of these together.
Here is the test:
Drag to TextEdit should insert text
Drag to Finder should add a file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
writeObjects: 不是唯一的方法。您还可以使用:
对于
NSURL
您还有机会使用 NSURL 添加(+URLFromPasteboard:
和-writeToPasteboard:
)。writeObjects: is not the only method. You can also use:
For
NSURL
you also have the opportunity to use the NSURL Additions (+URLFromPasteboard:
and-writeToPasteboard:
).