33

I cannot get this method to return YES:

- (BOOL) writeToPasteBoard:(NSString *)stringToWrite
{
    return [pasteBoard setString:stringToWrite forType:NSStringPboardType];
}

I have verified that stringToWrite is coming through properly, the method just always returns NO.

Any ideas?

Here is the rest of the class:

@interface ClipBoard : NSObject {
    NSPasteboard *pasteBoard;
}

- (BOOL) writeToPasteBoard:(NSString *)stringToWrite;
- (NSString *) readFromPasteBoard;
@end

@implementation ClipBoard
- (id) init
{
    [super init];
    pasteBoard = [NSPasteboard generalPasteboard];
    return self;
}

- (BOOL) writeToPasteBoard:(NSString *)stringToWrite
{
    return [pasteBoard setString:stringToWrite forType:NSStringPboardType];
}

- (NSString *) readFromPasteBoard
{
    return [pasteBoard stringForType:NSStringPboardType];
}

@end

joshwbrick
  • 5,882
  • 9
  • 48
  • 72

11 Answers11

38

Here is the working version of the method:

- (BOOL) writeToPasteBoard:(NSString *)stringToWrite
{
    [pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
    return [pasteBoard setString:stringToWrite forType:NSStringPboardType];
}
joshwbrick
  • 5,882
  • 9
  • 48
  • 72
  • 6
    You should use nil for object pointers, NULL only for non-object pointers. Using the wrong one misleads the reader (who will be you six months from now). – Peter Hosey Feb 28 '09 at 20:41
  • 2
    Actually this code snippet came from an Apple provided example. I think they would know the right way to do things. But I digress, it is now changed to nil. – joshwbrick Mar 01 '09 at 01:59
  • 8
    Sometimes Apple examples are written hastily or were written a long time ago. Some of the "Sample Code" projects they provide contain crimes against nature. Things change, stale examples are forever. :-) – Joshua Nozzi Mar 21 '12 at 13:22
21

Swift 2:

Copy a string to the general pasteboard with Swift 2:

let pasteboard = NSPasteboard.generalPasteboard()
pasteboard.declareTypes([NSPasteboardTypeString], owner: nil)
pasteboard.setString("Hello", forType: NSPasteboardTypeString)
Sebastian
  • 8,952
  • 3
  • 32
  • 30
19

Before you copy a string on NSPasteboard it's better to clear the contents and then save.

Swift 4

    // Set string
    NSPasteboard.general.clearContents()
    NSPasteboard.general.setString("I copied a string", forType: .string)
    // Read copied string
    NSPasteboard.general.string(forType: .string)

Objective-C

    // Set string
    [[NSPasteboard generalPasteboard] clearContents];
    [[NSPasteboard generalPasteboard] setString:@"I copied a string" forType:NSPasteboardTypeString];
    // Read string
    [[NSPasteboard generalPasteboard] stringForType:NSPasteboardTypeString];

And also there are other available types for copying items on NSPasteboard. Like:

  • NSPasteboardTypeString
  • NSPasteboardTypePDF
  • NSPasteboardTypeTIFF
  • NSPasteboardTypePNG
  • NSPasteboardTypeRTF

You can find more details on https://developer.apple.com/documentation/appkit/nspasteboardtype.

abdullahselek
  • 7,893
  • 3
  • 50
  • 40
15

Apple is suggesting people move away from NSStringPboardType and use NSPasteboardTypeString instead.

Jason Fuerstenberg
  • 1,341
  • 13
  • 13
14

As of 10.6, the following implementation is also possible:

- (BOOL) writeToPasteBoard:(NSString *)stringToWrite
{
    [pasteBoard clearContents];
    return [pasteBoard writeObjects:[NSArray arrayWithObject:stringToWrite]];
}

It's important to notice that #clearContents has to be called before something new can be written to the pasteboard, otherwise #writeObjects: keeps returning NO.

The new #writeObjects: methods is possible for objects that conform to the NSPasteboardWriting protocol. There's also an NSPasteboardReading Protocol, but using it wouldn't make the implementation any simpler.

Karsten
  • 2,772
  • 17
  • 22
4

Swift 4 version:

NSPasteboard.general.clearContents()
NSPasteboard.general.setString("Hello World", forType: .string)
Dave Wood
  • 13,143
  • 2
  • 59
  • 67
1

This works on Mojave 10.14.5 and does not use any deprecated APIs:

NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard declareTypes:[NSArray arrayWithObjects:NSPasteboardTypeString, nil] owner:nil];
[pasteboard setString:@"Hello clipboard!" forType:NSPasteboardTypeString];
mrexodia
  • 648
  • 12
  • 20
1

For macOS 10.12 and later:

let pasteboard = NSPasteboard.general
pasteboard.prepareForNewContents()
pasteboard.writeObjects(["hello world" as NSString])
Jonny
  • 1,969
  • 18
  • 25
1

Swift 5

To copy a string in the clipboard you need to "prepare the pasteboard" before set string to copy

    NSPasteboard.general.prepareForNewContents()
    NSPasteboard.general.setString(stringToCopy, forType: .string)
L. Davì
  • 31
  • 2
0

For the record, to copy a string to the system clipboard, you can use

NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
[pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
[pasteBoard setString:string forType:NSStringPboardType];
Snowman
  • 31,411
  • 46
  • 180
  • 303
0

Swift 5,

let pasteBoard = NSPasteboard.general
pasteBoard.clearContents()
pasteBoard.setString("Test string",forType :.string)

Retrieve part,

NSTextField.pasteAsPlainText(NSPasteboard.general.string(forType:.string)