scriptingunolibreoffice-basicopenoffice-basic

getScriptProvider and createScriptProvider in Libre/Open/Collabra Office


If location="document" Then
    sp = ThisComponent.getScriptProvider()
Else
    mspf = CreateUNOService("com.sun.star.script.provider.MasterScriptProviderFactory")
    sp = mspf.createScriptProvider("")
End If

That's a fairly standard example from the documentation: there is an example here: https://help.libreoffice.org/7.1/en-US/text/sbasic/guide/basic_2_python.html, and it's been around for a while: there is a /6.4/ example at the same place.

Any idea why it written like that, with getScriptProvider and createScriptProvider? The construct suggest that in some circumstances you have to Create a script provider, because you can't Get an existing one. But in my checks, getScriptProvider also works for 'user' and 'share' (which are just different locations for finding scripts).

It's not important to me, since I am only using location="document", but has anyone ever documented a situation where getScriptProvider fails?


Solution

  • Look again at the line you're asking about.

    sp = ThisComponent.getScriptProvider()

    In some cases, you may not have a component to work with, for example, if the current document was (or will be) closed. So, get a script provider from the MasterScriptProviderFactory instead.

    The source of the script provider determines which code can be run. If the code is in a different document, then get the script provider from that one instead, especially if the function has the same name in different documents.

    One good thing about user and share is that they are available no matter where the script provider comes from.