Reputation: 3077
How can i set a string so the user can paste it somewhere else in a cocoa objective c application?
Upvotes: 3
Views: 6235
Reputation: 3313
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.
Upvotes: 4
Reputation: 21005
[[NSPasteboard generalPasteboard] declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
[[NSPasteboard generalPasteboard] setString:@"My Text" forType:NSStringPboardType];
Upvotes: 5
Reputation: 1693
[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
.
Upvotes: 18
Reputation: 299565
See the Pasteboard Programming Guide. You specifically want the section on "Copying to a Pasteboard."
Upvotes: 5