[Solved] Java: Replace string in hidden document

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
wanglong
Posts: 51
Joined: Fri Sep 09, 2022 10:57 am
Location: Beijing,China

[Solved] Java: Replace string in hidden document

Post by wanglong »

I use Java to control the execution of the following script to replace a string in the selected area. When the document is opened in hidden mode, the replacement cannot be successful, but it works fine when opened explicitly.

Code: Select all

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


sub FindReplaceStringInUpLines(n as Integer,find as String,replace as String)
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(1) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Count"
args1(0).Value = n
args1(1).Name = "Select"
args1(1).Value = true

dispatcher.executeDispatch(document, ".uno:GoUp", "", 0, args1())

rem ----------------------------------------------------------------------
dim args6(4) as new com.sun.star.beans.PropertyValue
args6(0).Name = "SearchItem.SearchFlags"
args6(0).Value = 71680
args6(1).Name = "SearchItem.SearchString"
args6(1).Value = find
args6(2).Name = "SearchItem.ReplaceString"
args6(2).Value = replace
args6(3).Name = "SearchItem.Command"
args6(3).Value = 3
args6(4).Name = "Quiet"
args6(4).Value = false

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args6())

end sub
My JAVA method,

Code: Select all

public void findReplaceStringInUpLines(XTextDocument xTextDocument,XTextViewCursor xTextViewCursor,XParagraphCursor xParagraphCursor,String find,String replace,int N){
        xTextViewCursor.gotoRange(xParagraphCursor.getEnd(), false);  
        TextDocument.executeBasicScript(xTextDocument,"Standard.Module2.FindReplaceStringInUpLines",new Object[]{N,find,replace});
}
        
public static void executeBasicScript(XTextDocument xTextDocument,String scriptPath,Object[] args) {
        XScriptProviderSupplier xScriptProviderSupplier = UnoRuntime.queryInterface(XScriptProviderSupplier.class, xTextDocument);
        XScriptProvider xScriptProvider = xScriptProviderSupplier.getScriptProvider();

        try {
            XScript macro = xScriptProvider.getScript("vnd.sun.star.script:" + scriptPath + "?language=Basic&location=application");

            macro.invoke(args,new short[1][1],new Object[1][1]);
        } catch (
                ScriptFrameworkErrorException | WrappedTargetException e) {
            e.printStackTrace();
        }
}      

Title Edited. A descriptive title for posts helps others who are searching for solutions and increases the chances of a reply (Hagar, Moderator).
Was: Documents opened in hidden mode do not support the execution of UNO Basic scripts, do they?
Last edited by MrProgrammer on Wed Feb 28, 2024 7:06 pm, edited 1 time in total.
Libre Office 7.6 on Windows 11.
JeJe
Volunteer
Posts: 2931
Joined: Wed Mar 09, 2016 2:40 pm

Re: Java running UNO Basic script with documents in hidden mode

Post by JeJe »

That's the way it is. Instead of dispatch calls use the API.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
ms777
Volunteer
Posts: 197
Joined: Mon Oct 08, 2007 1:33 am

Re: Java running UNO Basic script with documents in hidden mode

Post by ms777 »

... just to be a bit more precise: Hidden documents do support the execution Basic scripts, but not dispatch calls
wanglong
Posts: 51
Joined: Fri Sep 09, 2022 10:57 am
Location: Beijing,China

Re: Java running UNO Basic script with documents in hidden mode

Post by wanglong »

But dispatch call has different result with Basic,like what I'm discussing in viewtopic.php?t=111143.
Libre Office 7.6 on Windows 11.
Post Reply