粘贴板上的多种类型,包括文件、rtfd 和自定义类型 (Mac OS X 10.6)

发布于 2024-11-15 23:23:56 字数 1250 浏览 3 评论 0原文

一旦您了解了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的呼吸 2024-11-22 23:23:56

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:).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文