[Solved] “Search and replace style” macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Irvine_Himself
Posts: 9
Joined: Wed Apr 29, 2015 5:23 am

[Solved] “Search and replace style” macro

Post by Irvine_Himself »

I am trying to get to grips with some of the more esoteric aspects of OpenOffice by developing a collection of templates linked by a master document to provide an authoring environment similar to yWriter or Scrivener. Things are going fairly well: Using custom styles, I have created Indexes and ToCs in the master document to give a surprisingly detailed graphical overviews of various aspects of a project. My problem at the moment is, I would like to develop a set of macros to apply custom character styles, for example “Major Character”, “Minor Character”, “Location”..... etc

Using the "record Macro" function, I have a rough outline of a “search and replace style” macro which I think could be modified to use regular expressions, but I am not sure how to access the documents predefined custom character styles from the macro.

Any guidance in the form of good tutorial links, examples or general advice, would be appreciated.

Irvine
Last edited by Irvine_Himself on Wed Apr 29, 2015 6:03 pm, edited 1 time in total.
OpenOffice 4.0.1 on Windows Xp
FJCC
Moderator
Posts: 9280
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: “search and replace style” macro

Post by FJCC »

I'm not sure exactly what you need but here is some initial information. You can get an array of the existing character styles with

Code: Select all

  oStyleFamilies = ThisComponent.getStyleFamilies()
  oCharStyles = oStyleFamilies.getByName("CharacterStyles")
  oElementNames = oCharStyles.getElementNames()
Also, a text range that has a single, non-default, character style has a non-void CharStyleName property.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
RoryOF
Moderator
Posts: 34618
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: “search and replace style” macro

Post by RoryOF »

I am aware that at least two cross-platform opensource author's organisers exist with most of the functionality of Scrivener and yWriter : one is Plume Creator at
Plume Creator
which seems stalled.
The other is oStoryBook at
oStoryBook
which is under active development.

You might find it of use to investigate these.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Irvine_Himself
Posts: 9
Joined: Wed Apr 29, 2015 5:23 am

Re: “search and replace style” macro

Post by Irvine_Himself »

Thanks, that helped a lot.

Since it contains some very useful ideas that others may find useful, I am posting the finished Macro.

As you can probably tell, I am neither familiar with the “Basic” programming language, nor with the “Uno” frame work, and, since my original post this morning, my idea of how the macro should work has evolved considerably.

Originally, I was thinking in terms of how I would do it manually, and this lead me to think in terms of several unique macros for “Characters”, “Locations”, “Weapons” … etc. After quite a few wrong turns, I arrived at the elegant solution below. (I know I'm being immodest, but you should have seen the complexity some of my earlier solutions to copying the clipboard and getting the applied style!)

In essence, a user applies a custom character style to the selected text, and the Macro then applies the same style to all similar texts in the document. It still needs a way of handling the exception when the selection does not have a custom character style, but apart from that, it works well.

Code: Select all

Sub Update_Indexing_Styles
Rem searches for all occurrences of selected text and applies the custom character style of the original selection

rem define variables
dim document   as object
dim dispatcher as object

rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

Dim clipboard as String
oSelection = ThisComponent.CurrentSelection
oStyles = oSelection.getByIndex(0)
clipboard = oStyles.getstring          Rem much simpler than using xTransfer to copy clipboard
charStyle = oStyles.CharStyleName      Rem many simialr variables avaible

RDescrip = ThisComponent.createReplaceDescriptor
RDescrip.searchString = clipboard
target = ThisComponent.findAll(RDescrip)
for i = 0 to target.count-1
   foundText = target.getByIndex(i)
   foundText.CharStyleName = charStyle
next i

Rem ----- References, a lot of good information in these links ------
Rem https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=76871&p=350439#p350439
Rem https://forum.openoffice.org/en/forum/viewtopic.php?f=7&t=67889#p302409
Rem https://forum.openoffice.org/en/forum/viewtopic.php?f=45&t=44358
Rem http://www.pitonyak.org/AndrewMacro.pdf
End Sub
With regard to “cross-platform open source author's organisers”: Although it's not open source, since its free, I do use yWriter, but miss the functionality of OpenOffice.
:ugeek:
Thanks again

Irvine
OpenOffice 4.0.1 on Windows Xp
User avatar
RoryOF
Moderator
Posts: 34618
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: “search and replace style” macro

Post by RoryOF »

oStoryBook is moving towards tighter integration with LibreOffice/OpenOffice.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Irvine_Himself
Posts: 9
Joined: Wed Apr 29, 2015 5:23 am

Re: [Solved] “search and replace style” macro

Post by Irvine_Himself »

The problem with oStoryBook is that it requires Java 8 and I am using windows Xp. I notice that since I last tried to install Java 8, there has been an update to fix the instalation issues, but everything else being equal, I am learning an awful lot about OpenOffice that I never previously understood.

Irvine
OpenOffice 4.0.1 on Windows Xp
User avatar
RoryOF
Moderator
Posts: 34618
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Solved] “search and replace style” macro

Post by RoryOF »

oStoryBook v4.9.16 will run with Java 7 and the links are still on the Files page. I have v4.9.17 compiling on Netbeans 8.0.2 and it ought be possible to recompile for Java 7 if one needed that, although tweaking the code might be required.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Post Reply