Yak shaving with OpenOffice.org

Fiddly little editing tasks are driving me mad. Formatting all the keyboard shortcuts as EmphasisBold and the command names as Literal… I figured that if I could write a program that got 80% of the cases right, that would not only save me time but also stress. So I spent two hours shaving the yak today, learning just enough OpenOffice.org Basic to put together two procedures that attempt to handle most of the cases. FormatCommands looks for anything within parentheses inside a body paragraph, and FormatKeyboardShortcut looks for things like C-c r t (remember, tasks). I don’t expect this to be usable to anyone, but if it can be a useful jumping-off-point for other people, that would be awesome.

Sub FormatCommands
   oDoc = ThisComponent
   Descriptor = oDoc.createSearchDescriptor()
   Descriptor.SearchRegularExpression = true
   Descriptor.SearchString = "\([^)]+\)"
   Found = oDoc.FindFirst(Descriptor)
   do while not isNull(Found)
       oNew = oDoc.Text.createTextCursorByRange(Found.Start)
       if oNew.CharStyleName = "" and (oNew.ParaStyleName = "Body" or oNew.ParaStyleName = "BodyFirst") then
             oNew.goRight(1, False)
             oNew.goRight(Len(Found.String) - 2, True)
             oNew.CharStyleName = "Literal"
       end if 
     Found = oDoc.findNext(Found.End, Descriptor)
   Loop   
End Sub
Sub FormatKeyboardShortcuts
   oDoc = ThisComponent
   Descriptor = oDoc.createSearchDescriptor()
   Descriptor.SearchRegularExpression = true
   Descriptor.SearchCaseSensitive = true
   Descriptor.SearchString = "([CMS]-)+([a-zA-Z0-9]+)*( [a-zA-Z0-9])* "
   Found = oDoc.FindFirst(Descriptor)
   do while not isNull(Found)
           if Found.CharStyleName = "" and (Found.ParaStyleName = "Body" or Found.ParaStyleName = "BodyFirst") then
                 Found.CharStyleName = "EmphasisBold"
           end if 
     Found = oDoc.findNext(Found.End, Descriptor)
   Loop   
End Sub
You can comment with Disqus or you can e-mail me at sacha@sachachua.com.