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
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])
-
3Just figured out (on MonoMac for OS X) that the
clearContents
is required, otherwise thesetString
fails and returnsFALSE
.– Uwe KeimNov 4, 2012 at 18:06 -
Must omit parentheses on first line in Swift implementation: let pasteBoard = NSPasteboard.general– Aarhus88Oct 11, 2017 at 12:52
-
NSStringPboardType
is deprecated: first deprecated in macOS 10.14. ReplaceNSStringPboardType
withNSPasteboardTypeString
.– KamilJun 25, 2021 at 10:51
For Swift 5
let pasteboard = NSPasteboard.general
pasteboard.clearContents()
pasteboard.setString("string to copy", forType: .string)
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
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
}
}
For Cocoa macOS in Swift 3:
let pasteBoard = NSPasteboard.general()
pasteBoard.clearContents()
pasteBoard.setString("something", forType: NSPasteboardTypeString)
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()
}
}