nss: upgrade to release 3.73
[LibreOffice.git] / wizards / source / scriptforge / SF_Session.xba
bloba41bffa513773d702eae10ae07627ed71449abb0
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <script:module xmlns:script="http://openoffice.org/2000/script" script:name="SF_Session" script:language="StarBasic" script:moduleType="normal">REM =======================================================================================================================
4 REM === The ScriptForge library and its associated libraries are part of the LibreOffice project. ===
5 REM === Full documentation is available on https://help.libreoffice.org/ ===
6 REM =======================================================================================================================
8 Option Compatible
9 Option Explicit
11 &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
12 &apos;&apos;&apos; SF_Session
13 &apos;&apos;&apos; ==========
14 &apos;&apos;&apos; Singleton class implementing the &quot;ScriptForge.Session&quot; service
15 &apos;&apos;&apos; Implemented as a usual Basic module
16 &apos;&apos;&apos;
17 &apos;&apos;&apos; Gathers diverse general-purpose properties and methods about :
18 &apos;&apos;&apos; - installation/execution environment
19 &apos;&apos;&apos; - UNO introspection utilities
20 &apos;&apos;&apos; - clipboard management
21 &apos;&apos;&apos; - invocation of external scripts or programs
22 &apos;&apos;&apos;
23 &apos;&apos;&apos; Service invocation example:
24 &apos;&apos;&apos; Dim session As Variant
25 &apos;&apos;&apos; session = CreateScriptService(&quot;Session&quot;)
26 &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
28 REM ================================================================== EXCEPTIONS
30 Const CALCFUNCERROR = &quot;CALCFUNCERROR&quot; &apos; Calc function execution failed
31 Const NOSCRIPTERROR = &quot;NOSCRIPTERROR&quot; &apos; Script could not be located
32 Const SCRIPTEXECERROR = &quot;SCRIPTEXECERROR&quot; &apos; Exception during script execution
33 Const WRONGEMAILERROR = &quot;WRONGEMAILERROR&quot; &apos; Wrong email address
34 Const SENDMAILERROR = &quot;SENDMAILERROR&quot; &apos; Mail could not be sent
35 Const UNKNOWNFILEERROR = &quot;UNKNOWNFILEERROR&quot; &apos; Source file does not exist
37 REM ============================================================ MODULE CONSTANTS
39 &apos;&apos;&apos; Script locations
40 &apos;&apos;&apos; ================
41 &apos;&apos;&apos; Use next constants as Scope argument when invoking next methods:
42 &apos;&apos;&apos; ExecuteBasicScript()
43 &apos;&apos;&apos; ExecutePythonScript()
44 &apos;&apos;&apos; Example:
45 &apos;&apos;&apos; session.ExecuteBasicScript(session.SCRIPTISEMBEDDED, &quot;Standard.myLib.myFunc&quot;, etc)
47 Const cstSCRIPTISEMBEDDED = &quot;document&quot; &apos; a library of the document (BASIC + PYTHON)
48 Const cstSCRIPTISAPPLICATION = &quot;application&quot; &apos; a shared library (BASIC)
49 Const cstSCRIPTISPERSONAL = &quot;user&quot; &apos; a library of My Macros (PYTHON)
50 Const cstSCRIPTISPERSOXT = &quot;user:uno_packages&quot; &apos; an extension for the current user (PYTHON)
51 Const cstSCRIPTISSHARED = &quot;share&quot; &apos; a library of LibreOffice Macros (PYTHON)
52 Const cstSCRIPTISSHAROXT = &quot;share:uno_packages&quot; &apos; an extension for all users (PYTHON)
53 Const cstSCRIPTISOXT = &quot;uno_packages&quot; &apos; an extension but install params are unknown (PYTHON)
55 REM ===================================================== CONSTRUCTOR/DESTRUCTOR
57 REM -----------------------------------------------------------------------------
58 Public Function Dispose() As Variant
59 Set Dispose = Nothing
60 End Function &apos; ScriptForge.SF_Array Explicit destructor
62 REM ================================================================== PROPERTIES
64 REM -----------------------------------------------------------------------------
65 Property Get ObjectType As String
66 &apos;&apos;&apos; Only to enable object representation
67 ObjectType = &quot;SF_Session&quot;
68 End Property &apos; ScriptForge.SF_Session.ObjectType
70 REM -----------------------------------------------------------------------------
71 Property Get ServiceName As String
72 &apos;&apos;&apos; Internal use
73 ServiceName = &quot;ScriptForge.Session&quot;
74 End Property &apos; ScriptForge.SF_Array.ServiceName
76 REM -----------------------------------------------------------------------------
77 Property Get SCRIPTISAPPLICATION As String
78 &apos;&apos;&apos; Convenient constants
79 SCRIPTISAPPLICATION = cstSCRIPTISAPPLICATION
80 End Property &apos; ScriptForge.SF_Session.SCRIPTISAPPLICATION
82 REM -----------------------------------------------------------------------------
83 Property Get SCRIPTISEMBEDDED As String
84 &apos;&apos;&apos; Convenient constants
85 SCRIPTISEMBEDDED = cstSCRIPTISEMBEDDED
86 End Property &apos; ScriptForge.SF_Session.SCRIPTISEMBEDDED
88 REM -----------------------------------------------------------------------------
89 Property Get SCRIPTISOXT As String
90 &apos;&apos;&apos; Convenient constants
91 SCRIPTISOXT = cstSCRIPTISOXT
92 End Property &apos; ScriptForge.SF_Session.SCRIPTISOXT
94 REM -----------------------------------------------------------------------------
95 Property Get SCRIPTISPERSONAL As String
96 &apos;&apos;&apos; Convenient constants
97 SCRIPTISPERSONAL = cstSCRIPTISPERSONAL
98 End Property &apos; ScriptForge.SF_Session.SCRIPTISPERSONAL
100 REM -----------------------------------------------------------------------------
101 Property Get SCRIPTISPERSOXT As String
102 &apos;&apos;&apos; Convenient constants
103 SCRIPTISPERSOXT = cstSCRIPTISPERSOXT
104 End Property &apos; ScriptForge.SF_Session.SCRIPTISPERSOXT
106 REM -----------------------------------------------------------------------------
107 Property Get SCRIPTISSHARED As String
108 &apos;&apos;&apos; Convenient constants
109 SCRIPTISSHARED = cstSCRIPTISSHARED
110 End Property &apos; ScriptForge.SF_Session.SCRIPTISSHARED
112 REM -----------------------------------------------------------------------------
113 Property Get SCRIPTISSHAROXT As String
114 &apos;&apos;&apos; Convenient constants
115 SCRIPTISSHAROXT = cstSCRIPTISSHAROXT
116 End Property &apos; ScriptForge.SF_Session.SCRIPTISSHAROXT
118 REM ============================================================== PUBLIC METHODS
120 REM -----------------------------------------------------------------------------
121 Public Function ExecuteBasicScript(Optional ByVal Scope As Variant _
122 , Optional ByVal Script As Variant _
123 , ParamArray pvArgs As Variant _
124 ) As Variant
125 &apos;&apos;&apos; Execute the Basic script given as a string and return the value returned by the script
126 &apos;&apos;&apos; Args:
127 &apos;&apos;&apos; Scope: &quot;Application&quot; (default) or &quot;Document&quot; (NOT case-sensitive)
128 &apos;&apos;&apos; (or use one of the SCRIPTIS... public constants above)
129 &apos;&apos;&apos; Script: library.module.method (Case sensitive)
130 &apos;&apos;&apos; library =&gt; The library may be not loaded yet
131 &apos;&apos;&apos; module =&gt; Must not be a class module
132 &apos;&apos;&apos; method =&gt; Sub or Function
133 &apos;&apos;&apos; Read https://wiki.openoffice.org/wiki/Documentation/DevGuide/Scripting/Scripting_Framework_URI_Specification
134 &apos;&apos;&apos; pvArgs: the arguments of the called script
135 &apos;&apos;&apos; Returns:
136 &apos;&apos;&apos; The value returned by the call to the script
137 &apos;&apos;&apos; Exceptions:
138 &apos;&apos;&apos; NOSCRIPTERROR The script could not be found
139 &apos;&apos;&apos; Examples:
140 &apos;&apos;&apos; session.ExecuteBasicScript(, &quot;XrayTool._Main.Xray&quot;, someuno) &apos; Sub: no return expected
142 Dim oScript As Object &apos; Script to be invoked
143 Dim vReturn As Variant &apos; Returned value
145 Const cstThisSub = &quot;Session.ExecuteBasicScript&quot;
146 Const cstSubArgs = &quot;[Scope], Script, arg0[, arg1] ...&quot;
148 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
150 vReturn = Empty
152 Check:
153 If IsMissing(Scope) Or IsEmpty(Scope) Then Scope = SCRIPTISAPPLICATION
154 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
155 If Not SF_Utils._Validate(Scope, &quot;Scope&quot;, V_STRING _
156 , Array(SCRIPTISAPPLICATION, SCRIPTISEMBEDDED)) Then GoTo Finally
157 If Not SF_Utils._Validate(Script, &quot;Script&quot;, V_STRING) Then GoTo Finally
158 End If
160 Try:
161 &apos; Execute script
162 Set oScript = SF_Session._GetScript(&quot;Basic&quot;, Scope, Script)
163 On Local Error GoTo CatchExec
164 If Not IsNull(oScript) Then vReturn = oScript.Invoke(pvArgs(), Array(), Array())
166 Finally:
167 ExecuteBasicScript = vReturn
168 SF_Utils._ExitFunction(cstThisSub)
169 Exit Function
170 Catch:
171 GoTo Finally
172 CatchExec:
173 SF_Exception.RaiseFatal(SCRIPTEXECERROR, &quot;Script&quot;, Script, Error$)
174 GoTo Finally
175 End Function &apos; ScriptForge.SF_Session.ExecuteBasicScript
177 REM -----------------------------------------------------------------------------
178 Public Function ExecuteCalcFunction(Optional ByVal CalcFunction As Variant _
179 , ParamArray pvArgs As Variant _
180 ) As Variant
181 &apos;&apos;&apos; Execute a Calc function by its (english) name and based on the given arguments
182 &apos;&apos;&apos; Args:
183 &apos;&apos;&apos; CalcFunction: the english name of the function to execute
184 &apos;&apos;&apos; pvArgs: the arguments of the called function
185 &apos;&apos;&apos; Each argument must be either a string, a numeric value
186 &apos;&apos;&apos; or an array of arrays combining those types
187 &apos;&apos;&apos; Returns:
188 &apos;&apos;&apos; The (string or numeric) value or the array of arrays returned by the call to the function
189 &apos;&apos;&apos; When the arguments contain arrays, the function is executed as an array function
190 &apos;&apos;&apos; Wrong arguments generate an error
191 &apos;&apos;&apos; Exceptions:
192 &apos;&apos;&apos; CALCFUNCERROR &apos; Execution error in calc function
193 &apos;&apos;&apos; Examples:
194 &apos;&apos;&apos; session.ExecuteCalcFunction(&quot;AVERAGE&quot;, 1, 5, 3, 7) returns 4
195 &apos;&apos;&apos; session.ExecuteCalcFunction(&quot;ABS&quot;, Array(Array(-1,2,3),Array(4,-5,6),Array(7,8,-9)))(2)(2) returns 9
196 &apos;&apos;&apos; session.ExecuteCalcFunction(&quot;LN&quot;, -3) generates an error
198 Dim oCalc As Object &apos; Give access to the com.sun.star.sheet.FunctionAccess service
199 Dim vReturn As Variant &apos; Returned value
200 Const cstThisSub = &quot;Session.ExecuteCalcFunction&quot;
201 Const cstSubArgs = &quot;CalcFunction, arg0[, arg1] ...&quot;
203 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
204 vReturn = Empty
206 Check:
207 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
208 If Not SF_Utils._Validate(CalcFunction, &quot;CalcFunction&quot;, V_STRING) Then GoTo Finally
209 End If
211 Try:
212 &apos; Execute function
213 Set oCalc = SF_Utils._GetUNOService(&quot;FunctionAccess&quot;)
214 On Local Error GoTo CatchCall
215 vReturn = oCalc.callFunction(UCase(CalcFunction), pvArgs())
217 Finally:
218 ExecuteCalcFunction = vReturn
219 SF_Utils._ExitFunction(cstThisSub)
220 Exit Function
221 Catch:
222 GoTo Finally
223 CatchCall:
224 SF_Exception.RaiseFatal(CALCFUNCERROR, CalcFunction)
225 GoTo Finally
226 End Function &apos; ScriptForge.SF_Session.ExecuteCalcFunction
228 REM -----------------------------------------------------------------------------
229 Public Function ExecutePythonScript(Optional ByVal Scope As Variant _
230 , Optional ByVal Script As Variant _
231 , ParamArray pvArgs As Variant _
232 ) As Variant
233 &apos;&apos;&apos; Execute the Python script given as a string and return the value returned by the script
234 &apos;&apos;&apos; Args:
235 &apos;&apos;&apos; Scope: one of the SCRIPTIS... public constants above (default = &quot;share&quot;)
236 &apos;&apos;&apos; Script: (Case sensitive)
237 &apos;&apos;&apos; &quot;library/module.py$method&quot;
238 &apos;&apos;&apos; or &quot;module.py$method&quot;
239 &apos;&apos;&apos; or &quot;myExtension.oxt|myScript|module.py$method&quot;
240 &apos;&apos;&apos; library =&gt; The library may be not loaded yet
241 &apos;&apos;&apos; myScript =&gt; The directory containing the python module
242 &apos;&apos;&apos; module.py =&gt; The python module
243 &apos;&apos;&apos; method =&gt; The python function
244 &apos;&apos;&apos; Read https://wiki.openoffice.org/wiki/Documentation/DevGuide/Scripting/Scripting_Framework_URI_Specification
245 &apos;&apos;&apos; pvArgs: the arguments of the called script
246 &apos;&apos;&apos; Date arguments are converted to iso format. However dates in arrays are not converted
247 &apos;&apos;&apos; Returns:
248 &apos;&apos;&apos; The value(s) returned by the call to the script. If &gt;1 values, enclosed in an array
249 &apos;&apos;&apos; Exceptions:
250 &apos;&apos;&apos; NOSCRIPTERROR The script could not be found
251 &apos;&apos;&apos; Examples:
252 &apos;&apos;&apos; session.ExecutePythonScript(session.SCRIPTISSHARED, &quot;Capitalise.py$getNewString&quot;, &quot;Abc&quot;) returns &quot;abc&quot;
254 Dim oScript As Object &apos; Script to be invoked
255 Dim vArg As Variant &apos; Individual argument
256 Dim vReturn As Variant &apos; Returned value
257 Dim i As Long
259 Const cstThisSub = &quot;Session.ExecutePythonScript&quot;
260 Const cstSubArgs = &quot;[Scope], Script, arg0[, arg1] ...&quot;
262 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
264 vReturn = Empty
266 Check:
267 If IsError(Scope) Or IsMissing(Scope) Then Scope = SCRIPTISSHARED
268 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
269 If Not SF_Utils._Validate(Scope, &quot;Scope&quot;, V_STRING _
270 , Array(SCRIPTISSHARED, SCRIPTISEMBEDDED, SCRIPTISPERSONAL, SCRIPTISSHAROXT, SCRIPTISPERSOXT, SCRIPTISOXT) _
271 ) Then GoTo Finally
272 If Not SF_Utils._Validate(Script, &quot;Script&quot;, V_STRING) Then GoTo Finally
273 End If
275 Try:
276 &apos; Filter date arguments - NB: dates in arrays are not filtered
277 For i = 0 To UBound(pvArgs) &apos; pvArgs always zero-based
278 vArg = pvArgs(i)
279 If VarType(vArg) = V_DATE Then pvArgs(i) = SF_Utils._CDateToIso(vArg)
280 Next i
282 &apos; Find script
283 Set oScript = SF_Session._GetScript(&quot;Python&quot;, Scope, Script)
285 &apos; Execute script
286 If Not IsNull(oScript) Then
287 vReturn = oScript.Invoke(pvArgs(), Array(), Array())
288 &apos; Remove surrounding array when single returned value
289 If IsArray(vReturn) Then
290 If UBound(vReturn) = 0 Then vReturn = vReturn(0)
291 End If
292 End If
294 Finally:
295 ExecutePythonScript = vReturn
296 SF_Utils._ExitFunction(cstThisSub)
297 Exit Function
298 Catch:
299 GoTo Finally
300 End Function &apos; ScriptForge.SF_Session.ExecutePythonScript
302 REM -----------------------------------------------------------------------------
303 Public Function GetProperty(Optional ByVal PropertyName As Variant) As Variant
304 &apos;&apos;&apos; Return the actual value of the given property
305 &apos;&apos;&apos; Args:
306 &apos;&apos;&apos; PropertyName: the name of the property as a string
307 &apos;&apos;&apos; Returns:
308 &apos;&apos;&apos; The actual value of the property
309 &apos;&apos;&apos; Exceptions
310 &apos;&apos;&apos; ARGUMENTERROR The property does not exist
312 Const cstThisSub = &quot;Session.GetProperty&quot;
313 Const cstSubArgs = &quot;PropertyName&quot;
315 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
316 GetProperty = Null
318 Check:
319 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
320 If Not SF_Utils._Validate(PropertyName, &quot;PropertyName&quot;, V_STRING, Properties()) Then GoTo Catch
321 End If
323 Try:
324 Select Case UCase(PropertyName)
325 Case Else
326 End Select
328 Finally:
329 SF_Utils._ExitFunction(cstThisSub)
330 Exit Function
331 Catch:
332 GoTo Finally
333 End Function &apos; ScriptForge.SF_Session.GetProperty
335 REM -----------------------------------------------------------------------------
336 Public Function HasUnoMethod(Optional ByRef UnoObject As Variant _
337 , Optional ByVal MethodName As Variant _
338 ) As Boolean
339 &apos;&apos;&apos; Returns True if a UNO object contains the given method
340 &apos;&apos;&apos; Code-snippet derived from XRAY
341 &apos;&apos;&apos; Args:
342 &apos;&apos;&apos; UnoObject: the object to identify
343 &apos;&apos;&apos; MethodName: the name of the method as a string. The search is case-sensitive
344 &apos;&apos;&apos; Returns:
345 &apos;&apos;&apos; False when the method is not found or when an argument is invalid
347 Dim oIntrospect As Object &apos; com.sun.star.beans.Introspection
348 Dim oInspect As Object &apos; com.sun.star.beans.XIntrospectionAccess
349 Dim bMethod As Boolean &apos; Return value
350 Const cstThisSub = &quot;Session.HasUnoMethod&quot;
351 Const cstSubArgs = &quot;UnoObject, MethodName&quot;
353 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
355 Check:
356 bMethod = False
357 If VarType(UnoObject) &lt;&gt; V_OBJECT Then GoTo Finally
358 If IsNull(UnoObject) Then GoTo Finally
359 If VarType(MethodName) &lt;&gt; V_STRING Then GoTo Finally
360 If MethodName = Space(Len(MethodName)) Then GoTo Finally
362 Try:
363 On Local Error GoTo Catch
364 Set oIntrospect = SF_Utils._GetUNOService(&quot;Introspection&quot;)
365 Set oInspect = oIntrospect.inspect(UnoObject)
366 bMethod = oInspect.hasMethod(MethodName, com.sun.star.beans.MethodConcept.ALL)
368 Finally:
369 HasUnoMethod = bMethod
370 SF_Utils._ExitFunction(cstThisSub)
371 Exit Function
372 Catch:
373 On Local Error GoTo 0
374 GoTo Finally
375 End Function &apos; ScriptForge.SF_Session.HasUnoMethod
377 REM -----------------------------------------------------------------------------
378 Public Function HasUnoProperty(Optional ByRef UnoObject As Variant _
379 , Optional ByVal PropertyName As Variant _
380 ) As Boolean
381 &apos;&apos;&apos; Returns True if a UNO object contains the given property
382 &apos;&apos;&apos; Code-snippet derived from XRAY
383 &apos;&apos;&apos; Args:
384 &apos;&apos;&apos; UnoObject: the object to identify
385 &apos;&apos;&apos; PropertyName: the name of the property as a string. The search is case-sensitive
386 &apos;&apos;&apos; Returns:
387 &apos;&apos;&apos; False when the property is not found or when an argument is invalid
389 Dim oIntrospect As Object &apos; com.sun.star.beans.Introspection
390 Dim oInspect As Object &apos; com.sun.star.beans.XIntrospectionAccess
391 Dim bProperty As Boolean &apos; Return value
392 Const cstThisSub = &quot;Session.HasUnoProperty&quot;
393 Const cstSubArgs = &quot;UnoObject, PropertyName&quot;
395 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
397 Check:
398 bProperty = False
399 If VarType(UnoObject) &lt;&gt; V_OBJECT Then GoTo Finally
400 If IsNull(UnoObject) Then GoTo Finally
401 If VarType(PropertyName) &lt;&gt; V_STRING Then GoTo Finally
402 If PropertyName = Space(Len(PropertyName)) Then GoTo Finally
404 Try:
405 On Local Error GoTo Catch
406 Set oIntrospect = SF_Utils._GetUNOService(&quot;Introspection&quot;)
407 Set oInspect = oIntrospect.inspect(UnoObject)
408 bProperty = oInspect.hasProperty(PropertyName, com.sun.star.beans.PropertyConcept.ALL)
410 Finally:
411 HasUnoProperty = bProperty
412 SF_Utils._ExitFunction(cstThisSub)
413 Exit Function
414 Catch:
415 On Local Error GoTo 0
416 GoTo Finally
417 End Function &apos; ScriptForge.SF_Session.HasUnoProperty
419 REM -----------------------------------------------------------------------------
420 Public Function Methods() As Variant
421 &apos;&apos;&apos; Return the list of public methods of the Session service as an array
423 Methods = Array( _
424 &quot;ExecuteBasicScript&quot; _
425 , &quot;ExecuteCalcFunction&quot; _
426 , &quot;ExecutePythonScript&quot; _
427 , &quot;HasUnoMethod&quot; _
428 , &quot;HasUnoProperty&quot; _
429 , &quot;OpenURLInBrowser&quot; _
430 , &quot;RunApplication&quot; _
431 , &quot;SendMail&quot; _
432 , &quot;UnoMethods&quot; _
433 , &quot;UnoObjectType&quot; _
434 , &quot;UnoProperties&quot; _
435 , &quot;WebService&quot; _
438 End Function &apos; ScriptForge.SF_Session.Methods
440 REM -----------------------------------------------------------------------------
441 Public Sub OpenURLInBrowser(Optional ByVal URL As Variant)
442 &apos;&apos;&apos; Opens a URL in the default browser
443 &apos;&apos;&apos; Args:
444 &apos;&apos;&apos; URL: The URL to open in the browser
445 &apos;&apos;&apos; Examples:
446 &apos;&apos;&apos; session.OpenURLInBrowser(&quot;https://docs.python.org/3/library/webbrowser.html&quot;)
448 Const cstPyHelper = &quot;$&quot; &amp; &quot;_SF_Session__OpenURLInBrowser&quot;
450 Const cstThisSub = &quot;Session.OpenURLInBrowser&quot;
451 Const cstSubArgs = &quot;URL&quot;
453 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
455 Check:
456 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
457 If Not SF_Utils._Validate(URL, &quot;URL&quot;, V_STRING) Then GoTo Finally
458 End If
460 Try:
461 ExecutePythonScript(SCRIPTISSHARED, _SF_.PythonHelper &amp; cstPyHelper, URL)
463 Finally:
464 SF_Utils._ExitFunction(cstThisSub)
465 Exit Sub
466 Catch:
467 GoTo Finally
468 End Sub &apos; ScriptForge.SF_Session.OpenURLInBrowser
470 REM -----------------------------------------------------------------------------
471 Public Function Properties() As Variant
472 &apos;&apos;&apos; Return the list or properties as an array
474 Properties = Array( _
477 End Function &apos; ScriptForge.SF_Session.Properties
479 REM -----------------------------------------------------------------------------
480 Public Function RunApplication(Optional ByVal Command As Variant _
481 , Optional ByVal Parameters As Variant _
482 ) As Boolean
483 &apos;&apos;&apos; Executes an arbitrary system command
484 &apos;&apos;&apos; Args:
485 &apos;&apos;&apos; Command: The command to execute
486 &apos;&apos;&apos; This may be an executable file or a document which is registered with an application
487 &apos;&apos;&apos; so that the system knows what application to launch for that document
488 &apos;&apos;&apos; Parameters: a list of space separated parameters as a single string
489 &apos;&apos;&apos; The method does not validate the given parameters, but only passes them to the specified command
490 &apos;&apos;&apos; Returns:
491 &apos;&apos;&apos; True if success
492 &apos;&apos;&apos; Examples:
493 &apos;&apos;&apos; session.RunApplication(&quot;Notepad.exe&quot;)
494 &apos;&apos;&apos; session.RunApplication(&quot;C:\myFolder\myDocument.odt&quot;)
495 &apos;&apos;&apos; session.RunApplication(&quot;kate&quot;, &quot;/home/me/install.txt&quot;) &apos; (Linux)
497 Dim bReturn As Boolean &apos; Returned value
498 Dim oShell As Object &apos; com.sun.star.system.SystemShellExecute
499 Dim sCommand As String &apos; Command as an URL
500 Const cstThisSub = &quot;Session.RunApplication&quot;
501 Const cstSubArgs = &quot;Command, [Parameters]&quot;
503 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
504 bReturn = False
506 Check:
507 If IsMissing(Parameters) Then Parameters = &quot;&quot;
508 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
509 If Not SF_Utils._ValidateFile(Command, &quot;Command&quot;) Then GoTo Finally
510 If Not SF_Utils._Validate(Parameters, &quot;Parameters&quot;, V_STRING) Then GoTo Finally
511 End If
513 Try:
514 Set oShell = SF_Utils._GetUNOService(&quot;SystemShellExecute&quot;)
515 sCommand = SF_FileSystem._ConvertToUrl(Command)
516 oShell.execute(sCommand, Parameters, com.sun.star.system.SystemShellExecuteFlags.DEFAULTS)
517 bReturn = True
519 Finally:
520 RunApplication = bReturn
521 SF_Utils._ExitFunction(cstThisSub)
522 Exit Function
523 Catch:
524 GoTo Finally
525 End Function &apos; ScriptForge.SF_Session.RunApplication
527 REM -----------------------------------------------------------------------------
528 Public Sub SendMail(Optional ByVal Recipient As Variant _
529 , Optional ByRef Cc As Variant _
530 , Optional ByRef Bcc As Variant _
531 , Optional ByVal Subject As Variant _
532 , Optional ByRef Body As Variant _
533 , Optional ByVal FileNames As Variant _
534 , Optional ByVal EditMessage As Variant _
536 &apos;&apos;&apos; Send a message (with or without attachments) to recipients from the user&apos;s mail client
537 &apos;&apos;&apos; The message may be edited by the user before sending or, alternatively, be sent immediately
538 &apos;&apos;&apos; Args:
539 &apos;&apos;&apos; Recipient: an email addresses (To recipient)
540 &apos;&apos;&apos; Cc: a comma-delimited list of email addresses (carbon copy)
541 &apos;&apos;&apos; Bcc: a comma-delimited list of email addresses (blind carbon copy)
542 &apos;&apos;&apos; Subject: the header of the message
543 &apos;&apos;&apos; FileNames: a comma-separated list of filenames to attach to the mail. SF_FileSystem naming conventions apply
544 &apos;&apos;&apos; Body: the unformatted text of the message
545 &apos;&apos;&apos; EditMessage: when True (default) the message is editable before being sent
546 &apos;&apos;&apos; Exceptions:
547 &apos;&apos;&apos; UNKNOWNFILEERROR File does not exist
548 &apos;&apos;&apos; WRONGEMAILERROR String not recognized as an email address
549 &apos;&apos;&apos; SENDMAILERROR System error, probably no mail client
551 Dim sEmail As String &apos; An single email address
552 Dim sFile As String &apos; A single file name
553 Dim sArg As String &apos; Argument name
554 Dim vCc As Variant &apos; Array alias of Cc
555 Dim vBcc As Variant &apos; Array alias of Bcc
556 Dim vFileNames As Variant &apos; Array alias of FileNames
557 Dim oMailService As Object &apos; com.sun.star.system.SimpleCommandMail or com.sun.star.system.SimpleSystemMail
558 Dim oMail As Object &apos; com.sun.star.system.XSimpleMailClient
559 Dim oMessage As Object &apos; com.sun.star.system.XSimpleMailMessage
560 Dim lFlag As Long &apos; com.sun.star.system.SimpleMailClientFlags.XXX
561 Dim ARR As Object : ARR = ScriptForge.SF_Array
562 Dim i As Long
563 Const cstComma = &quot;,&quot;, cstSemiColon = &quot;;&quot;
564 Const cstThisSub = &quot;Session.SendMail&quot;
565 Const cstSubArgs = &quot;Recipient, [Cc=&quot;&quot;&quot;&quot;], [Bcc=&quot;&quot;&quot;&quot;], [Subject=&quot;&quot;&quot;&quot;], [FileNames=&quot;&quot;&quot;&quot;], [Body=&quot;&quot;&quot;&quot;], [EditMessage=True]&quot;
567 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
569 Check:
570 If IsMissing(Cc) Or IsEmpty(Cc) Then Cc = &quot;&quot;
571 If IsMissing(Bcc) Or IsEmpty(Bcc) Then Bcc = &quot;&quot;
572 If IsMissing(Subject) Or IsEmpty(Subject) Then Subject = &quot;&quot;
573 If IsMissing(FileNames) Or IsEmpty(FileNames) Then FileNames = &quot;&quot;
574 If IsMissing(Body) Or IsEmpty(Body) Then Body = &quot;&quot;
575 If IsMissing(EditMessage) Or IsEmpty(EditMessage) Then EditMessage = True
577 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
578 If Not SF_Utils._Validate(Cc, &quot;Recipient&quot;, V_STRING) Then GoTo Finally
579 If Not SF_Utils._Validate(Cc, &quot;Cc&quot;, V_STRING) Then GoTo Finally
580 If Not SF_Utils._Validate(Bcc, &quot;Bcc&quot;, V_STRING) Then GoTo Finally
581 If Not SF_Utils._Validate(Subject, &quot;Subject&quot;, V_STRING) Then GoTo Finally
582 If Not SF_Utils._Validate(FileNames, &quot;FileNames&quot;, V_STRING) Then GoTo Finally
583 If Not SF_Utils._Validate(Body, &quot;Body&quot;, V_STRING) Then GoTo Finally
584 If Not SF_Utils._Validate(EditMessage, &quot;EditMessage&quot;, V_BOOLEAN) Then GoTo Finally
585 End If
587 &apos; Check email addresses
588 sArg = &quot;Recipient&quot; : sEmail = Recipient
589 If Not SF_String.IsEmail(sEmail) Then GoTo CatchEmail
590 sArg = &quot;Cc&quot; : vCc = ARR.TrimArray(Split(Cc, cstComma))
591 For Each sEmail In vCc
592 If Not SF_String.IsEmail(sEmail) Then GoTo CatchEmail
593 Next sEmail
594 sArg = &quot;Bcc&quot; : vBcc = ARR.TrimArray(Split(Bcc, cstComma))
595 For Each sEmail In vBcc
596 If Not SF_String.IsEmail(sEmail) Then GoTo CatchEmail
597 Next sEmail
599 &apos; Check file existence
600 If Len(FileNames) &gt; 0 Then
601 vFileNames = ARR.TrimArray(Split(FileNames, cstComma))
602 For i = 0 To UBound(vFileNames)
603 sFile = vFileNames(i)
604 If Not SF_Utils._ValidateFile(sFile, &quot;FileNames&quot;) Then GoTo Finally
605 If Not SF_FileSystem.FileExists(sFile) Then GoTo CatchNotExists
606 vFileNames(i) = ConvertToUrl(sFile)
607 Next i
608 End If
610 Try:
611 &apos; Initialize the mail service
612 Set oMailService = SF_Utils._GetUNOService(&quot;MailService&quot;)
613 If IsNull(oMailService) Then GoTo CatchMail
614 Set oMail = oMailService.querySimpleMailClient()
615 If IsNull(oMail) Then GoTo CatchMail
616 Set oMessage = oMail.createSimpleMailMessage()
617 If IsNull(oMessage) Then GoTo CatchMail
619 &apos; Feed the new mail message
620 With oMessage
621 .setRecipient(Recipient)
622 If Subject &lt;&gt; &quot;&quot; Then .setSubject(Subject)
623 If UBound(vCc) &gt;= 0 Then .setCcRecipient(vCc)
624 If UBound(vBcc) &gt;= 0 Then .setBccRecipient(vBcc)
625 .Body = Iif(Len(Body) = 0, &quot; &quot;, Body) &apos; Body must not be the empty string ??
626 .setAttachement(vFileNames)
627 End With
628 lFlag = Iif(EditMessage, com.sun.star.system.SimpleMailClientFlags.DEFAULTS, com.sun.star.system.SimpleMailClientFlags.NO_USER_INTERFACE)
630 &apos; Send using the mail service
631 oMail.sendSimpleMailMessage(oMessage, lFlag)
633 Finally:
634 SF_Utils._ExitFunction(cstThisSub)
635 Exit Sub
636 Catch:
637 GoTo Finally
638 CatchEmail:
639 SF_Exception.RaiseFatal(WRONGEMAILERROR, sArg, sEmail)
640 GoTo Finally
641 CatchNotExists:
642 SF_Exception.RaiseFatal(UNKNOWNFILEERROR, &quot;FileNames&quot;, sFile)
643 GoTo Finally
644 CatchMail:
645 SF_Exception.RaiseFatal(SENDMAILERROR)
646 GoTo Finally
647 End Sub &apos; ScriptForge.SF_Session.SendMail
649 REM -----------------------------------------------------------------------------
650 Public Function SetProperty(Optional ByVal PropertyName As Variant _
651 , Optional ByRef Value As Variant _
652 ) As Boolean
653 &apos;&apos;&apos; Set a new value to the given property
654 &apos;&apos;&apos; Args:
655 &apos;&apos;&apos; PropertyName: the name of the property as a string
656 &apos;&apos;&apos; Value: its new value
657 &apos;&apos;&apos; Exceptions
658 &apos;&apos;&apos; ARGUMENTERROR The property does not exist
660 Const cstThisSub = &quot;Session.SetProperty&quot;
661 Const cstSubArgs = &quot;PropertyName, Value&quot;
663 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
664 SetProperty = False
666 Check:
667 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
668 If Not SF_Utils._Validate(PropertyName, &quot;PropertyName&quot;, V_STRING, Properties()) Then GoTo Catch
669 End If
671 Try:
672 Select Case UCase(PropertyName)
673 Case Else
674 End Select
676 Finally:
677 SF_Utils._ExitFunction(cstThisSub)
678 Exit Function
679 Catch:
680 GoTo Finally
681 End Function &apos; ScriptForge.SF_Session.SetProperty
683 REM -----------------------------------------------------------------------------
684 Public Function UnoMethods(Optional ByRef UnoObject As Variant) As Variant
685 &apos;&apos;&apos; Returns a list of the methods callable from an UNO object
686 &apos;&apos;&apos; Code-snippet derived from XRAY
687 &apos;&apos;&apos; Args:
688 &apos;&apos;&apos; UnoObject: the object to identify
689 &apos;&apos;&apos; Returns:
690 &apos;&apos;&apos; A zero-based sorted array. May be empty
692 Dim oIntrospect As Object &apos; com.sun.star.beans.Introspection
693 Dim oInspect As Object &apos; com.sun.star.beans.XIntrospectionAccess
694 Dim vMethods As Variant &apos; Array of com.sun.star.reflection.XIdlMethod
695 Dim vMethod As Object &apos; com.sun.star.reflection.XIdlMethod
696 Dim lMax As Long &apos; UBounf of vMethods
697 Dim vMethodsList As Variant &apos; Return value
698 Dim i As Long
699 Const cstThisSub = &quot;Session.UnoMethods&quot;
700 Const cstSubArgs = &quot;UnoObject&quot;
702 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
704 Check:
705 vMethodsList = Array()
706 If VarType(UnoObject) &lt;&gt; V_OBJECT Then GoTo Finally
707 If IsNull(UnoObject) Then GoTo Finally
709 Try:
710 On Local Error GoTo Catch
711 Set oIntrospect = SF_Utils._GetUNOService(&quot;Introspection&quot;)
712 Set oInspect = oIntrospect.inspect(UnoObject)
713 vMethods = oInspect.getMethods(com.sun.star.beans.MethodConcept.ALL)
715 &apos; The names must be extracted from com.sun.star.reflection.XIdlMethod structures
716 lMax = UBound(vMethods)
717 If lMax &gt;= 0 Then
718 ReDim vMethodsList(0 To lMax)
719 For i = 0 To lMax
720 vMethodsList(i) = vMethods(i).Name
721 Next i
722 vMethodsList = SF_Array.Sort(vMethodsList, CaseSensitive := True)
723 End If
725 Finally:
726 UnoMethods = vMethodsList
727 SF_Utils._ExitFunction(cstThisSub)
728 Exit Function
729 Catch:
730 On Local Error GoTo 0
731 GoTo Finally
732 End Function &apos; ScriptForge.SF_Session.UnoMethods
734 REM -----------------------------------------------------------------------------
735 Public Function UnoObjectType(Optional ByRef UnoObject As Variant) As String
736 &apos;&apos;&apos; Identify the UNO type of an UNO object
737 &apos;&apos;&apos; Code-snippet derived from XRAY
738 &apos;&apos;&apos; Args:
739 &apos;&apos;&apos; UnoObject: the object to identify
740 &apos;&apos;&apos; Returns:
741 &apos;&apos;&apos; com.sun.star. ... as a string
742 &apos;&apos;&apos; a zero-length string if identification was not successful
744 Dim oService As Object &apos; com.sun.star.reflection.CoreReflection
745 Dim vClass as Variant &apos; com.sun.star.reflection.XIdlClass
746 Dim sObjectType As String &apos; Return value
747 Const cstThisSub = &quot;Session.UnoObjectType&quot;
748 Const cstSubArgs = &quot;UnoObject&quot;
750 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
752 Check:
753 sObjectType = &quot;&quot;
754 If VarType(UnoObject) &lt;&gt; V_OBJECT Then GoTo Finally
755 If IsNull(UnoObject) Then GoTo Finally
757 Try:
758 On Local Error Resume Next
759 &apos; Try usual ImplementationName method
760 sObjectType = UnoObject.getImplementationName()
761 If sObjectType = &quot;&quot; Then
762 &apos; Now try CoreReflection trick
763 Set oService = SF_Utils._GetUNOService(&quot;CoreReflection&quot;)
764 vClass = oService.getType(UnoObject)
765 If vClass.TypeClass &gt;= com.sun.star.uno.TypeClass.STRUCT Then sObjectType = vClass.Name
766 End If
768 Finally:
769 UnoObjectType = sObjectType
770 SF_Utils._ExitFunction(cstThisSub)
771 Exit Function
772 End Function &apos; ScriptForge.SF_Session.UnoObjectType
774 REM -----------------------------------------------------------------------------
775 Public Function UnoProperties(Optional ByRef UnoObject As Variant) As Variant
776 &apos;&apos;&apos; Returns a list of the properties of an UNO object
777 &apos;&apos;&apos; Code-snippet derived from XRAY
778 &apos;&apos;&apos; Args:
779 &apos;&apos;&apos; UnoObject: the object to identify
780 &apos;&apos;&apos; Returns:
781 &apos;&apos;&apos; A zero-based sorted array. May be empty
783 Dim oIntrospect As Object &apos; com.sun.star.beans.Introspection
784 Dim oInspect As Object &apos; com.sun.star.beans.XIntrospectionAccess
785 Dim vProperties As Variant &apos; Array of com.sun.star.beans.Property
786 Dim vProperty As Object &apos; com.sun.star.beans.Property
787 Dim lMax As Long &apos; UBounf of vProperties
788 Dim vPropertiesList As Variant &apos; Return value
789 Dim i As Long
790 Const cstThisSub = &quot;Session.UnoProperties&quot;
791 Const cstSubArgs = &quot;UnoObject&quot;
793 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
795 Check:
796 vPropertiesList = Array()
797 If VarType(UnoObject) &lt;&gt; V_OBJECT Then GoTo Finally
798 If IsNull(UnoObject) Then GoTo Finally
800 Try:
801 On Local Error GoTo Catch
802 Set oIntrospect = SF_Utils._GetUNOService(&quot;Introspection&quot;)
803 Set oInspect = oIntrospect.inspect(UnoObject)
804 vProperties = oInspect.getProperties(com.sun.star.beans.PropertyConcept.ALL)
806 &apos; The names must be extracted from com.sun.star.beans.Property structures
807 lMax = UBound(vProperties)
808 If lMax &gt;= 0 Then
809 ReDim vPropertiesList(0 To lMax)
810 For i = 0 To lMax
811 vPropertiesList(i) = vProperties(i).Name
812 Next i
813 vPropertiesList = SF_Array.Sort(vPropertiesList, CaseSensitive := True)
814 End If
816 Finally:
817 UnoProperties = vPropertiesList
818 SF_Utils._ExitFunction(cstThisSub)
819 Exit Function
820 Catch:
821 On Local Error GoTo 0
822 GoTo Finally
823 End Function &apos; ScriptForge.SF_Session.UnoProperties
825 REM -----------------------------------------------------------------------------
826 Public Function WebService(Optional ByVal URI As Variant) As String
827 &apos;&apos;&apos; Get some web content from a URI
828 &apos;&apos;&apos; Args:
829 &apos;&apos;&apos; URI: URI text of the web service
830 &apos;&apos;&apos; Returns:
831 &apos;&apos;&apos; The web page content of the URI
832 &apos;&apos;&apos; Exceptions:
833 &apos;&apos;&apos; CALCFUNCERROR
834 &apos;&apos;&apos; Examples:
835 &apos;&apos;&apos; session.WebService(&quot;wiki.documentfoundation.org/api.php?&quot; _
836 &apos;&apos;&apos; &amp; &quot;hidebots=1&amp;days=7&amp;limit=50&amp;action=feedrecentchanges&amp;feedformat=rss&quot;)
838 Dim sReturn As String &apos; Returned value
839 Const cstThisSub = &quot;Session.WebService&quot;
840 Const cstSubArgs = &quot;URI&quot;
842 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
843 sReturn = &quot;&quot;
845 Check:
846 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
847 If Not SF_Utils._Validate(URI, &quot;URI&quot;, V_STRING) Then GoTo Finally
848 End If
850 Try:
851 sReturn = SF_Session.ExecuteCalcFunction(&quot;WEBSERVICE&quot;, URI)
853 Finally:
854 WebService = sReturn
855 SF_Utils._ExitFunction(cstThisSub)
856 Exit Function
857 Catch:
858 GoTo Finally
859 End Function &apos; ScriptForge.SF_Session.WebService
861 REM =========================================================== PRIVATE FUNCTIONS
863 REM -----------------------------------------------------------------------------
864 Private Function _GetScript(ByVal psLanguage As String _
865 , ByVal psScope As String _
866 , ByVal psScript As String _
867 ) As Object
868 &apos;&apos;&apos; Get the adequate script provider and from there the requested script
869 &apos;&apos;&apos; Called by ExecuteBasicScript() and ExecutePythonScript()
870 &apos;&apos;&apos; The execution of the script is done by the caller
871 &apos;&apos;&apos; Args:
872 &apos;&apos;&apos; psLanguage: Basic or Python
873 &apos;&apos;&apos; psScope: one of the SCRIPTISxxx constants
874 &apos;&apos;&apos; The SCRIPTISOXT constant is an alias for 2 cases, extension either
875 &apos;&apos;&apos; installed for one user only, or for all users
876 &apos;&apos;&apos; Managed here by trial and error
877 &apos;&apos;&apos; psScript: Read https://wiki.openoffice.org/wiki/Documentation/DevGuide/Scripting/Scripting_Framework_URI_Specification
878 &apos;&apos;&apos; Returns:
879 &apos;&apos;&apos; A com.sun.star.script.provider.XScript object
881 Dim sScript As String &apos; The complete script string
882 Dim oScriptProvider As Object &apos; Script provider singleton
883 Dim oScript As Object &apos; Return value
884 Const cstScript1 = &quot;vnd.sun.star.script:&quot;
885 Const cstScript2 = &quot;?language=&quot;
886 Const cstScript3 = &quot;&amp;location=&quot;
888 Try:
889 &apos; Build script string
890 sScript = cstScript1 &amp; psScript &amp; cstScript2 &amp; psLanguage &amp; cstScript3 &amp; LCase(psScope)
892 &apos; Find script
893 Set oScript = Nothing
894 &apos; Python only: installation of extension is determined by user =&gt; unknown to script author
895 If psScope = SCRIPTISOXT Then &apos; =&gt; Trial and error
896 On Local Error GoTo ForAllUsers
897 sScript = cstScript1 &amp; psScript &amp; cstScript2 &amp; psLanguage &amp; cstScript3 &amp; SCRIPTISPERSOXT
898 Set oScriptProvider = SF_Utils._GetUNOService(&quot;ScriptProvider&quot;, SCRIPTISPERSOXT)
899 Set oScript = oScriptProvider.getScript(sScript)
900 End If
901 ForAllUsers:
902 On Local Error GoTo CatchNotFound
903 If IsNull(oScript) Then
904 If psScope = SCRIPTISOXT Then psScope = SCRIPTISSHAROXT
905 Set oScriptProvider = SF_Utils._GetUNOService(&quot;ScriptProvider&quot;, psScope)
906 Set oScript = oScriptProvider.getScript(sScript)
907 End If
909 Finally:
910 _GetScript = oScript
911 Exit Function
912 CatchNotFound:
913 SF_Exception.RaiseFatal(NOSCRIPTERROR, psLanguage, &quot;Scope&quot;, psScope, &quot;Script&quot;, psScript)
914 GoTo Finally
915 End Function &apos; ScriptForge.SF_Session._GetScript
917 REM =============================================== END OF SCRIPTFORGE.SF_SESSION
918 </script:module>