2018-03-18

1: LibreOffice or OpenOffice with Java, C++, C#, VB.NET, Python, JavaScript, and/or so on

> <The previous article in this series | The table of contents of this series | The next article in this series>

Via UNO, not Microsoft COM. From inside (macros) and/or outside (external programs). The GUI and/or key bindings augmented. Call 'soffice'?

Topics


About: UNO (Universal Network Objects)
About: LibreOffice
About: Apache OpenOffice
About: The Java programming language
About: C++
About: Microsoft .NET Framework (C#, Visual Basic.NET, and so on)
About: The Python programming language
About: JavaScript
About: BeanShell

The table of contents of this article


Starting Context


  • The reader has a basic knowledge on the personal computer architecture (what file, program, program instance (process), and so on are).
  • The reader has a basic knowledge on LibreOffice or Apache OpenOffice.
  • The reader has a basic knowledge on (or willingness to learn) at least one of the programming languages cited in 'Topics'.

Target Context


  • The reader will know a bare outline of what can be done with LibreOffice or Apache OpenOffice more than using it as a ready-made interactive application.

Orientation


This article is the prologue to the series, 'Exploiting LibreOffice or Apache OpenOffice'.

The details of what are mentioned in this article will be dug into in subsequent articles of this series.

Stage Direction
Here are Hypothesizer 7, Objector 1A, and Objector 1B in front of a computer.


Main Body


1: UNO Is a Set of Specifications Based on Which LibreOffice and Apache OpenOffice Are Built


Hypothesizer 7
While what UNO is exactly like will be discussed in the next article, you are supposed to know here at least that UNO is a set of specifications based on which LibreOffice and Apache OpenOffice are built.

Objector 1A
"UNO"? I'm not interested.

Hypothesizer 7
Sir, you will have to be interested if you intend to exploit LibreOffice or Apache OpenOffice extensively.

Objector 1A
But "UNO" sounds irrelevant.

Hypothesizer 7
. . . It is unwise to judge something with the sound.

It is with UNO that your program instance will operate your LibreOffice or Apache OpenOffice instance.

Objector 1A
So, UNO is an API with which I program LibreOffice. You should have said so!

Hypothesizer 7
Sir, it will be beneficial to understand that UNO is not just an interface between your program and LibreOffice or Apache OpenOffice, but is how LibreOffice and Apache OpenOffice themselves are built.

Objector 1A
Bullshit! I don't care how LibreOffice is built!

Hypothesizer 7
. . .

Objector 1A
Can't I use COM?

Hypothesizer 7
Do you mean Microsoft COM, that Microsoft-oriented specifications, sir?

Objector 1A
Of course.

Hypothesizer 7
I do not say that you cannot at all, but you cannot do much with it.

Objector 1A
Why the hell?

Hypothesizer 7
That is because LibreOffice and Apache OpenOffice are not built based on Microsoft COM, but on UNO. With UNO, your program instance can manipulate the UNO objects structure in your LibreOffice or Apache OpenOffice instance, which is the reason why that above-mentioned understanding is beneficial.

Objector 1A
Bullshit!

Stage Direction
Hypothesizer 7 scratches the back of his neck.


2: You Can Create Your Macro in Java, Python, JavaScript, or BeanShell, Instead of Basic


Hypothesizer 7
You people do not write your macros in Basic, do you?

Stage Direction
Objector 1A glares at Hypothesizer 7.

Objector 1A
What's wrong if I do?

Hypothesizer 7
I did not say "wrong", did I, sir?

Objector 1A
You meant so!

Stage Direction
Hypothesizer 7 waggles his head sadly.

Hypothesizer 7
You mean, you imagined so, sir.

Objector 1A
I felt it strongly!

Hypothesizer 7
The strength of your imagination is no proof of the realness, sir.

In reality, I was just hoping that any of you had not been misled to use the . . . not-so-well-designed programming language by the . . . not-so-articulate LibreOffice or Apache OpenOffice GUI.

Objector 1A
So, you meant "wrong"!

Hypothesizer 7
No, I did not. If you made a well-informed choice, it's fine, sir.

Objector 1A
"well-informed choice"? Of course!

Hypothesizer 7
Why did you choose Basic over Python, if you indeed chose Basic, if I may ask?

Objector 1A
"chose"? Well, I don't remember having chosen Basic: I just clicked the 'Edit Macros' menu item, which just led me to the Basic IDE.

Hypothesizer 7
. . . Do you call that a well-informed choice, sir?

Objector 1A
I'm not to be blamed! The GUI is!

Hypothesizer 7
In fact, I agree: the GUI is as though you have to use Basic.

Stage Direction
Objector 1A's face becomes livid.

Objector 1A
To think of that, it's wrong! Why wasn't I even asked?! I was treated like a slave! I was treated disrespectfully!

Hypothesizer 7
Well, the GUI may not have any malice, but is at least guilty of being misleading. Actually, what the GUI signifies is that you are very free to edit your macro in Java, Python, JavaScript, or BeanShell, outside the GUI.

Objector 1A
Who can perceive such a signification?!

Hypothesizer 7
I don't guess that many can, which seems to be the reason why Basic is used disproportionately prevalently with its not-so-well-designed-ness.

Objector 1A
Did you say "Java"?

Hypothesizer 7
Yes, I did.

Objector 1A
But the GUI doesn't show any Java option.

Hypothesizer 7
It is inarticulate.

Objector 1A
And it sucks!

Anyway, how can I edit my Java or Python macro "outside the GUI"?

Hypothesizer 7
You can edit it with your favorite text editor, or with your favorite IDE, if you like.

Objector 1A
Is that it?

Hypothesizer 7
If you know where you should place your macro (and the configuration file for Java), basically yes.

Objector 1A
"basically"?

Hypothesizer 7
You may want to know also some techniques like how to use a full Python instead of the limited Python (if that is the case), how to set Java classes paths, how to set Python modules paths, how to debug the macro, and so on.

Objector 1A
What do you mean by "the limited Python"?

Hypothesizer 7
If you have installed your LibreOffice or Apache OpenOffice with a downloaded package from the LibreOffice download page or the Apache OpenOffice download page, the Python being used should be a limited Python that resides inside the LibreOffice or Apache OpenOffice, where 'limited' means that only a part of the full features is included in it.

Objector 1A
Huh? I don't like that; why should I be limited?

Hypothesizer 7
Then, you may want to know a technique.

Objector 1A
Then, let me know the technique.

Hypothesizer 7
It and some other techniques will be discussed in some future articles.

Objector 1A
When?

Hypothesizer 7
You will have to stay tuned in order to know when.

Objector 1A
Bullshit!

Stage Direction
Objector 1B's sob echoes in the room.

Hypothesizer 7
. . . Madam, what happened?

Objector 1B
I'm mortified by having been treated so "disrespectfully".

Hypothesizer 7
. . . Madam.

Objector 1B
Why should I have been treated like that?

Stage Direction
Objector 1B sobs convulsively.

Hypothesizer 7
. . . That is because you people do not spread your objections, I guess.

Objector 1B
I didn't know!

Hypothesizer 7
You know now, madam.

Objector 1B
. . .

Hypothesizer 7
If you choose Java, you can implement basically anything Java allows, but there is an exception: using JavaFX in Linux.

Objector 1A
Huh? That doesn't make sense. Why can't I do whatever java allows?

Hypothesizer 7
That is because the event loop of JavaFX conflicts with the event loop of LibreOffice or Apache OpenOffice.

Objector 1A
Hmm, that's a regret: I was counting on GUI features of Java, because the dialog boxes feature of LibreOffice is poor, to say the least.

Hypothesizer 7
But you can use Swing all right.

Objector 1A
Well, maybe Swing does in most cases, but there are some things only JavaFX allows . . .

Hypothesizer 7
Of course, your macro can invoke a separate JavaFX process.

Objector 1A
You know, just showing the GUI in a separate process doesn't do; the macro has to receive events.

Hypothesizer 7
It just can, via the standard output of the GUI process, for example.

Objector 1A
I can't do such a thing!

Hypothesizer 7
But, it is easy: the macro can just run a thread that reads the standard output.

Objector 1A
. . . I mean, of course, I can do technically, but I can't spare my precious time for such a triviality.

Hypothesizer 7
Of course. I will show my implementation in a future article, if you are interested.

Objector 1A
When?

Hypothesizer 7
You will have to stay tuned in order to know when.

Objector 1A
I shouldn't have asked . . .

Hypothesizer 7
The situation of GUI for Python macros is similar to that of JavaFX for Java macros, including about invoking separate GUI processes.

Objector 1A
You mean, including about showing your implementation, too?

Hypothesizer 7
Yes.

I do not particularly comment on JavaScript or BeanShell because I do not use them, but I feel an obligation to warn that Basic is fundamentally handicapped and the Basic IDE is not so un-clumsy.


3: You Can Create Also Some Other Non-Macro Kinds of UNO Artifacts, Some of Them Only in Java and/or C++


Hypothesizer 7
In fact, macro is not the only kind of UNO artifacts you can create.

Objector 1A
What else should I want to create than macros?

Hypothesizer 7
Well, I cannot comprehensively list all the kinds here, but some examples are UNO service, UNO dispatch command, and spread sheet cell function.

Objector 1A
Do you mean that I can create my original cell function?

Hypothesizer 7
Yes, I do.

However, I have to remind you that some of such non-macro UNO artifacts can be created only in Java and/or C++, as far as I know.

Objector 1A
A vague story . . .

Hypothesizer 7
Sufficient details can not be dug into in any prologue like this; you will have to read some of subsequent articles for such details.


4: You Can Augment GUI Menus (Including Context Menus) and/or Tool Boxes


Hypothesizer 7
You can augment GUI menus (including context menus) and/or tool boxes.

Objector 1B
Hmm, not bad. . . . Is it difficult?

Hypothesizer 7
Not particularly, if you know how, madam.

Objector 1B
It can be done with Python?

Hypothesizer 7
Actually, in order to add your static menu item or tool button, you need only to create and register a configuration XML file, except you have to already have a macro or UNO dispatch command that is to be registered as the event handler.

Objector 1B
Not bad at all.

Hypothesizer 7
In order to add your context menu item, you need to do some programming in Java, C++, or Python (maybe also in JavaScript or BeanShell, but I don't know).


5: You Can Operate Your LibreOffice or Apache OpenOffice Instance Also From Outside (External Programs)


Hypothesizer 7
You people already know that your LibreOffice or Apache OpenOffice instance can be operated from inside, using macros.

In fact, the LibreOffice or Apache OpenOffice instance can be operated also from outside.

Objector 1A
Which means . . . what?

Hypothesizer 7
You set up the LibreOffice or Apache OpenOffice instance as a UNO server, and your external program connects to the server via TCP/IP socket or named pipe.

Objector 1A
Can my external program be in a remote computer?

Hypothesizer 7
If you use TCP/IP socket, yes; otherwise, no.

Objector 1A
Can my external program be a Web server?

Hypothesizer 7
There is no reason why it should not be a Web server, or whatever program.

Objector 1B
But do I have to create a LibreOffice daemon?

Hypothesizer 7
No, madam, there is no reason why the LibreOffice server must be a daemon, or a Windows service, although you can easily create such a daemon or a Windows service.


6: You Can Programmatically Operate Your LibreOffice or Apache OpenOffice Instance in Almost Any Way


Objector 1B
You say "your LibreOffice or Apache OpenOffice instance can be operated", but what can be done, exactly?

Hypothesizer 7
Almost anything you can do via the GUI of LibreOffice or Apache OpenOffice can do can be done by your program.

Objector 1B
"Almost" . . .

Hypothesizer 7
Some intricate GUI manipulations like moving the text cursor in the spread sheet cell can not be done (as far as I know), but basic operations like opening a file, reading the document, writing the document, storing the document, and closing the document can be done of course.

Objector 1B
What kinds of files, exactly?

Hypothesizer 7
You can see them via the GUI: any file that can be dealt with via the GUI can be dealt with from your program.

Madam, you may want to correctly understand that your program does not directly handle any file, but asks the LibreOffice or Apache OpenOffice instance to handle the file, so there is no reason why whatever file that can be dealt with by the LibreOffice or Apache OpenOffice instance can not be dealt with from your program.

Objector 1B
Can my program set some styles like fonts, colors, and so on?

Hypothesizer 7
Certainly.


7: You Can Create Your Extension for Distributing a Set of Some UNO Artifacts


Hypothesizer 7
You can create your extension for distributing a set of some UNO artifacts.

Objector 1B
"extension" sounds difficult to create . . .

Hypothesizer 7
In fact, extension is just a ZIP file, although you have to prepare some configuration files.

Objector 1B
But how about "UNO artifacts"?

Hypothesizer 7
A typical UNO artifact is a macro, which you can create, can't you, madam?

Objector 1B
"macro"? I thought, "UNO artifact" is something special.

Hypothesizer 7
You do not need to create any UNO artifact in any specific way for being included in any extension, if that is what you mean.

Objector 1B
Is it difficult to create those configuration files?

Hypothesizer 7
I do not see why it will be regarded as difficult, if the specifications are clear.

Objector 1B
But are they clear?

Hypothesizer 7
They will be cleared in some future articles.


8: Calling 'soffice' Repeatedly Is Handicapped Efficiency-Wise and Functionality-Wise


Objector 1A
As my program calls 'soffice --convert-to', . . .

Stage Direction
Hypothesizer 7 takes and lets out a long deep breath.

Objector 1A
. . . Why did you sigh?

Hypothesizer 7
Did I?

Objector 1A
You did!

Hypothesizer 7
Then, I must have took a deep breath, unconsciously.

Objector 1A
It was a sigh!

Hypothesizer 7
You need not bother with whatever it was.

Objector 1A
. . . As my program calls 'soffice --convert-to', . . .

Stage Direction
Hypothesizer 7 takes a deep breath.

Objector 1A
. . ., the concurrent conversions except the first of them fail.

Hypothesizer 7
I know.

Objector 1A
. . . And?

Hypothesizer 7
And what, sir?

Objector 1A
The solution!

Stage Direction
Hypothesizer 7 takes a deep breath.

Hypothesizer 7
As you seem not be concerned with efficiency, why don't you just synchronize the calls?

Objector 1A
Huh? Why do I seem so? Of course, efficiency matters!

Hypothesizer 7
Then, using 'soffice --convet-to' at all is ill-advised. Don't you know the news? I wonder why the news does not spread.

Objector 1A
How dare you! I use what I want!

Stage Direction
Hypothesizer 7 takes a deep breath.

Hypothesizer 7
It will be my obligation to point out that your problem will be solved cleanly by directly using the UNO API, but if you insist to stick with 'soffice --convert-to', of course, you have every right to do so on your own responsibility.

Objector 1A
What an attitude! Insolent!

Hypothesizer 7
. . . Sir, I do not understand what you demand of me. I just agreed with you: you can do as you want. So, moving on . . .

Objector 1A
Impertinent!

Hypothesizer 7
. . . Some sample code for converting files will be shown in Java, C++, C#, and Python, in a future article

Objector 1A
Absurd!

Hypothesizer 7
I should point out also that macros can be called far better efficiency-wise and functionality-wise by directly using the UNO API than by calling 'soffice': even in-file macros can be called nicely, as is shown in a future article.

Objector 1A
He is lying!


References


<The previous article in this series | The table of contents of this series | The next article in this series>