使用UTI而不是NSFileContentsPboardType将文件内容放在NSPasteboard上?

发布于 2024-11-08 02:44:43 字数 1567 浏览 1 评论 0原文

我有包含一些图像和文本的富文本文件。在我的 Cocoa 应用程序中,我尝试将该文件的内容放在 NSDragPboard 上,以便用户可以将内容拖到其他应用程序(TextEdit、Mail 等)中。 )。

一个用例是这样的:富文本文件包含电子邮件模板,用户将内容拖到新的邮件消息中。

我尝试了这个但没有成功:

NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
[pboard writeFileContents:filePath];

Apple 文档是这样说的:

写入文件内容 文件名发送给接收者并声明 数据类型 NSFileContentsPboardType 以及 适合该文件的类型 扩展名(由 NSCreateFileContentsPboardType 传递文件时的函数 扩展名),如果有的话。

在 ClipboardViewer 应用程序中,我可以看到一些数据类型,其中之一是 NXFileContentsPboardType。但是,邮件和文本编辑等应用程序不允许删除该数据类型。

当我手动将富文本内容复制到剪贴板 (Control+C) 时,我看到了所需的所有不同数据类型:

  • public.rtf
  • public.utf8-plain-text< /code>
  • NSStringPboardType
  • ...

那么我怎样才能自己以编程方式做到这一点呢?我是否必须弄清楚所有各种尿路感染类型并设置相应的数据?我认为这就是 [pboard writeFileContents:path] 的用途...

谢谢,马克。

编辑:

取得进展...这适用于.rtf文件,但不适用于.txt文件:

NSString *uti = // helper code to get UTI from NSURL resource key...
NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
[pboard declareTypes:[NSArray arrayWithObject:uti] owner:nil];
[pboard setData:[NSData dataWithContentsOfFile:filePath] forType:uti];

对于.txt 文件粘贴板上的数据类型为 public.text。但是,TextEdit 和 Mail.app 不允许删除该类型。 public.rtf 在富文本的情况下工作正常......

I have rich-text file with a few images and text. In my Cocoa app I'm trying to put the contents of that file on the NSDragPboard so that the user can drag the contents into other applications (TextEdit, Mail, etc.).

One usecase is this: the rich-text file contains an email template and the user drags the contents into a new Mail message.

I tried this without success:

NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
[pboard writeFileContents:filePath];

The Apple docs say this:

Writes the contents of the file
filename to the receiver and declares
the data to be of type
NSFileContentsPboardType and also of a
type appropriate for the file’s
extension (as returned by the
NSCreateFileContentsPboardType
function when passed the files
extension), if it has one.

In the ClipboardViewer app I can see a few data types, one being NXFileContentsPboardType. However, apps such as Mail and TextEdit don't allow to drop that data type.

When I copy the rich-text content to the clipboard by hand (Control+C), I see all the different data types I want:

  • public.rtf
  • public.utf8-plain-text
  • NSStringPboardType
  • ...

So how can I do that programmatically myself? Do I have to figure out all the various UTI types and set the according data? I thought that's what [pboard writeFileContents:path] was for...

Thanks, Mark.

EDIT:

Making progress... This works for .rtf files, but not for .txt files:

NSString *uti = // helper code to get UTI from NSURL resource key...
NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
[pboard declareTypes:[NSArray arrayWithObject:uti] owner:nil];
[pboard setData:[NSData dataWithContentsOfFile:filePath] forType:uti];

For .txt files the data type on the pasteboard is public.text. However, TextEdit and Mail.app won't allow dropping that type. public.rtf in case of rich text works fine...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

他是夢罘是命 2024-11-15 02:44:44

似乎无法将文件内容添加到粘贴板并自动检测内容类型。

事实证明,将 RTF/RTFD 文件内容放在粘贴板上非常简单:

NSAttributedString *contents = [[NSAttributedString alloc] initWithPath:filePath documentAttributes:NULL];
// This sets the correct type automatically:
[pboard writeObjects:[NSArray arrayWithObject:contents]];
[contents release];

Looks like it's not possible to add file contents to the pasteboard and have the content types be detected automatically.

It turns out that putting RTF/RTFD file contents on the pasteboard is as simple as:

NSAttributedString *contents = [[NSAttributedString alloc] initWithPath:filePath documentAttributes:NULL];
// This sets the correct type automatically:
[pboard writeObjects:[NSArray arrayWithObject:contents]];
[contents release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文