I am using Calc on Ubuntu to start a spreadsheet from a symlink. In a macro tried to get a path to the spreadsheet location. I have used the ThisComponent.GetURL
according to the standard approach. Unexpectedly, I am getting a path to symlink instead of a document. I am asking for advice on how to get around it?
Other options is use Python:
import uno
import os
def get_real_path():
doc = XSCRIPTCONTEXT.getDocument()
path = os.readlink(uno.fileUrlToSystemPath(doc.URL))
return path
Then… call from Basic…
Sub main()
mspf = CreateUNOService("com.sun.star.script.provider.MasterScriptProviderFactory")
sp = mspf.createScriptProvider("")
uri = "vnd.sun.star.script:test.py$get_real_path?language=Python&location=user"
path = sp.getScript(uri).invoke(Array(), Array(), Array())
MsgBox path
End Sub
Caution, call from document, not from IDE.
I also got error in Basic.
Changed one line in python code from:
def get_real_path():
to:
def get_real_path(*args):
Then changing Basic code to:
dim a1(0), b1(0), c1(0) as variant
scriptPro = ThisComponent.getScriptProvider()
myScript = scriptPro.getScript( _
"vnd.sun.star.script:test.py$get_real_path?language=Python&location=user")
cev = myScript.invoke(a1, b1, c1)
MsgBox cev
.
then worked.
I confirm. Do not understand what I did wrong first time but both scripts now work for me.
Before using this solution in Python, there are two steps to prepare:
- put the python script in a folder
~/.config/libreoffice/4/user/Scripts/python
- install the package, e.g. with a command from the console:
sudo apt install libreoffice-script-provider-python
I have developed the following solution:
Function GetPath as String
Path$ = ConvertFromURL(ThisComponent.URL)
GetPath = Path$
tmpFile$ = "~/TMP/ls.txt"
rem https://forum.openoffice.org/en/forum/viewtopic.php?f=13&t=19427
sBash$ = "bash -c 'realpath "&""""&Path$&""""&" >"&tmpFile$&"'"
Shell(sBash$,,, True)
iNumber = Freefile
Open tmpFile For Input As iNumber
Line Input #iNumber, sLine$
Close #iNumber
If sLine$ <> "" Then GetPath = sLine$
End Function
I admit this is a crude solution. Thank you for other solutions.