[Solved] “Search and replace style” macro

Creating a macro - Writing a Script - Using the API

[Solved] “Search and replace style” macro

Postby Irvine_Himself » Wed Apr 29, 2015 6:04 am

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
Irvine_Himself
 
Posts: 9
Joined: Wed Apr 29, 2015 5:23 am

Re: “search and replace style” macro

Postby FJCC » Wed Apr 29, 2015 6:30 am

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   Expand viewCollapse view
  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.
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7190
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: “search and replace style” macro

Postby RoryOF » Wed Apr 29, 2015 7:27 am

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.6 on Xubuntu 18.04.2 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 29043
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: “search and replace style” macro

Postby Irvine_Himself » Wed Apr 29, 2015 5:56 pm

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   Expand viewCollapse view
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
Irvine_Himself
 
Posts: 9
Joined: Wed Apr 29, 2015 5:23 am

Re: “search and replace style” macro

Postby RoryOF » Wed Apr 29, 2015 6:00 pm

oStoryBook is moving towards tighter integration with LibreOffice/OpenOffice.
Apache OpenOffice 4.1.6 on Xubuntu 18.04.2 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 29043
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

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

Postby Irvine_Himself » Wed Apr 29, 2015 6:21 pm

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
Irvine_Himself
 
Posts: 9
Joined: Wed Apr 29, 2015 5:23 am

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

Postby RoryOF » Wed Apr 29, 2015 6:32 pm

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.6 on Xubuntu 18.04.2 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 29043
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 4 guests