Editing Markdown or txt files in OpenOffice
By andre
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:
Sub OpenTextFile(FileName as String, Optional Lang as String)
Dim docURL As String
Dim document as object
Dim aProps(1) As New com.sun.star.beans.PropertyValue
Dim dispatcher as object
aProps(0).Name = "FilterName"
aProps(0).Value = "ASCII Filter"
aProps(1).Name = "FilterOptions"
aProps(1).Value = "UTF8"
docURL = ConvertToURL(FileName)
document = StarDesktop.loadComponentFromURL(docURL, "_blank", 0, aProps)
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
Dim args4(0) as new com.sun.star.beans.PropertyValue
If Not IsMissing(Lang) Then
args4(0).Name = "Language"
args4(0).Value = "Current_" + Lang
document = ThisComponent.CurrentController.Frame
dispatcher.executeDispatch(document, ".uno:SelectAll", "", 0, Array())
dispatcher.executeDispatch(document, ".uno:LanguageStatus", "", 0, args4())
End If
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:
start "Office" /B "%PROGRAMFILES%\OpenOffice.org 3\program\swriter.exe" "macro:///Standard.Module1.OpenTextFile(\"%1%\")"
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):
start "Office" /B "%PROGRAMFILES%\OpenOffice.org 3\program\swriter.exe" "macro:///Standard.Module1.OpenTextFile(\"%1%\",\"Spanish\")"
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.