0SX
0SX

Reputation: 1292

How to copy textField to OSX clipboard?

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.

Upvotes: 21

Views: 19910

Answers (6)

kaevinio
kaevinio

Reputation: 520

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
    }
}

Upvotes: 4

Andrew_STOP_RU_WAR_IN_UA
Andrew_STOP_RU_WAR_IN_UA

Reputation: 11426

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()
    }
}

Upvotes: 0

Jay Mehta
Jay Mehta

Reputation: 1791

For Swift 5

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

Upvotes: 17

Pier
Pier

Reputation: 10817

For Cocoa macOS in Swift 3:

let pasteBoard = NSPasteboard.general()
pasteBoard.clearContents()
pasteBoard.setString("something", forType: NSPasteboardTypeString)

Upvotes: 1

erkanyildiz
erkanyildiz

Reputation: 13204

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])

Upvotes: 60

Sheamus
Sheamus

Reputation: 6606

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

Upvotes: 3

Related Questions