Daniel
Daniel

Reputation: 3077

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?

Upvotes: 3

Views: 6235

Answers (4)

Mark Horgan
Mark Horgan

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

Peter Lapisu
Peter Lapisu

Reputation: 21005

[[NSPasteboard generalPasteboard] declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
[[NSPasteboard generalPasteboard] setString:@"My Text" forType:NSStringPboardType];

reference

Upvotes: 5

Stefanf
Stefanf

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

Rob Napier
Rob Napier

Reputation: 299565

See the Pasteboard Programming Guide. You specifically want the section on "Copying to a Pasteboard."

Upvotes: 5

Related Questions