Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.4 Convert document formats on the command line UNIX
Tiger only hintThere's a new command line (unix) utility in OS X 10.4 called textutil which will convert document formats known to the Cocoa layer. According to the man page (man textutil), the formats supported are: txt, html, rtf, rtfd, doc, wordml, or webarchive. Most usefully, you can convert rtf, doc and html documents to text format for further processing with other command line tools.

The basic syntax is:
textutil -convert fmt filename
Where fmt is one of the formats above, and filename is the name of the file you wish to convert. The man page lists many other useful options, including -info to display information about a file. This displays the type, size, length (in characters), and an abstract from the contents of the text file.

You can also use textutil to change text encodings on text and html files (eg to/from UTF-8, iso8859-1, etc).
    •    
  • Currently 3.71 / 5
  You rated: 2 / 5 (7 votes cast)
 
[32,270 views]  

10.4 Convert document formats on the command line | 7 comments | Create New Account
Click here to return to the '10.4 Convert document formats on the command line' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4 Convert document formats on the command line
Authored by: adrianm on Jul 05, '05 12:26:12PM

How handy.
Was wondering if the text services had been exposed in the cmdline. Cheers!



[ Reply to This | # ]
10.4 Convert document formats on the command line
Authored by: seb2 on Jul 05, '05 08:12:51PM
Sounds good. But
/usr/bin/textutil -convert txt -encoding NSUnicodeStringEncoding -inputencoding NSISOLatin1StringEncoding <fileName>
results in "Invalid output encoding." -- How come?

[ Reply to This | # ]
10.4 Convert document formats on the command line
Authored by: adriaant on Jul 06, '05 07:24:21AM

Use utf-16 as the parameter for -encoding



[ Reply to This | # ]
10.4 Convert document formats on the command line
Authored by: lucite on Jul 07, '05 10:28:23PM

OMFG - how cool is that.
Thx OP - you've just saved me hours of stupid labor.

N.B. The HTML generated is 4.01, but I'll live.



[ Reply to This | # ]
10.4 Convert document formats on the command line
Authored by: chacher on Mar 05, '08 05:30:44AM

can someone please tell me how to output the converted files to a new/different directory?
thanks

OR

point me towards a good into to Terminal – I've been putting off learning it, but after this tip, I'm READY



[ Reply to This | # ]
10.4 Convert document formats on the command line
Authored by: morespace54 on Mar 05, '08 10:46:07AM

That would be:

textutil -convert fmt filename -output path/to/newfile.fmt


So let's say you have a document named "MySource" in a RTF format (in your current directory) and you want to save the result as a file named "Result" in a TXT format (in your Documents directory), you would do the following:

textutil -convert txt MySource.rtf -output ~/Documents/Result.txt



[ Reply to This | # ]
10.4 Convert document formats on the command line
Authored by: peterpayne on Oct 27, '09 12:21:15AM

In true Unix spirit, it would of course be considered "n00b" to actually give a complete list of all the encodings that we might want to use. Yes, I am reading the man page.

*sigh*



[ Reply to This | # ]