'Swift 4+: Issues copying a String to Clipboard using NSPasteboard

I had this all working in Swift 3 and earlier but with Swift 4 no matter what variation I use this code will instead output text as a URL. If I put in "This is my sample text" the output after pasting the clipboard will be "This%20is%20my%20sample%20text". I have tried KuTTypeFileURL but that doesn't appear to make any difference either. What am I missing here? I have seen posts and discussions about how Apple is changing Pboards and other issues with sandboxing but I can't seem to figure this out at all.

original code what was working in swift 3 and earlier

private func copyToClipBoard(textToCopy: String) {
    let pasteBoard = NSPasteboard.general()
    pasteBoard.clearContents()
    pasteBoard.setString(textToCopy, forType: NSStringPboardType)

}

This gives an error of 'NSStringPboardType' is unavailable in Swift: use 'PasteboardType.string'

After searching online I came across these posts that describe the same issue and the workaround was to use the kuTTypeUrl as String

Found here stackoverflow.com/questions/44537356/… and here forums.developer.apple.com/thread/79144

When I try it this way it simply outputs as a URL when I just need a String.

@IBOutlet weak var nameTextField: NSTextField!

@IBAction func nameCopy(_ sender: Any) {
    copyToClipBoard(textToCopy: nameTextField.stringValue)
}


let NSStringPboardType = NSPasteboard.PasteboardType(kUTTypeURL as String)

private func copyToClipBoard(textToCopy: String) {
    let pasteBoard = NSPasteboard.general
    pasteBoard.clearContents()
    pasteBoard.setString(textToCopy, forType: NSStringPboardType)
}


Solution 1:[1]

You are pasting an URL because you created a PasteboardType kUTTypeURL.

The solution is much simpler, there is a predefined string type

private func copyToClipBoard(textToCopy: String) {
    let pasteBoard = NSPasteboard.general
    pasteBoard.clearContents()
    pasteBoard.setString(textToCopy, forType: .string)

}

The note in the documentation

Apps that adopt App Sandbox cannot access files identified using the string pasteboard type. Instead, use an NSURL object, a bookmark, or a filename pasteboard type.

is related to files (aka string paths), not to regular strings

Solution 2:[2]

I just ran into a similar issue. My code looked like this:

NSPasteboard.general.setString("Hello World", forType: .string)

Unfortunately, this didn't work. But I figured there is a bug that if you don't store the NSPasteboard.general into a variable, the object created as part of the general computed property gets deinitialized before the setString change is propagated to the system.

So if you tried doing this in one line like me, just split it up to two instead, which worked for me:

let pasteboard = NSPasteboard.general
pasteboard.setString("Hello World", forType: .string)

I reported this bug via Feedback Assistant to Apple (FB9988062).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Jeehut