[Solved] Writer Macro Search&Replace with Text Formating

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
svenb
Posts: 5
Joined: Fri Mar 30, 2018 7:19 pm

[Solved] Writer Macro Search&Replace with Text Formating

Post by svenb »

Hi together,

coming from MS Word I am still at very first step of learning macros.

I would like to do to following:
Search for RegExp1 RegExp2 RegExp3 and so on...
Replace with RegExp1 (Format1) RegExp2 (Format2) and so on...

Format would be things like Fontcolor, Fontsize, Searched Text+Tabluature etc.

I know I can this all manualy with search&replace. The built in Macrorecorder doesn record the formatings, only the search criteria.

Thanks for your help!
Last edited by Hagar Delest on Tue Apr 03, 2018 9:48 pm, edited 1 time in total.
Reason: tagged [Solved].
OpenOffice 3.1 on Windows Vista
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Writer Macro Search&Replace with Text Formating

Post by Zizi64 »

The built in Macrorecorder doesn record the formatings,
You need WRITE your macros in the AOO and LO based on the functions of the API - if you want work efficiently -, because the Macro Recorder has a very limited capability.

API: Application Programming Interface - it is basicly different from the MS VBA.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
svenb
Posts: 5
Joined: Fri Mar 30, 2018 7:19 pm

Re: Writer Macro Search&Replace with Text Formating

Post by svenb »

I know. But in Word I learned a lot out of their macro recorder. For example the names for the variables and so on. With this in mind I can start programming. But in AOO and LO I have no real idea. Even in the forums I didn´t find what I was searching for.

How to manipulate the formating of the replacementstring? In the example from the basic guide they only mentioned "Bold". But how to change the color and the fontsize?
OpenOffice 3.1 on Windows Vista
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Writer Macro Search&Replace with Text Formating

Post by RoryOF »

As Zizi64 says, it is all in the API documentation! Possibly you will find as much as you need in the OO BASIC documentation (shorter than API documention) The documentation BASIC is linked off
https://wiki.openoffice.org/wiki/Docume ... ASIC_Guide
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Writer Macro Search&Replace with Text Formating

Post by Zizi64 »

I know. But in Word I learned a lot out of their macro recorder. For example the names for the variables and so on. With this in mind I can start programming.
It will not be enough for the programming with the API functions.

Read and study the Andrew Pitonyak's free macro programming books. The sample codes in the books was written in the StarBasic programming language. You can use the IDE of the AOO/LO to try the samples. (You can use all of supported programming languages for the API functions, but it is more difficult for a beginner.)

http://www.pitonyak.org/oo.php
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Writer Macro Search&Replace with Text Formating

Post by RoryOF »

If this is a one-off task, multiple careful Find and Replace operations will be very much quicker than writing a complex macro.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
svenb
Posts: 5
Joined: Fri Mar 30, 2018 7:19 pm

Re: Writer Macro Search&Replace with Text Formating

Post by svenb »

Oh No, it is for a conversion from another software which I have to do regular. Without a script I will take too much time. I know how to write a script which will find the right strings, but I have no idea (no working idea) how to change the format of the replacestring... I guess it is as simple as in word, but what's the right codeline for charColor and Charlize?
OpenOffice 3.1 on Windows Vista
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Writer Macro Search&Replace with Text Formating

Post by Zizi64 »

Do you want to apply some direct formatting properties by a macro code? It is not a good idea. The most valuable feature of the AOO/LO are the STYLES. The editing / modifying the format of a document become more efficient by the usage of the styles. Use them manually and/or with your macros.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Writer Macro Search&Replace with Text Formating

Post by RoryOF »

It would help if you showed us a few examples of formatted Find strings, with their formatted Replace strings.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
svenb
Posts: 5
Joined: Fri Mar 30, 2018 7:19 pm

Re: Writer Macro Search&Replace with Text Formating

Post by svenb »

The goal is getting the text from the clipboard (wich is nothing more than a ass-file) to readable format. Readable in the meaning, that nonsense stuff is getting a white color and sometime is getting a smale size. In my MS Word worklfow this is optimal because the only thing which is "new" are tabs and linebreaks. A second script can remove them so that the file can be reimportet to a ass-software.

Here a three lines out of the ass:

Dialogue: 0,0:02:31.42,0:02:36.68,Default,Hugh,0,0,0,,10 Tote seit 2005 und im letzten Jahr 5 Tote.
Dialogue: 0,0:02:38.90,0:02:57.28,Default,,0,0,0,,Eindeutiger kann ein Trend kaum sein. Kein Wunder, dass die Anwohner immer besorgter werden. Und wer je einen Hai leibhaftig gesehen hat, der traut sich erst recht nicht mehr ins Wasser.
Dialogue: 0,0:02:58.08,0:03:00.64,Default,Matt,0,0,0,,Ich fuhr dort rüber und bin da ins Wasser.

"Dialogue: 0," >> color white, size2
"0:02:31" >> will stay as it is
".42," >>all of the: "\...\," >> getting white, size2
Next timecode: "0:02:36" >> getting grey, half of the normal size
"Default," >> getting white, size2 PLUS Tabulator
"Hugh" >> if there is no text than insert "Kommentar" blod >> if text is there than the text bold, cursiv
",0,0,0,," >> getting white, size2 PLUS Linebreak (no Paragraph!)


At the End all strings "\N" are getting Linebreaks
Than the script is inserting some lines before the text and doing a bit of layoutwork.

As you see with this kind of conversion I am not losing any information. I can redo everything quit easily. Another script is deleting the lines and remove the tabs and new linebreaks. When coping that to the ass-software it doesn´t matter what fontsize or whatever kind of format is used.

Maybe you see now why I need a easy way for formating the ReplaceString. Getting these thins searched and replaced isn´t a problem ... but how to apply the format?
Something like this in Word:
With Selection.Find.Replacement.Font
.Size = 6
.Color = wdColorGray25
End With
OpenOffice 3.1 on Windows Vista
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Writer Macro Search&Replace with Text Formating

Post by Zizi64 »

Please read the books written by A. Pitonyak, and study Pitonyak's post in this topic:
viewtopic.php?f=20&t=2794
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Writer Macro Search&Replace with Text Formating

Post by RoryOF »

I think the OP should refer to section 14.2 et seq, in Andrew Pitonyak' OpenOffice Macros Explained, as pointed to by Zizi64.

The sample data supplied, which would have been better supplied as an actual uploaded file, suggests to me that it is comma separated values (.CSV format) - I have a memory that recently a poster confirmed that an .ass file was .CSV in some other language , although I cannot now find that reference. If this is a CSV file I would suggest loading it to Calc, then simply formatting each column in that Calc spreadsheet to have the required character formats.

But, if there is an existing method of doing the task, using another software, why not use it? Why change? What is important - getting the task done, or saving a few $s? By all means save the few $s, if that is your desire, using your time, but it is unfair to consume time from volunteers on the forum expecting them to do your thinking and your research.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Writer Macro Search&Replace with Text Formating

Post by Zizi64 »

Yes, a sample file (or two with the "before" and "after" state) would be the best explanation of the task.

(A CSV file can not contain any formatting property.)
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: Writer Macro Search&Replace with Text Formating

Post by Sébastien C »

Hello to all.

If I may... If the text of A. Pitonyak is, necessarily in English, a reference, it remains that to give an example to our friend Svenb on what he asks precisely does not seem to me superfluous...
I do not have enough English to really produce this code completely since I do not understand all the details you ask.
But I think it can be useful to distinguish two forms of search/replace using macros.
Of course, you have to know the regular expressions whose help for LibreOffice is available here.
The attached file contains a character style named “littleWhite”. It is actually gray to be able to be visualized
The two macros that are inserted there may be able to start by giving you practical examples before going further in what you project.
It is maybe a good thing to display the mark of paragraph “¶” and the linebreak “↲” just for know what chr(10) is.

Code: Select all

' ╔══════════════════════════════════════════════════════════════════════════════╗
' ║ Two ways for search / remplace.                                              ║█
' ╚══════════════════════════════════════════════════════════════════════════════╝█
'  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀

Sub myReplaceAll()
 Dim          myReplace As Object
 Dim mySearchRep(10, 1) As String
 Dim                  i As Integer
 
 '  Here the list of your  search/replace.
  mySearchRep( 0, 0) = "Default,"  : mySearchRep( 0, 1) = "$0\t"
  mySearchRep( 1, 0) = "$"         : mySearchRep( 1, 1) = chr(10)
 'mySearchRep( 2, 0) =             : mySearchRep( 2, 1) = 
 'mySearchRep( 3, 0) =             : mySearchRep( 3, 1) = 
 'mySearchRep( 4, 0) =             : mySearchRep( 4, 1) = 
 'mySearchRep( 5, 0) =             : mySearchRep( 5, 1) = 
 'mySearchRep( 6, 0) =             : mySearchRep( 6, 1) = 
 'mySearchRep( 7, 0) =             : mySearchRep( 7, 1) = 
 'mySearchRep( 8, 0) =             : mySearchRep( 8, 1) = 
 'mySearchRep( 9, 0) =             : mySearchRep( 9, 1) = 
 'mySearchRep(10, 0) =             : mySearchRep(10, 1) = 

 myReplace = thisComponent.createReplaceDescriptor()

 For i = 0 to 1                                         ' O to 1 or a lot off more...
  With myReplace
   .SearchCaseSensitive     = True
   .SearchRegularExpression = True
   .SearchString            = mySearchRep(i, 0)
   .ReplaceString           = mySearchRep(i, 1)
  End With

  thisComponent.replaceAll ( myReplace() )
 Next i
End Sub


Sub applyStyle()
 Dim mySearch As Object, myResult As Object, myElement As Object
 Dim i as integer

 mySearch = thisComponent.createSearchDescriptor()

 mySearch.searchString            = "Default,"          ' The string that you search.
 mySearch.searchRegularExpression = false

 myResult = thisComponent.findAll(mySearch)
   
 If myResult.hasElements Then
  For i = 0 to myResult.count-1
   myElement               = myResult.getByIndex(i)
   myElement.CharStyleName = "littleWhite"              ' The style that you apply to your string.
  Next i
 End If
End Sub
With hope of helping.
:D
Attachments
dialogue.odt
(12.13 KiB) Downloaded 186 times
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: Writer Macro Search&Replace with Text Formating

Post by Sébastien C »

An add to my precedent post.

Inspired by my own code snippet, I produced the linked file too... It is just a merge with the code above and an adaptation of my code snippet. I think if you understand my code above, you will understand the use of this Calc file. So I find it a little funny and useful maybe not just for you...

The text file do not need a macro anymore but of course still one or more named personal styles.

If you open the file “dialogue.odtafter the Calc file "batchSearchReplaceWithStyle.ods”, you have, of course, to click on the “Refresh the list of text files (Writer)” button.

Enjoy
:mrgreen:
Attachments
batchSearchReplaceWithStyle.ods
Useful WITHOUT programming
(16.16 KiB) Downloaded 208 times
dialogue.odt
An example right now WITHOUT macro.
(10.33 KiB) Downloaded 184 times
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
svenb
Posts: 5
Joined: Fri Mar 30, 2018 7:19 pm

Re: Writer Macro Search&Replace with Text Formating

Post by svenb »

Thanks Sebastien, with this codelines I got it. I just made a simple version, breaking up your array into many replace and find instructions. As there doesn´t change anything, it´s no big thing. I have created new character styles to get it done your way. Works fine! When I will have some more time, I will try to solve it the way you did - using an array of string. That look where nice and will produce less codelines.

Thanks a lot Sebastien!
OpenOffice 3.1 on Windows Vista
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: Writer Macro Search&Replace with Text Formating

Post by Sébastien C »

You are welcome!

And for those who do not know what is an array of strings, there is still my addition in the form of a Calc’s file, that just allows to study the help of LibreOffice regular expressions. I still find extraordinary to be able to use a spreadsheet as a huge dialog box...

See you !
:D
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
Post Reply