Skip to content

Convert Text Files to Different Formats

OS X has a built-in terminal command, textutil, that can be used to convert text files to different formats. You can access it from a Shell to use in your Xojo apps.

With textutil, you can convert from or to these formats: txt, html, rtf, rtfd, doc, docx, wordml, odt and webarchive.

Its syntax is pretty simple:

textutil -convert fmt filename

So if you had a Word document and you wanted to convert it to html to load into a Xojo HTMLViewer, you would use a command like this:

textutil -convert html "Example.doc"

The resulting html file has the same name, so it would be called “Example.html”.

This Xojo code prompts you for a file and converts it to html:

Dim file As FolderItemfile = GetOpenFolderItem("")

If file <> Nil Then
  Dim command As String
  command = "textutil -convert html " + file.ShellPath
  Dim s As New Shell
  s.Execute(command)

  Dim htmlFile As FolderItem
  Dim name As String
  name = file.Name.Replace(".doc", ".html")

  htmlFile = file.Parent.Child(name)

  HTMLViewer1.LoadPage(htmlFile)
End If

I hope you find this little trick useful . Please share it with others.