When I retrieve an NSAttributedString from NSPasteboard, the background color is lost. This is what I am copying:
And this is what is returned (I am saving it to an rtf file):
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)])