Find and Replace Macro Guidence

Hello all;

I am looking for some guidance with macros a la Find/Replace and have no idea where to begin. Here is what I am looking to do:

I’ve just finished my second book, and I am now down to the nitty gritty of editing, things like homophones, quotation formatting, n/m dash formatting, double spaces, and all that goodly annoying stuff. For the last book I used Find/Replace and some RegEx to achieve this. The process was a follows, over and over again:

  • Put the cursor at the beginning
  • Set up the find
  • Increment though each match and check it. It might be right, it might be wrong, especially with homophones :grinning:
  • Reach the end and start over with the next search criteria. I have about fifteen or so of these criteria and each chapter gets the same treatment.

The laborious part of this is resetting the find dialog each time. It just gets old :upside_down_face:

So… I was wondering if macro’s would help the issue. All I want to do is have the macro put the cursor at character one of the document, then open up the Find/Replace dialog with a predefined criteria. From there I would take over, press find next, and do whatever needs doing.

In my head there would be a macro for each of my criteria. In short, I want Libre office to keep a library of named searches for me so that I could run down the list with all 45 chapters. This would speed things up considerably.

In my day job I do a lot of scripting, so the concept is not alien to me. However, I have no idea how, or where, to begin. Ideally I would like to manually set up the find, then snapshot it somehow. Barring that I need to find out how to define the criteria programmatically. I guess what I looking for is a sign post of sorts. Where do I begin? There would be a syntax involved here. Where do I find that syntax - not examples of it mind you, but the whole ball of wax, at least for cursor portion and find/replace invocation and setup?

Any guidance would be appreciated.

Thanks in advance.

Regards;
Ken

PS, some side notes:

I know language tool can do part of what I am looking for. However, homophone checking appears to be an online/ AI function. Now, I don’t mind AI. it has its uses, and this is one of them. However, i am allergic to public AI. I simply do not trust the things, or moreover the people behind them. Call me cynical, but I’m going to leave the bear alone for now. :wink:

Also, you’ll likely want to know about the computer and build, so:

System:

  • Kernel: 6.8.0-136-generic arch: x86_64 bits: 64 compiler: gcc v: 13.3.0 clocksource: tsc
  • Desktop: Cinnamon v: 6.4.8 tk: GTK v: 3.24.41 wm: Muffin v: 6.4.1 vt: 7 dm: LightDM v: 1.30.0
  • Distro: Linux Mint 22.1 Xia base: Ubuntu 24.04 noble

Libre Office

  • Version: 24.2.7.2 (X86_64) / LibreOffice Community
  • Build ID: 420(Build:2)
  • CPU threads: 12; OS: Linux 6.8; UI render: default; VCL: gtk3
  • Locale: en-US (en_US.UTF-8); UI: en-US
  • Ubuntu package version: 4:24.2.7-0ubuntu0.24.04.6
  • Calc: threaded

Use the macro recorder.

Probably AltSearch.oxt can be helpful for you. It has an in-built batch mode which can include more than one proceedings of search&replace.
The range can be edited easily.


altsearch_main_en__ScaleMaxHeightWzMwMF0


The offline help ? includes a useful chapter for batch mode.


Please note: At the moment version 1.5.3.0 is the latest one.

1 Like

The typical start for “the whole ball” is the book “Macros explained” by Andrew Pitonyak:
https://www.pitonyak.org/oo.php
.
Like @Villeroy suggested, my first macros for LO were recorded ones.

also online : HelpAltSearch_xx.htm-> EN, ES, FR, IT, CS, DE, NL, RU

Thank you all!

I poured myself a cup of coffee this morning and dove head first to this. Between Google/Gemini (the close by not quite AI bot :slightly_smiling_face:), a dash of Stack Exchange, and some good old fashioned guess work I got about 50% of the way there.

This what I came up with. It ploped the cursor where I want it, and invoked the F/R dialog:

REM  *****  BASIC  *****



Sub OpenFindAndReplace

    If  ThisComponent.SupportsService("com.sun.star.text.TextDocument") Then
        ThisComponent.getCurrentController().getViewCursor().gotoStart(False)
    EndIf 

	Dim doc As Object
    Dim document As Object
    Dim dispatcher As Object

    doc = ThisComponent
    document   = doc.CurrentController.Frame
    dispatcher = CreateUnoService("com.sun.star.frame.DispatchHelper")
    
    ' Execute the built-in SearchDialog command
    dispatcher.executeDispatch(document, ".uno:SearchDialog", "", 0, Array())
 
  
End Sub

It works like a charm and also gave me a basic idea of what is what. However, the next step gave me fits. If I understand it correctly, you can define named pair parameters in Array(), which should (in theory) control the settings of the F/S dialog. In my head that should be simple, but try as my head might, no dice. It very much wants to present you with the last used parameters, which is exactly what I am trying to defeat.

To compound matters, Gemini said the following:

In LibreOffice, the built-in graphical Find & Replace dialog cannot be opened prefilled via a standard macro because the native GUI doesn’t expose an API to inject initial text into its inputs. Instead, you can either perform the search and replace programmatically via a macro silently, or write a macro that mimics a step-by-step interactive search.

Now, huge gain of salt, I know. This the same thing that thought glue on pizza sounded good. :laughing: However, I am (at this point) inclined to lend it some credence. The idea makes sense and matches my experience thus far. So, my next question is: IS this true? If it is, what I am up to isn’t possible the way I envision it. That doesn’t mean I am dead in the water, but I will need a new approach.

@Grantler: I took a look at Alt F/R. It’s close, and I may end up using it if I can not crack this nut, but I want to see how far I get first.

@ Villeroy: I tried the recorder, but did not work per se. I’m still tinkering with it and it might a bit more sense now that I have a feel for the syntax.

@ Wanderer: I found that this morning, and bookmarked it. I see some reading in my future!

Thank you all for the input.
Ken

With Python, it’s easy to move the cursor to the beginning of “the entire” document.

import uno

def main():

    doc = XSCRIPTCONTEXT.getDocument()
    cursor = doc.Text.createTextCursor()
    cursor.gotoStart(False)

    return

If you give me an example of a search, I’ll expand on the example to show you how to do it.

Sure:

Like said in my update about, the idea is simple:

  1. Put the cursor at the start of the doc
  2. Open up the F/R dialog
  3. Set up the parameters as needed (Search text, Regular expression on/off, Whole words, etc.)
  4. Then STOP

I don’t want it to do the search, just set up the search. The nature of what I am doing is not a bulk replace situation. It is a “did I get it right” thing. Think ‘there’ when I mean ‘their’. That kind of stuff. I will have to evaluate each one every time.

After this morning, I am not sure this is directly possible. The F/S dialog seems (maybe) to not accept incoming settings favoring the last user entered settings.

Anyway, that is the idea. Thank for your help, truly!

Ken