Enhavo
Indekso 🔎︎

# ScriptForge.Services service

The ScriptForge library is built upon an extensible collection of so-called "Services".
This collection is implemented as categories of Basic libraries or Python modules:

1. the standard ScriptForge library shipped with LibreOffice

2. a number of "associated" libraries shipped with LibreOffice as well

3. any user/contributor LibreOffice extension wanting to fit into the same framework

A service is a collection of properties or methods which implement the service.

For the author of a user script, a service may be either a module within a library, or an instance of a class module.

An event manager is a script contained in a library which binds an event triggering a macro - usually defined by the Tools - Customize menu - to the concerned service instance.

As an example, if several documents trigger the same macro when they are loaded, it might be useful to know which document triggered the macro this time. That's where an event manager plays its role.

The following methods make up the kernel of the ScriptForge framework:

- RegisterScriptServices

  Called internally by ScriptForge to register for a library the list of services it implements.
  Each library associated to Scriptforge or extending it must implement its own RegisterScriptServices method.

- RegisterService

  Called - as many times as there are services to register in the library - by RegisterScriptServices.

- RegisterEventManager

  Called to register a library event manager by RegisterScriptServices.

- CreateScriptService

  Called by user scripts to get an object giving access to the service given as argument.
  All services should be invoked through the CreateScriptService method.

Conventionally, the String, Array and Exception services may be invoked directly respectively as SF_String, SF_Array and SF_Exception.

List of Methods in the Services Service
CreateScriptService | RegisterScriptServices RegisterService | RegisterEventManager

## CreateScriptService

Gain access to one of the services of a library for the benefit of a user script.
The returned value is a Basic object or Nothing if an error occurred.

A service can be understood as either:

- as a set of methods gathered in a Basic standard module

- or a set of methods and properties gathered in a Basic class module.

### Syntax:

           CreateScriptService(Service As String, [arg0, ...] As Variant) As Variant

### Parameters:

Service: The name of the service identified as "library.service".
The library is a Basic library that must exist in the GlobalScope. The default value is "ScriptForge".
The service is one of the services registered by the library via the RegisterScriptServices() method.

arg0, ...: A list of arguments required by the invoked service.
If the first argument refers to an event manager, then arg0 is mandatory and must be the UNO object representing the event provided as argument to the user macro.

### Example:

            GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
            ' To be done once
            Dim svc As Object
            Set svc = CreateScriptService("Array")
            ' Refers to the "ScriptForge.Array" service or SF_Array
            Set svc = CreateScriptService("ScriptForge.Dictionary")
            ' Returns a new empty dictionary class instance; "ScriptForge." is optional
            Set svc = CreateScriptService("SFDocuments.Calc")
            ' Refers to the Calc service, implemented in the associated SFDocuments library
            Set svc = CreateScriptService("Timer", True)
            ' Returns a Timer class instance starting immediately
            Set svc = CreateScriptService("SFDocuments.DocumentEvent", oEvent)
            ' Refers to the DocumentEvent service implemented in the associated SFDocuments library
            ' Returns the instance of the Document class that fired the event

## RegisterScriptServices

By executing a series of invocations of RegisterService() and RegisterEventManager(), the RegisterScriptServices() method incorporates a library into the ScriptForge framework.
Each library pertaining to the framework must implement its own version of this method.

The method has to be stored in a standard Basic module as opposed to a class module.

A service is either:

- a Basic standard module passed as a Basic object.

- or a string designating the function to execute to get an instance of the service. It is in fact the function containing the New keyword of a Set statement creating the instance.

              GlobalScope.LibraryName.ModuleName ' Object
              "LibraryName.ModuleName.FunctionName" ' String

### Example:

          Public Sub RegisterScriptServices()
          ' To be stored in library = myLibrary
              With GlobalScope.ScriptForge.SF_Services
                  .RegisterService("myService1", GlobalScope.myLibrary.myModule)
                  ' Refer to a Basic standard module implementing the service as a set of methods
                  .RegisterService("myService2", "myLibrary.someModule.someFunction")
                  ' The function should return an instance of a Basic object class implementing the service
                  ' ...
              End With
          End Sub

When a user script contains a statement such as:

    Set myServ = CreateScriptService("myLibrary.myService1")

ScriptForge performs these tasks:

1. load the library myLibrary when necessary

2. invoke the RegisterScriptServices method to load the list of services of myLibrary in memory

3. initialize the variable myServ with the given service

## RegisterService

The method returns True if the name-value pair given as argument could be registered successfully.

### Syntax:

          GlobalScope.ScriptForge.SF_Services.RegisterService(ServiceName As String, ServiceReference As Variant) As Boolean

### Parameters:

ServiceName: The name of the service as a case-insensitive string. The name must be unique.

ServiceReference: A service reference is either:

- a Basic standard module passed as a Basic object.

- or a string designating the function to execute to get an instance of the service. It is in fact the function containing the New keyword of a Set statement creating the instance.

              GlobalScope.LibraryName.ModuleName ' Object
              "LibraryName.ModuleName.FunctionName" ' String

### Example:

              With GlobalScope.ScriptForge.SF_Services
                  .RegisterService("myService1", GlobalScope.myLibrary.myModule)
                  ' Refer to a Basic standard module implementing the service as a set of methods
                  .RegisterService("myService2", "myLibrary.someModule.someFunction")
                  ' The function should return an instance of a Basic object class implementing the service
                  ' ...
              End With

## RegisterEventManager

The method returns True if the name-value pair given as argument could be registered successfully.

### Syntax:

          GlobalScope.ScriptForge.SF_Services.RegisterEventManager(ServiceName As String, ServiceReference As String) As Boolean

### Parameters:

ServiceName: The name of the service as a case-insensitive string. The name must be unique.

ServiceReference: A string designating the function to execute to get an instance of the service. It is in fact the function containing the New keyword of a Set statement creating the instance.:

        "LibraryName.ModuleName.FunctionName" ' String

### Example:

              With GlobalScope.ScriptForge.SF_Services
                  .RegisterEventManager("myEventMgr", "myLibrary.someModule.someFunction")
                  ' The function should return an instance of a Basic object class implementing the service
                  ' ...
              End With

Ĉiuj ScriptForge-BASIC-subprogramoj aŭ identigiloj kun antaŭfiksita substreka signo "_" estas rezervitaj por interna uzado. Ili ne celas al uzado en BASIC-makrooj.

Rilataj temoj

Set Statement

GlobalScope specifier