Reading from the clipboard with Swift 3 on macOS

前端 未结 5 1788
轮回少年
轮回少年 2021-02-07 14:44

I\'m a beginner with Swift, and I\'m trying to figure out how can I read what has been copied to the clipboard On macOS (Swift 3)? I\'ve searched a lot but can\'t seem to find a

相关标签:
5条回答
  • 2021-02-07 14:58

    Another Swift 4 Solution

    // Write to pasteboard
    let pasteboard = NSPasteboard.general
    pasteboard.declareTypes([NSPasteboard.PasteboardType.string], owner: nil)
    pasteboard.setString("Good Morning", forType: NSPasteboard.PasteboardType.string)
    
    // Read from pasteboard
    let read = pasteboard.pasteboardItems?.first?.string(forType: .string)
    
    0 讨论(0)
  • 2021-02-07 15:00

    Another solution.

    class ViewController : NSViewController {
    
      @IBAction func pasteMenuItemAction(_ sender: NSMenuItem) {
        let p = NSPasteboard.general
        let x = p.readObjects(forClasses: [NSString.self], options: nil)
        let s = x as! [NSString]
        if 0 < s.count {
          print(s[0])
        }
      }
    
    }
    

    That func pasteMenuItemAction() is bound to an Edit > Paste menu item.

    I use writeObjects() for Edit > Copy. So it is natural for me to use its counterpart readObjects() here.

    Confirmed with Xcode 9.2, Swift 4

    Added:

    One of the solutions for Edit > Copy:

      @IBAction func copyMenuItemAction(_ sender: NSMenuItem) {
        let t = "Hello!"
        let p = NSPasteboard.general
        p.clearContents()
        p.writeObjects([t as NSPasteboardWriting])
      }
    

    That func copyMenuItemAction() is bound to an Edit > Copy menu item.

    0 讨论(0)
  • 2021-02-07 15:01

    Easier ✅ Swift 4:

    @IBAction func pasteAction(_ sender: Any) {
        guard let stringToPaste: String = UIPasteboard.general.items.last?.first?.value as? String else { return }
        MYTEXTVIEW.text = stringToPaste
    }
    
    0 讨论(0)
  • 2021-02-07 15:10

    Times have changed. In Swift 3+ you would do it like this: (if you are only interested in strings)

    func clipboardContent() -> String?
    {
        return NSPasteboard.general.pasteboardItems?.first?.string(forType: .string)
    }
    
    0 讨论(0)
  • 2021-02-07 15:13

    Works for Swift 3 and Swift 4

    // Set string to clipboard
    let pasteboard = NSPasteboard.general
    pasteboard.declareTypes([NSPasteboard.PasteboardType.string], owner: nil)
    pasteboard.setString("Good Morning", forType: NSPasteboard.PasteboardType.string)
    
    var clipboardItems: [String] = []
    for element in pasteboard.pasteboardItems! {
        if let str = element.string(forType: NSPasteboard.PasteboardType(rawValue: "public.utf8-plain-text")) {
            clipboardItems.append(str)
        }
    }
    
    // Access the item in the clipboard
    let firstClipboardItem = clipboardItems[0] // Good Morning
    
    0 讨论(0)
提交回复
热议问题