Invoke Python script with Basic in OXT

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

Invoke Python script with Basic in OXT

Post by Mr.Dandy »

Hello all,

As titled, I'm trying to call a python script since my OXT.
I put Test.py in a Scripts folder and call through MasterScriptProviderFactory service.

Code: Select all

Sub GiveMeSum
   msgbox RunMe("DandyMe.oxt|Scripts|Test.py$sum", array() )
End Sub
But this don't work. Python script is not found.
I provide the OXT sample to reproduce.

Advice welcome :)
Attachments
DandyMe.oxt
(1.98 KiB) Downloaded 206 times
OpenOffice 4.1.12 - Windows 10
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Invoke Python script with Basic in OXT

Post by Villeroy »

This calls a soundex function stored in $PROFILE/Scripts/pyCalc/SheetFunctions.py

Code: Select all

Function getMasterScriptProvider()
   if NOT isObject(g_MasterScriptProvider) then
      oMasterScriptProviderFactory = createUnoService("com.sun.star.script.provider.MasterScriptProviderFactory")
      g_MasterScriptProvider = oMasterScriptProviderFactory.createScriptProvider("")
   endif
   getMasterScriptProvider = g_MasterScriptProvider
End Function

Function SOUNDEX(s$, optional iLen%)
   sURL = URL_Main & "soundex" & URL_Args
   oMSP = getMasterScriptProvider()
   oScript = oMSP.getScript(sURL)
   if isMissing(iLen) then
      i = 4
   else
      i = cInt(iLen)
   endif
   x = oScript.invoke(Array(s,i),Array(),Array())
   SOUNDEX = x
End Function
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

Re: Invoke Python script with Basic in OXT

Post by Mr.Dandy »

OK but my problem is to call my script inside OXT location.
This code returns a path error:

Code: Select all

Function RunMe(sScript As String, cmd As Variant) As Variant
	Dim oMspf As Object, oSP As Object, oPy As Object
	Dim sLoc As String
	oMspf = createUnoService("com.sun.star.script.provider.MasterScriptProviderFactory")
	oSP = oMspf.createScriptProvider("")
	
	sLoc = "user:uno_packages"
	oPy = oSP.getScript("vnd.sun.star.script:" & sScript & "?language=Python&location=" & sLoc)
	
	RunMe = oPy.invoke(cmd, Array(), Array())
End Function
So which is the content for sScript and sLoc in my case ?

Thanks
OpenOffice 4.1.12 - Windows 10
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Invoke Python script with Basic in OXT

Post by Villeroy »

I don't know. With Python you can implement an UNO service with an interface providing this function.

Then you would call that function similar to sheet functions like this:

Code: Select all

oService = createUnoService("MyTools.PyFunctionProvider")
x = oService.callFunction("linecount", sPathName)
-----------------------------
You may also implement this as a sheet function add-in and then call it with the built-in FunctionAccess service:

Code: Select all

oService = createUnoService("com.sun.star.sheet.FunctionAccess")
x = oService.callFunction("MyTools.PyFunctionProvider.linecount", Array(sPathName))
viewtopic.php?f=20&t=95466&p=457840#p457586 has an array function which solves sudoku puzzles like this: =PYSUDOKU(9x9_CellRange) [Ctrl+Shift+Enter] The resulting array is the solved 9x9 range. The function is accessible by c.s.s.sheet.FunctionAccess.
-------------------
It may be possible to install a Python module just like Basic modules, so the code is accessible as a macro via Tools>Macros>.... I don't know and I don't care anymore. I wasted too much life time with stuff like that.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: Invoke Python script with Basic in OXT

Post by hubert lambert »

Hi,

Preferred solutions should be Villeroy's ones.
But if you want to keep it simple, you juste need to declare your Scripts folder as a script container, adding this line in your manifest.xml :

Code: Select all

 <manifest:file-entry manifest:full-path="Scripts" manifest:media-type="application/vnd.sun.star.framework-script"/> 
See https://wiki.openoffice.org/wiki/Docume ... de_service.
Regards.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
Post Reply