Question

I am working on an application where I need to be able to know what type of data is just get copied onto NSPasteBoard. From documentation I knew that when we copy any content of file NSPasteBoard passes that content to pasteboard server from which any application on system can access that data.

Now, I want poll the paste board server and determine whether user has just copied some textual content or not, if it is textual content then the application will will handle those data otherwise ignore other data such as pictures, folder etc.


Findings from Apple documentation

From documentation I found that, when we set data onto the pasteboard, we can setData types. But I am not 100% sure what type of datatype is being set if we copy some text content on system. The parameter of the dataType in setData method is NSString.


Any further guidance will be very helpful.

Was it helpful?

Solution

Well designed applications that copy text to the pasteboard should use one of the standard types listed in the documentation:

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSPasteboard_Class/Reference/Reference.html#//apple_ref/doc/uid/20000328-SW33

So, you can just check if some of the standard text types, like NSPasteboardTypeString or NSPasteboardTypeRTF, exist on the pasteboard with -[NSPasteboard availableTypeFromArray:]

Applications may put data in the pasteboard in many different forms. For example, if you copy text in Safari, you'll get both plain text and rich text representations. This is a nice way to support all sorts of applications; if an app can't paste rich text, perhaps it can paste plain text, so Safari copies both.

From how you describe the problem though, the fact that applications copy multiple types to the pasteboard might make things more challenging for figuring out if just text was copied. For example, copying an image in Safari copies the image data into NSPasteboardTypeTIFF, but also copies the URL into NSPasteboardTypeString. So, (depending totally on what you want to accomplish) you may want to check for the absence of certain types, like NSPasteboardTypeTIFF, in addition to the presence of text types, to figure if useful text was copied.

A great way to explore all the data on the pasteboard is with Apple's ClipboardViewer example code: http://developer.apple.com/library/mac/#samplecode/ClipboardViewer/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008825-Intro-DontLinkElementID_2

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top