Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a lot (>10k ... this can't be done manually) of rtf and rtfd files that I would like to turn into txt files. Unfortunately they contain pictures and hyperlinks that I don't want to lose. Ideally, I'd like to:

  1. have hyperlinks from the rtf/rtfd stored as plain text (something like [[description]link])

  2. have all pictures "separated" from the rtfd file

  3. have the new txt with URLs as well as the pictures from the rtf/rtfd file saved together in a folder named after the old rtf/rtfd file.

How can this be done?

share|improve this question

1 Answer 1

.rtfd are just bundles of a TXT.rtf file with non-standard formatting instructions, and the images. So you already have these and the RTF file separate.

RTF files are just the text with some formatting instructions, similar to HTML. So you can use simple text based string replacements.

An embedded image looks like this in my sample document:

{{\NeXTGraphic Screen Shot 2012-02-01 at 19.47.21.png \width13940 \height11020
}¬}

In the RTF file, change all occurrences of \NeXTGraphic to something like "Image:"; the other formatting will simply be ignored when you open it the next time (at least in my limited testing).

An embedded hyperlink looks like this:

{\field{\*\fldinst{HYPERLINK "http://superuser.com/questions/384862/osx-converting-rtfd-files-to-txt-without-deleting-hyperlinks-and-pictures"}}{\fldrslt applescript - OSX: Converting rtfd files to txt, without deleting hyperlinks and pictures - Super User}}\
\
}

Again, rather simple text replacement which you can do in most editors of your choice, or e.g. using sed on the command line. fldinst is the underlying hyperlink URL, fldrslt is the label.


Once you have replaced all images and hyperlinks by plain-text compatible alternatives, you can simply run textutil -convert txt Document.rtfd/TXT.rtf to convert the RTF file to plain text.

If you hadn't done the replacements of the embedded imaged and hyperlinks beforehand, these would have been lost.


Of course, textutil can convert to HTML and image files without losing information. If you're more comfortable scripting the replacements of HTML tags, you can do this first.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.