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
4 comments
Jean-Francois Arseneault
2008-01-28T13:43:40ZInvariably, as I read of your hacks, I can't help but think of this strip...
Adam Spiers
2008-02-27T11:24:08ZThat's great! I need something very similar but for manipulating OO.o Draw objects. Where did you find out about the Basic API calls?
Sacha Chua
2008-02-28T13:03:11ZHere are my bookmarks for openoffice.org. =)
Adam Spiers
2008-02-28T16:09:45ZGreat, thanks a lot!