SF_Session.xba 39 KB

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