1
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?

david
  • 2,435
  • 1
  • 21
  • 33
  • "Libre/Open/Collabra Office" — just say "LibreOffice." If for some reason that isn't good enough, add more tags. People who use the other suites are aware that most questions involving one of them apply to the others as well. – Jim K Dec 16 '22 at 19:11

1 Answers1

1

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.

Jim K
  • 12,824
  • 2
  • 22
  • 51