easymacro/docs/source/application.rst

369 lines
6.5 KiB
ReStructuredText

Application
===========
Remember, always import library.
.. code-block:: python
import easymacro as app
Create instances
----------------
* Instances without context
.. code-block:: python
toolkit = app.create_instance('com.sun.star.awt.Toolkit')
* Instances with context
.. code-block:: python
service = 'com.sun.star.awt.DialogProvider2'
dialog = app.create_instance(service, True)
* Get desktop
.. code-block:: python
desktop = app.create_instance('com.sun.star.frame.Desktop', True)
Or
.. code-block:: python
desktop = app.lo.desktop
Fonst
-----
* Get all fonts
.. code-block:: python
fonts = app.lo.fonts()
for f in fonts:
print(f'Name: {f.Name} - StyleName: {f.StyleName}')
Filters
-------
* Get all `support filters`_
.. code-block:: python
filters = app.lo.filters()
ds = []
for f in filters:
data = f"UI Name: {f['UIName']} - Name: {f['Name']} - Type: {f['Type']}"
app.debug(data)
Call dispatch
-------------
You can call any `dispatch command`_ used only if property or method no exists in original object or in `easymacro.py`
.. code-block:: python
doc = app.docs.active
command = 'Gallery'
app.lo.dispatch(doc, command)
Method automatically add `.uno:`
ClipBoard
---------
* Set text in clipboard
.. code-block:: python
app.clipboard.set('My Text')
* Get content of clipboard
.. code-block:: python
content = app.clipboard.content
app.debug(content)
Disable or enabled commands
---------------------------
You can disable any command.
.. code-block:: python
cmd = 'OpenFromCalc'
result = app.command.disable(cmd)
app.debug(result)
And enabled.
.. code-block:: python
result = app.command.enabled(cmd)
app.debug(result)
`OpenFromCalc` is options for open documents in calc, disable or enabled menu entry and icon toolbar.
LibreOffice configuration
-------------------------
Get value
^^^^^^^^^
Some values are read only.
.. code-block:: python
node_name = '/org.openoffice.Office.Common/Help'
key = 'System'
value = app.get_app_config(node_name, key)
app.debug(value)
node_name = '/org.openoffice.Office.Common/Misc/'
key = 'FirstRun'
value = app.get_app_config(node_name, key)
app.debug(value)
key = 'UseSystemFileDialog'
value = app.get_app_config(node_name, key)
app.debug(value)
Set value
^^^^^^^^^
.. code-block:: python
node_name = '/org.openoffice.Office.UI/ColorScheme'
key = 'CurrentColorScheme'
new_value = 'LibreOffice Dark'
result = app.set_app_config(node_name, key, new_value)
app.debug(result)
.. warning::
Caution with modify registry, not all nodes we can change.
Some nodes and keys:
* `/org.openoffice.Office.Common/Save/Document`
* AutoSave
* AutoSaveTimeIntervall
Shortcuts
---------
Global
^^^^^^
Iter in all shortcuts. Shortcuts for all applications.
.. code-block:: python
for shortcut, command in app.shortcuts:
app.debug(shortcut, command)
If you want all info.
.. code-block:: python
sc = app.shortcuts
data = sc.get_all()
app.debug(data)
Verify if exists shortcut.
.. code-block:: python
sc = app.shortcuts
shortcut = 'Shift+Ctrl+Alt+T'
app.debug(shortcut in sc)
Add new shortcut for execute uno command.
.. code-block:: python
sc = app.shortcuts
shortcut = 'Shift+Ctrl+Alt+T'
command = 'MacroDialog'
sc.set(shortcut, command)
Add new shortcut for execute macro.
.. code-block:: python
sc = app.shortcuts
shortcut = 'Shift+Ctrl+Alt+M'
macro = {'library': 'test', 'name': 'main'}
sc.set(shortcut, macro)
Get `command` by `shortcut`.
.. code-block:: python
sc = app.shortcuts
shortcut = 'Shift+Ctrl+Alt+M'
command = sc.get_by_shortcut(shortcut)
app.debug(command)
Get `shortcut` by `command`. Could be more than one.
.. code-block:: python
sc = app.shortcuts
command = 'MacroDialog'
shortcuts = sc.get_by_command(command)
app.debug(shortcuts)
Remove by shortcut.
.. code-block:: python
sc = app.shortcuts
shortcut = 'Shift+Ctrl+Alt+M'
sc.remove_by_shortcut(shortcut)
Remove by command.
.. code-block:: python
sc = app.shortcuts
macro = {'library': 'test', 'name': 'main'}
sc.remove_by_command(macro)
Reset all editions.
.. code-block:: python
app.shortcuts.reset()
For applications
^^^^^^^^^^^^^^^^
Get shortcuts for application. For Calc.
.. code-block:: python
sc = app.shortcuts['calc']
All methods some the same.
For other applications: `writer`, `draw`, `impress`, `math`
Menus
-----
Add new
^^^^^^^
Insert new menu in Calc.
.. code-block:: python
menu_name = 'zaz.my.menu'
menu = {
'Label': 'My menu',
'CommandURL': menu_name,
'Submenu': [
{
'Label': 'Open Macros Dialog...',
'CommandURL': 'MacroDialog',
},
{
'Label': '-',
},
{
'Label': 'My macro',
'CommandURL': {'library': 'test', 'name': 'hello'},
},
{
'Label': 'Execute macro...',
'CommandURL': 'RunMacro',
'ShortCut': 'Shift+Ctrl+Alt+E',
},
]
}
menu_bar = app.menus['calc']
menu_bar.insert(menu)
Remove
^^^^^^
.. code-block:: python
menu_name = 'zaz.my.menu'
menu_bar = app.menus['calc']
menu_bar.remove(menu_name)
Insert in exists menu
^^^^^^^^^^^^^^^^^^^^^
Insert if not exists in menu Tools, after submenu Macros.
.. code-block:: python
menu = app.menus['calc']['tools']
menu_name = 'zaz.my.menu'
menu_new = {
'Label': 'My menu',
'CommandURL': menu_name,
'Submenu': [
{
'Label': 'Open Macros Dialog...',
'CommandURL': 'MacroDialog',
},
{
'Label': '-',
},
{
'Label': 'My macro',
'CommandURL': {'library': 'test', 'name': 'hello'},
},
{
'Label': 'Execute macro...',
'CommandURL': 'RunMacro',
'ShortCut': 'Shift+Ctrl+Alt+E',
},
]
}
if menu_name in menu:
menu.remove(menu_name)
else:
menu.insert(menu_new, '.uno:MacrosMenu')
.. _dispatch command: https://wiki.documentfoundation.org/Development/DispatchCommands
.. _support filters: https://help.libreoffice.org/latest/en-US/text/shared/guide/convertfilters.html