From 9688e4970eed2b4bb4265df2a5781452a8ef5c52 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Ledure Date: Sun, 30 Jan 2022 17:19:31 +0100 Subject: ScriptForge - (SF_Session, SF_Document) add arguments to RunCommand() Next wiki page https://wiki.documentfoundation.org/Development/DispatchCommands facilitates the use of commands with the DispatchHelper service, including with arguments This commit - adds the RunCommand() method in the Session service - extends the scope of existing RunCommand() methods for the Document, Base, Calc and Writer services with the addition of command arguments Syntax by examples: (Basic) session.RunCommand("BasicIDEAppear", _ "Document", "LibreOffice Macros & Dialogs", _ "LibName", "ScriptForge", _ "Name", "SF_Session", _ "Line", 100) (Python) (1) session.RunCommand("BasicIDEAppear", "Document", "LibreOffice Macros & Dialogs", "LibName", "ScriptForge", "Name", "SF_Session", "Line", 100) (Python) (2) session.RunCommand("BasicIDEAppear", Document = "LibreOffice Macros & Dialogs", LibName = "ScriptForge", Name = "SF_Session", Line = 100) Change-Id: I60cd5eb1ed0a057420215ce7aebe92b2aafabd53 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129175 Tested-by: Jean-Pierre Ledure Tested-by: Jenkins Reviewed-by: Jean-Pierre Ledure --- wizards/source/sfdocuments/SF_Document.xba | 43 ++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 8 deletions(-) (limited to 'wizards/source/sfdocuments/SF_Document.xba') diff --git a/wizards/source/sfdocuments/SF_Document.xba b/wizards/source/sfdocuments/SF_Document.xba index 37c4e4e6bbe7..9f95470d2903 100644 --- a/wizards/source/sfdocuments/SF_Document.xba +++ b/wizards/source/sfdocuments/SF_Document.xba @@ -813,40 +813,67 @@ Catch: End Function ' SFDocuments.SF_Document.RemoveMenu REM ----------------------------------------------------------------------------- -Public Sub RunCommand(Optional ByVal Command As Variant) -''' Run on the document the given menu command. The command is executed without arguments +Public Sub RunCommand(Optional ByVal Command As Variant _ + , ParamArray Args As Variant _ + ) +''' Run on the current document window the given menu command. The command is executed with or without arguments ''' A few typical commands: ''' Save, SaveAs, ExportToPDF, SetDocumentProperties, Undo, Copy, Paste, ... -''' Dozens can be found in the directory $install/share/config/soffice.cfg/modules +''' Dozens can be found on next page: https://wiki.documentfoundation.org/Development/DispatchCommands ''' Args: ''' Command: Case-sensitive. The command itself is not checked. +''' If the command does not contain the ".uno:" prefix, it is added. ''' If nothing happens, then the command is probably wrong +''' Args: Pairs of arguments name (string), value (any) ''' Returns: ''' Examples: -''' oDoc.RunCommand("About") +''' oDoc.RunCommand("EditDoc", "Editable", False) ' Toggle edit mode +Dim vArgs As Variant ' Alias of Args Dim oDispatch ' com.sun.star.frame.DispatchHelper +Dim vProps As Variant ' Array of PropertyValues +Dim vValue As Variant ' A single value argument +Dim sCommand As String ' Alias of Command +Dim i As Long +Const cstPrefix = ".uno:" + Const cstThisSub = "SFDocuments.Document.RunCommand" -Const cstSubArgs = "Command" +Const cstSubArgs = "Command, [arg0Name, arg0Value], [arg1Name, arg1Value], ..." - If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch + If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch Check: + ' When called from a subclass (Calc, Writer, ..) the arguments are gathered into one single array item + vArgs = Args + If IsArray(Args) Then + If UBound(Args) >= 0 And IsArray(Args(0)) Then vArgs = Args(0) + End If If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then If Not _IsStillAlive() Then GoTo Finally If Not ScriptForge.SF_Utils._Validate(Command, "Command", V_STRING) Then GoTo Finally + If Not ScriptForge.SF_Utils._ValidateArray(vArgs, "Args", 1) Then GoTo Finally + For i = 0 To UBound(vArgs) - 1 Step 2 + If Not ScriptForge.SF_Utils._Validate(vArgs(i), "Arg" & CStr(i/2) & "Name", V_STRING) Then GoTo Finally + Next i End If Try: + ' Build array of property values + vProps = Array() + For i = 0 To UBound(vArgs) - 1 Step 2 + If IsEmpty(vArgs(i + 1)) Then vValue = Null Else vValue = vArgs(i + 1) + vProps = ScriptForge.SF_Array.Append(vProps, ScriptForge.SF_Utils._MakePropertyValue(vArgs(i), vValue)) + Next i Set oDispatch = ScriptForge.SF_Utils._GetUNOService("DispatchHelper") - oDispatch.executeDispatch(_Frame, ".uno:" & Command, "", 0, Array()) + If ScriptForge.SF_String.StartsWith(Command, cstPrefix) Then sCommand = Command Else sCommand = cstPrefix & Command + oDispatch.executeDispatch(_Frame, sCommand, "", 0, vProps) Finally: ScriptForge.SF_Utils._ExitFunction(cstThisSub) Exit Sub Catch: GoTo Finally -End Sub ' SFDocuments.SF_Document.RunCommand +End Sub ' SFDocuments.SF_Document.RunCommand REM ----------------------------------------------------------------------------- Public Function Save() As Boolean -- cgit