Invoke Python script with Basic in OXT

Keyboard macros or custom scripts

Invoke Python script with Basic in OXT

Postby Mr.Dandy » Wed Sep 02, 2020 11:10 am

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   Expand viewCollapse view
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 79 times
OpenOffice 4.1.7 - Windows 10
User avatar
Mr.Dandy
 
Posts: 332
Joined: Tue Dec 11, 2012 4:22 pm

Re: Invoke Python script with Basic in OXT

Postby Villeroy » Wed Sep 02, 2020 10:37 pm

This calls a soundex function stored in $PROFILE/Scripts/pyCalc/SheetFunctions.py
Code: Select all   Expand viewCollapse view
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
Villeroy
Volunteer
 
Posts: 29713
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Invoke Python script with Basic in OXT

Postby Mr.Dandy » Thu Sep 03, 2020 8:52 am

OK but my problem is to call my script inside OXT location.
This code returns a path error:
Code: Select all   Expand viewCollapse view
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.7 - Windows 10
User avatar
Mr.Dandy
 
Posts: 332
Joined: Tue Dec 11, 2012 4:22 pm

Re: Invoke Python script with Basic in OXT

Postby Villeroy » Thu Sep 03, 2020 10:13 am

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   Expand viewCollapse view
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   Expand viewCollapse view
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
User avatar
Villeroy
Volunteer
 
Posts: 29713
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Invoke Python script with Basic in OXT

Postby hubert lambert » Mon Sep 07, 2020 2:50 pm

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   Expand viewCollapse view
 <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
hubert lambert
 
Posts: 143
Joined: Mon Jun 13, 2016 10:50 am


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: Google [Bot] and 4 guests