Editing Markdown or txt files in OpenOffice

I have been introduced to Markdown format some time ago and today I find it extremely convenient for most of the writing I need to do. Markdown is essentially "markup without markup" - it requires you to do very little in order to format your text and it can be easily converted in a number of other formats, such as PDF, Word, ODT, LaTeX, HTML, Docbook etc. From experience I found that the most convenient environment to edit Markdown files is OpenOffice (or LibreOffice). Not only is it free and open source, it is actually quite convenient. You don't need much to get started - just install the latest version of OpenOffice and open your .markdown document in it.

The only problem I have had with it was the "ASCII filter options" dialogue, which OpenOffice shows when I opened a Markdown, or any other text file in it. Fortunately, you can avoid the extra clicks involved in dealing with it by putting together a bit of OpenOffice macro code and some shell scripting.

First, create the following macro in your OpenOffice - make sure it is global and doesn't belong to a specific document:

  1. Sub OpenTextFile(FileName as String, Optional Lang as String)
  3. Dim docURL As String
  4. Dim document   as object
  5. Dim aProps(1) As New com.sun.star.beans.PropertyValue 
  6. Dim dispatcher as object
  8. aProps(0).Name = "FilterName"
  9. aProps(0).Value = "ASCII Filter"
  10. aProps(1).Name = "FilterOptions"
  11. aProps(1).Value = "UTF8"
  13. docURL = ConvertToURL(FileName) 
  14. document = StarDesktop.loadComponentFromURL(docURL, "_blank", 0, aProps) 
  16. dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
  18. Dim args4(0) as new com.sun.star.beans.PropertyValue
  19. If Not IsMissing(Lang) Then
  20. 	args4(0).Name = "Language"
  21. 	args4(0).Value = "Current_" + Lang
  22. 	document = ThisComponent.CurrentController.Frame
  23. 	dispatcher.executeDispatch(document, ".uno:SelectAll", "", 0, Array())
  24. 	dispatcher.executeDispatch(document, ".uno:LanguageStatus", "", 0, args4())
  25. End If
  27. End Sub

This macro allows you to open your file for editing without any dialogues, allowing you to supply an optional language for spelling. If no language is provided, the default for your instance of OpenOffice will be used. It also assumes, that the file is encoded in UTF-8. Feel free to modify the script to suit your own needs.

Next, create a shell script to invoke this macro from command line. The general approach is to do the following:

swriter.exe 'macro:///Standard.Module1.OpenTextFile("<file-name>")'

where Standard.Module1 is the module in which you have saved the script. On Windows you can create the following simple script:

  1. start "Office" /B "%PROGRAMFILES%\OpenOffice.org 3\program\swriter.exe" "macro:///Standard.Module1.OpenTextFile(\"%1%\")"
  2. exit

Or, to open the file for editing in Spanish, use the following (note the lack of white spaces between the parameters and double quotes everywhere - it doesn't seem to work otherwise):

  1. start "Office" /B "%PROGRAMFILES%\OpenOffice.org 3\program\swriter.exe" "macro:///Standard.Module1.OpenTextFile(\"%1%\",\"Spanish\")"
  2. exit

Invoking OpenOffice via "Start" will prevent a terminal window from remaining in the background.

Save it as OpenMarkdownEn.cmd or OpenMarkdownEs.cmd and then instruct Windows Shell via Explorer to use it to open your Markdown file. If you have a consistent extension for all your Markdown files, i.e. .markdown, use the "Open with..." entry in the right-click context menu to associate the extension with the script - this way you will save yourself a lot of mouse clicks for all future openings of Markdown files.


Add new comment

Subscribe to Comments for "Editing Markdown or txt files in OpenOffice"