19

I'm stuck here. I know how to copy and paste on the iPhone side of things but how can I copy contents from a textField to the global clipboard in OSX. I've been searching the web but there are really no examples. So let me explain in detail what I'm trying to accomplish. I have a NSTextField named helloField and I want to be able to copy the contents of this helloField to the global pasteboard by pressing a button. How can this be done and is there certain libraries I need? Thanks.

6 Answers 6

58

On iOS

[UIPasteboard generalPasteboard].string = helloField.text;

On OSX

[[NSPasteboard generalPasteboard] clearContents];
[[NSPasteboard generalPasteboard] setString:helloField.stringValue forType:NSStringPboardType];

On macOS and Swift 3.x

let pasteBoard = NSPasteboard.general()
pasteBoard.clearContents()
pasteBoard.writeObjects([text as NSString])
3
  • 3
    Just figured out (on MonoMac for OS X) that the clearContents is required, otherwise the setString fails and returns FALSE.
    – Uwe Keim
    Nov 4, 2012 at 18:06
  • Must omit parentheses on first line in Swift implementation: let pasteBoard = NSPasteboard.general
    – Aarhus88
    Oct 11, 2017 at 12:52
  • NSStringPboardType is deprecated: first deprecated in macOS 10.14. Replace NSStringPboardType with NSPasteboardTypeString.
    – Kamil
    Jun 25, 2021 at 10:51
12

For Swift 5

let pasteboard = NSPasteboard.general
pasteboard.clearContents()
pasteboard.setString("string to copy", forType: .string)
2

Code to copy a string to the clipboard:

[[NSPasteboard generalPasteboard] clearContents];
[[NSPasteboard generalPasteboard] setString:copiedString forType:NSPasteboardTypeString];

NSStringPboardType is deprecated. There's a note in NSPasteboard.h about pboard types:

Use of pboard types should be replaced with use of UTIs. Pboard types will be deprecated in a future release.

Also in the header file:

APPKIT_EXTERN NSString *const NSPasteboardTypeString NS_AVAILABLE_MAC(10_6); // Replaces NSStringPboardType
...
APPKIT_EXTERN NSString *NSStringPboardType; //Use NSPasteboardTypeString

2

You can create an extension for your String which supports iOS and macOS:

extension String {
    func copy() {
        #if os(macOS)
        let pasteboard = NSPasteboard.general
        pasteboard.clearContents()
        pasteboard.setString(self, forType: .string)
        #else
        UIPasteboard.general.string = self
        #endif
    }
}
1

For Cocoa macOS in Swift 3:

let pasteBoard = NSPasteboard.general()
pasteBoard.clearContents()
pasteBoard.setString("something", forType: NSPasteboardTypeString)
0
Clipboard.set("some text")

class:

import AppKit

public class Clipboard {
    public static func set(text: String?) {
        if let text = text {
            let pasteBoard = NSPasteboard.general
                pasteBoard.clearContents()
                pasteBoard.setString(text, forType: .string)
        }
    }
    
    @available(macOS 10.13, *)
    public static func set(url: URL?) {
        guard let url = url else { return }
        let pasteBoard = NSPasteboard.general
        
        pasteBoard.clearContents()
        pasteBoard.setData(url.dataRepresentation, forType: .URL)
    }
    
    @available(macOS 10.13, *)
    public static func set(urlContent: URL?) {
        guard let url = urlContent,
              let nsImage = NSImage(contentsOf: url)
        else { return }
        
        let pasteBoard = NSPasteboard.general
        pasteBoard.clearContents()
        pasteBoard.writeObjects([nsImage])
    }
    
    public static func clear() {
        NSPasteboard.general.clearContents()
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.