1

I am using the NSPasteboard to read data off it and perform actions with the received data.

Specifically, I am interested in information of the types listed below in order of priority.

  1. File path
  2. Text data
  3. Image data (this is not copying the image file, but actual parts of the image, for e.g. when you open an image in an editor, make a selection and press command+v)

This is what I have in code

_pasteBoard = [NSPasteboard generalPasteboard];
NSArray *types = [_pasteBoard types];

NSArray *files = [_pasteBoard propertyListForType: NSFilenamesPboardType];

NSString *text = [_pasteBoard propertyListForType: NSStringPboardType];

NSString *text2 = [_pasteBoard propertyListForType: NSPasteboardTypeString];

NSString *rtfText = [_pasteBoard propertyListForType: NSRTFPboardType];

NSData *imageData = [_pasteBoard dataForType: NSTIFFPboardType];

Right now, I am simply testing if I am able to get the required data or not. So When I select some files on the Destkop then files contains the selected file URLs. So that works.

Similarly when I select some parts of an image and copy it to the clipboard then imageData contains some data, which I then write to another file and can see that only the selected potion was copied, so that is OK too. Also, I understand that when I copy an image "file" to the clipboard then too imageData will not be nil, but that is ok because topmost priority goes to file URL and that is the case that will do what needs to be done.

The problem that I have run into is with the lines related to reading text from the pasteboard.

NSString *text = [_pasteBoard propertyListForType: NSStringPboardType];

NSString *text2 = [_pasteBoard propertyListForType: NSPasteboardTypeString];

NSString *rtfText = [_pasteBoard propertyListForType: NSRTFPboardType];

The Problem Descriptio:

  1. I copied the URL of this page and both "text" and "text2" contain the string from the pasteboard. So it good here :)

  2. I copy some lines from a .cs code file opened in TextEdit, the clipboard shows that I have "public.utf8-plain-text" and "NSStringPboardType" but "text" and "text2" are both nil;

  3. From the same C#.NET .cs file, I select a single word, without any breaks for eg. "System.Threading", the clipboard shows that I have "public.utf8-plain-text" and "NSStringPboardType" and both "text" and "text2" have the copied text but any thing copied with spaces or lines will not. "myProject.Core.Helpers" will work but "namespace myProject.Core.Helpers" wont.

  4. Similarly, I copy some text from a RTF file, the clipboard shows that I have a lot of RTF related information along with the same old "public.utf8-plain-text" and "NSStringPboardType" types too, but again "text", "text2" and, this time, "rtfText" are all nil.

So, how do I get the text that I need in all cases from the clipboard? I know that RTF data can contain a lot more then text but I am just concerned with getting the text out in a string file.

Any help will be greatly appreciated.

Thanks!

4

1 回答 1

2

问题几乎可以肯定是您没有考虑粘贴板上的多个项目。在过去(OS X 10.5 及更早版本),粘贴板上只有一项。该项目可能有多种类型,它们只是应该是同一事物的表示。

现在,粘贴板可以有多个项目。每个项目仍然可以有多种类型。该-types方法返回所有项目类型的联合。但-propertyListForType:只会检查第一项。

您可以遍历返回的数组-pasteboardItems并询问每个数组的类型,然后获取每种类型的属性列表对象。

或者,对于您感兴趣的每种类型,您可以-readObjectsForClasses:options:使用相应类的单元素数组进行调用。这不会(而且您通常不应该)区分不同的纯文本类型。所有这些都会匹配NSString

不过,最常见的方法是按照应用程序的偏好(通常是最有表现力到最少)构建一个包含所有可以处理的类的数组,然后调用-readObjectsForClasses:options:. 然后处理所有返回的对象。每个对象将以“最佳”可用形式用于粘贴板上的单个项目。

于 2014-05-24T10:16:48.063 回答