Set string to pasteboard (copy, paste) in cocoa application
How can i set a string so the 开发者_开发技巧user can paste it somewhere else in a cocoa objective c application?
[pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self];
[pboard setString:@"Unbelievable" forType:NSStringPboardType];
If you are targeting OS X 10.6 and higher only, use NSPasteboardTypeString
instead of NSStringPboardType
.
See the Pasteboard Programming Guide. You specifically want the section on "Copying to a Pasteboard."
[[NSPasteboard generalPasteboard] declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
[[NSPasteboard generalPasteboard] setString:@"My Text" forType:NSStringPboardType];
reference
This is the way you do if you're targeting OSX 10.6 or higher:
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
[pasteboard writeObjects:@[@"Some string"]];
For more information see the Pasteboard Programming Guide on Apple's site.
精彩评论