How to get the type of pasteboard entry?
I've got an NSPasteboard:
NSPasteboard *pasteboard;
and get it with:
pasteboard = [[NSPasteboard generalPasteboard] retain];
Now I want to determine of what type the last entry is, like formatted text or image etc. and if text, get the contents of it etc.
How do I find out the type of data hold in the pastboard?
I logged the output from [pasteboard types]:
2011-05-07 20:13:30.491 YourApp[15335:903] Pasteboard changed: (
"public.utf8-plain-text",
NSStringPboardType,
"dyn.ah62d4rv4gu8y63n2nuuhg5pbsm4ca6dbsr4gnkduqf31k3pcr7u1e3basv61a3k",
"NeXT smar开发者_StackOverflow中文版t paste pasteboard type"
)
but I'm not sure how to check if it's text or anything else...
This is covered fully in the Pasteboard Programming Guide. The standard way to do this is to make a list of the types you are interested in and then just ask the pasteboard to give you those types. If it has objects of those types, you get them. Otherwise, you get nothing.
NSPasteboard * pboard = [NSPasteboard generalPasteboard];
NSArray * interestingTypes;
interestingTypes = [NSArray arrayWithObjects:[NSAttributedString class],
[NSString class], nil];
NSArray * pboardContents = [pboard readObjectsForClasses:interestingTypes
options:nil];
if( pboardContents ) {
// Use the contents
}
You can also make multiple inquiries with different lists of types:
NSArray * imgType = [NSArray arrayWithObject:[NSImage class]];
NSArray * strType = [NSArray arrayWithObject:[NSString class]];
NSArray * pboardImg = [pboard readObjectsForClasses:imgType
options:nil];
NSArray * pboardStr = [pboard readObjectsForClasses:strType
options:nil];
if( pboardImg ){
// Got an image!
}
if( pboardStr ){
// Got a string!
}
You can also simply ask the pasteboard if it has one of the types you're interested in, without actually getting the objects. This will give you a simple YES
or NO
:
[pboard canReadObjectForClasses:interestingTypes
options:nil];
Note that pasteboard handling changed in Snow Leopard, so this is, unfortunately, all different in Leopard.
精彩评论