0

When I retrieve an NSAttributedString from NSPasteboard, the background color is lost. This is what I am copying:

enter image description here

And this is what is returned (I am saving it to an rtf file):

enter image description here

This is my code for it:

func readBoard () {
var board = NSPasteboard.general
    var attr = board.readObjects(forClasses: [NSAttributedString.self])![0] as! NSAttributedString
print(attr)
    export(attributedText: attr)
}

func export(attributedText:NSAttributedString) {

    let file = "Output.rtf"

        do {

            let range = NSRange(location: 0, length: attributedText.length)

            if let data = attributedText.rtf(from: range, documentAttributes: [NSAttributedString.DocumentAttributeKey.characterEncoding : String.Encoding.utf8, NSAttributedString.DocumentAttributeKey.documentType : NSAttributedString.DocumentType.rtf]) {

                if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
                    let url = dir.appendingPathComponent(file)

                    try! data.write(to: url)
                }
            }

        }
        catch {

        }


    }

How do I keep the backgroundColor attribute? Thanks.

Edit:

When I print board.types, I get this:

Optional([__ObjC.NSPasteboard.PasteboardType(_rawValue: dyn.ah62d4qmxhk4d425try1g44pdsm11g55gsu1e82xnqzv1kpneqz30g6xmsb4g86u), __ObjC.NSPasteboard.PasteboardType(_rawValue: com.microsoft.Object-Descriptor), __ObjC.NSPasteboard.PasteboardType(_rawValue: public.rtf), __ObjC.NSPasteboard.PasteboardType(_rawValue: NeXT Rich Text Format v1.0 pasteboard type), __ObjC.NSPasteboard.PasteboardType(_rawValue: public.utf16-external-plain-text), __ObjC.NSPasteboard.PasteboardType(_rawValue: CorePasteboardFlavorType 0x75743136), __ObjC.NSPasteboard.PasteboardType(_rawValue: public.utf8-plain-text), __ObjC.NSPasteboard.PasteboardType(_rawValue: NSStringPboardType), __ObjC.NSPasteboard.PasteboardType(_rawValue: dyn.ah62d4rv4gk81n65yru), __ObjC.NSPasteboard.PasteboardType(_rawValue: CorePasteboardFlavorType 0x7573746C), __ObjC.NSPasteboard.PasteboardType(_rawValue: com.apple.traditional-mac-plain-text), __ObjC.NSPasteboard.PasteboardType(_rawValue: CorePasteboardFlavorType 0x54455854), __ObjC.NSPasteboard.PasteboardType(_rawValue: dyn.ah62d4rv4gk81g7d3ru), __ObjC.NSPasteboard.PasteboardType(_rawValue: CorePasteboardFlavorType 0x7374796C), __ObjC.NSPasteboard.PasteboardType(_rawValue: public.html), __ObjC.NSPasteboard.PasteboardType(_rawValue: Apple HTML pasteboard type), __ObjC.NSPasteboard.PasteboardType(_rawValue: public.utf16-plain-text), __ObjC.NSPasteboard.PasteboardType(_rawValue: CorePasteboardFlavorType 0x75747874), __ObjC.NSPasteboard.PasteboardType(_rawValue: com.adobe.pdf), __ObjC.NSPasteboard.PasteboardType(_rawValue: Apple PDF pasteboard type), __ObjC.NSPasteboard.PasteboardType(_rawValue: dyn.ah62d4qmxhk4d425try1g44pdsm11g55gsu1en5pcqzwc4y5tsz3gg3k), __ObjC.NSPasteboard.PasteboardType(_rawValue: com.microsoft.Embed-Source), __ObjC.NSPasteboard.PasteboardType(_rawValue: dyn.ah62d4qmxhk4d425try1g44pdsm11g55gsu1e24psrq0zg55zsmv0n), __ObjC.NSPasteboard.PasteboardType(_rawValue: com.microsoft.Link-Source), __ObjC.NSPasteboard.PasteboardType(_rawValue: dyn.ah62d4qmxhk4d425try1g44pdsm11g55gsu1e24psrq0zg55zsmv0npneqz30g6xmsb4g86u), __ObjC.NSPasteboard.PasteboardType(_rawValue: com.microsoft.Link-Source-Descriptor), __ObjC.NSPasteboard.PasteboardType(_rawValue: dyn.ah62d4qmxhk4d425try1g44pdsm11g55gsu1e82xnqzv1kxdmr3zu), __ObjC.NSPasteboard.PasteboardType(_rawValue: com.microsoft.ObjectLink), __ObjC.NSPasteboard.PasteboardType(_rawValue: com.apple.webarchive), __ObjC.NSPasteboard.PasteboardType(_rawValue: Apple Web Archive pasteboard type), __ObjC.NSPasteboard.PasteboardType(_rawValue: dyn.ah62d4qmxhk4d425try1g44pdsm11g55gsu1ek2pyqfh0e4xfqr4a), __ObjC.NSPasteboard.PasteboardType(_rawValue: com.microsoft.DataObject)])

Ben A.
  • 874
  • 7
  • 23
  • "NeXT Rich Text Format v1.0 pasteboard type" should be the correct pasteboard type you are looking for – Marek H Mar 21 '18 at 05:28
  • @MarekH How would I use this as the pasteboard type? – Ben A. Mar 21 '18 at 17:26
  • @MarekH I printed `board.types`, and one of the types returned was `__ObjC.NSPasteboard.PasteboardType(_rawValue: NeXT Rich Text Format v1.0 pasteboard type)`, so it is already a type. The issue must be with something else. – Ben A. Mar 22 '18 at 00:09

1 Answers1

0

I found what was wrong. The output only has the backgroundColor when the highlighted text is copied from Text Edit (probably because it is an rtf file). I was copying highlighting from Word.

Ben A.
  • 874
  • 7
  • 23