[英]Why does desktop.getCurrentComponent() return None in PyUNO?
Trying to revive a PyUNO sample script called Wavelet to learn how LO works nowadays and get re-started.试图恢复一个名为Wavelet的 PyUNO 示例脚本,以了解当今 LO 的工作原理并重新开始。 Since LibreOffice & UNO changed a bit from the script's creation time I am running into problems.
由于 LibreOffice 和 UNO 从脚本的创建时间发生了一些变化,我遇到了问题。
Managed to get the desktop object.设法获得桌面 object。 Now I want to retrieve the open document's component.
现在我想检索打开文档的组件。 How do I achieve this properly?
我该如何正确实现这一目标? The
desktop.getCurrentComponent()
call returns None
. desktop.getCurrentComponent()
调用返回None
。
LibreOffice version: 6.4.6.2. LibreOffice 版本: 6.4.6.2。
System: Ubuntu MATE 20.04 x86_64.系统: Ubuntu MATE 20.04 x86_64。
The code follows:代码如下:
#!/usr/bin/python3
def TestWave(event):
wavelet = Wavelet(XSCRIPTCONTEXT)
wavelet.trigger( () )
import uno
import unohelper
import string
from com.sun.star.task import XJobExecutor
class Wavelet( unohelper.Base, XJobExecutor ):
def __init__( self, ctx ):
self.ctx = ctx
def trigger( self, args ):
desktop = self.ctx.ServiceManager.createInstanceWithContext(
"com.sun.star.frame.Desktop", self.ctx )
doc = desktop.getCurrentComponent()
print('doc:', doc)
#try:
search = doc.createSearchDescriptor()
search.SearchRegularExpression = True
search.SearchString = "\\<(k|s|v|z|o|u|i|a) "
found = doc.findFirst( search )
while found:
print("found:", found.String)
found.String = string.replace( found.String, " ", u"\xa0" )
found = doc.findNext( found.End, search)
#except:
# pass
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(
Wavelet,
"name.vojta.openoffice.Wavelet",
("com.sun.star.task.Job",),)
if __name__ == "__main__":
import os
# Start OpenOffice.org, listen for connections and open testing document
os.system( "loffice '--accept=socket,host=localhost,port=2002;urp;' --writer ./WaveletTest.odt &" )
# Get local context info
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", localContext )
ctx = None
# Wait until the OO.o starts and connection is established
while ctx == None:
try:
ctx = resolver.resolve(
"uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
except:
pass
# Trigger our job
wavelet = Wavelet( ctx )
wavelet.trigger( () )
Output: Output:
doc: None
Traceback (most recent call last):
File "./wavelet.py", line 62, in <module>
wavelet.trigger( () )
File "./wavelet.py", line 24, in trigger
search = doc.createSearchDescriptor()
AttributeError: 'NoneType' object has no attribute 'createSearchDescriptor'
Cross posted at the following address: https://ask.libreoffice.org/en/question/283785/why-does-desktopgetcurrentcomponent-return-none-in-pyuno/交叉张贴在以下地址: https://ask.libreoffice.org/en/question/283785/why-does-desktopgetcurrentcomponent-return-none-in-pyuno/
Try without giving desktop.getCurrentComponent()
a variable, so erase doc =
.尝试不给
desktop.getCurrentComponent()
一个变量,所以删除doc =
。 I remember I had that problem, but did not understand why it was doing it.我记得我有这个问题,但不明白为什么会这样。 All I remember is that not naming it made my code work.
我只记得不命名它使我的代码工作。 That is the only advice I can give you.
这是我能给你的唯一建议。
Tried to wait until the document component becomes available.试图等到文档组件可用。 And it worked:
它奏效了:
doc = None
while doc is None:
doc = desktop.getCurrentComponent()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.