1

I am trying to save the content of a NSTextView item containing text, images, and different formatting as bold, italic, different font size and color. Currently I am only able to save the raw text but not the images nor the formatting.

let fileURL = savePath.appendingPathComponent("savedText.txt")
if !fileManager.fileExists(atPath: fileURL.path) {
    do {
        try fileManager.createDirectory(at: savePath, withIntermediateDirectories: true, attributes: nil)
        fileManager.createFile(atPath: fileURL.path, contents: nil, attributes: nil)
        print("File created :", fileURL)
    } catch {
        print("Failed to save text to file:", error)
    }
}
do {
    try textView.string.write(to: fileURL, atomically: true, encoding: .utf8)
} catch {
    print("Failed to save the file")
}

I tried saving the file as rtf or rtfd but it's only saving the raw content.

Edit : I found how to save and load the rich text but still now way to save the images pasted in the text view. If I make a file .rtfd with an image it loads so the problem is clearly somewhere in the save function. Here is my new code to save :

// Save the text to a file whenever it changes
let fileURL = savePath

// if the file doesn't exist make one
if !fileManager.fileExists(atPath: fileURL.path) {
    do {
        try fileManager.createDirectory(at: savePath, withIntermediateDirectories: true, attributes: nil)
        fileManager.createFile(atPath: fileURL.path, contents: nil, attributes: nil)
        print("File created :", fileURL)
    } catch {
        print("Failed to save text to file:", error)
    }
}
// Backup the old file
do {
    try fileManager.copyItem(at: fileURL.appendingPathComponent("savedText.rtfd"), to: fileURL.appendingPathComponent("savedText.rtfd.old"))
} catch {
    print("Error copying file: \(error)")
}

// Save the file
let image = NSImage(named: "MyImage")
let attachment = NSTextAttachment()
attachment.image = image

let attributedString = NSMutableAttributedString(attributedString: textView.attributedString())
let attachmentString = NSAttributedString(attachment: attachment)
attributedString.append(attachmentString)

let fileWrapper = FileWrapper(directoryWithFileWrappers: [:])
let rtfData = attributedString.rtf(from: NSRange(location: 0, length: attributedString.length))
if let rtfData = rtfData {
    fileWrapper.addRegularFile(withContents: rtfData, preferredFilename: "savedText.rtfd")
}

do {
    try fileWrapper.write(to: fileURL, options: .atomic, originalContentsURL: nil)
} catch {
    print("Faild to save the file : \(error)")
}
HangarRash
  • 7,314
  • 5
  • 5
  • 32
  • Does this answer your question? [How can I save the attributed string (text) into file (swift, cocoa)?](https://stackoverflow.com/questions/33178895/how-can-i-save-the-attributed-string-text-into-file-swift-cocoa) – Willeke Dec 31 '22 at 20:35
  • Yes it did help a bit but now I have an other problem, when I save the rftd fiplewrapper to a file it create a folder with the name savedText.rftd with the content. But I don't know how to set the savedText.rftd to be a file with the content and not a folder – Nilon123456789 Dec 31 '22 at 22:09
  • Have you tried rtfd instead of rftd? – Willeke Dec 31 '22 at 23:34
  • No I didn't. thank you ! – Nilon123456789 Jan 01 '23 at 00:21

1 Answers1

0

Here is how I finally made it :

I used from the NSTextView.attributedString() the function (rtfdFileWrapper(from: NSRange)) to extract the rich text and images as a rtfd File Wrapper. After you have to save it with the write function of the FileWrapper

let fileURL = savePath.appendingPathComponent(fileName)

// Save the file

let attributedString = textView.attributedString()
let rtfdData = attributedString.rtfdFileWrapper(from: NSRange(location: 0, length: attributedString.length))
if let rtfdData = rtfdData {
    rtfdData.filename = "savedText.rtfd"
    do {
        try rtfdData.write(to: fileURL, options: .atomic, originalContentsURL: savePath)
    } catch {
        print("Faild to write \(error)")
    }
}