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