Bug 151076 - Python script InsertText.py fails with error message
Summary: Python script InsertText.py fails with error message
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Writer (show other bugs)
Version:
(earliest affected)
7.3.6.2 release
Hardware: All All
: medium normal
Assignee: Rafael Lima
URL:
Whiteboard: target:7.5.0
Keywords:
Depends on:
Blocks:
 
Reported: 2022-09-20 09:39 UTC by Alex Thurgood
Modified: 2022-10-10 18:26 UTC (History)
1 user (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alex Thurgood 2022-09-20 09:39:23 UTC
Description:
1) Start LibreOffice
2) Open new Writer document
3) Type some text
4) Macros > Run > Select the Insert Text python script
5) Click on Run.
6) Error message:
com.sun.star.uno.RuntimeException: Error during invoking function InsertText in module file:///Applications/LibreOffice.app/Contents/Resources/Scripts/python/InsertText.py (<class 'TypeError'>: InsertText() missing 1 required positional argument: 'text'
  File "/Applications/LibreOffice.app/Contents/Resources/pythonscript.py", line 915, in invoke
    ret = self.func( *args )
)



Steps to Reproduce:
See above

Actual Results:
Script fails to execute, displays error message

Expected Results:
Script should execute and insert text.


Reproducible: Always


User Profile Reset: Yes



Additional Info:
Version: 7.3.6.2 / LibreOffice Community
Build ID: c28ca90fd6e1a19e189fc16c05f8f8924961e12e
CPU threads: 8; OS: Mac OS X 12.6; UI render: default; VCL: osx
Locale: fr-FR (fr_FR.UTF-8); UI: fr-FR
Calc: threaded

from macOS appstore, if that makes any difference (Arm M1)
Comment 1 Rafael Lima 2022-09-20 19:15:01 UTC
Repro with

Version: 7.3.6.2 / LibreOffice Community
Build ID: 30(Build:2)
CPU threads: 12; OS: Linux 5.15; UI render: default; VCL: kf5 (cairo+xcb)
Locale: pt-BR (pt_BR.UTF-8); UI: en-US
Ubuntu package version: 1:7.3.6-0ubuntu0.22.04.1
Calc: threaded

Indeed the macro does not work even in a new LO installation.
Comment 2 Rafael Lima 2022-09-20 19:28:45 UTC
TBH I really don't know why this macro is even accessible via Toos - Macros - Run Macro, because it requires a positional "text" argument to work. See:

https://opengrok.libreoffice.org/xref/core/scripting/examples/python/InsertText.py

So it seems the "InsertText" function is meant to be called by other scripts only and not as a standalone script.

For instance, create a Writer document and open an APSO Python shell. Now type:

from InsertText import InsertText
InsertText("Hello")

The code above will insert "Hello" where your cursor is located.

The only weird thing is that the InsertText macro should not be reachable via the Run Macro menu. The script isn't even exported using g_exportedScripts, so I don't see why it is listed to the user.
Comment 3 Rafael Lima 2022-09-20 19:43:12 UTC
A simple fix is to add the following line at the end of InsertText.py.

g_exportedScripts = ()

This would make the script no longer appear in the Run Macro dialog. And it would still work when called from another macro, as in the example I gave in Comment #2.

Let's leave this as UNCONFIRMED for now to see if anyone else has other ideas on this matter.
Comment 4 Commit Notification 2022-10-07 13:13:51 UTC
Rafael Lima committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/84e9a0056967d5c641ec63bf6a4dc6c0f2798e3d

tdf#151076 Create callable Python script using InsertText

It will be available in 7.5.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.