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.
- File path
- Text data
- 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:
I copied the URL of this page and both "text" and "text2" contain the string from the pasteboard. So it good here :)
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;
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.
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!