4 tools
Mauricio Baeza edited this page 2019-10-05 11:50:54 -05:00

Tools

Always import the library

import easymacro as app

Info from PC

  • Operate system
    app.msgbox(app.OS)
  • Current user
    app.msgbox(app.USER)
  • Name PC
    app.msgbox(app.PC)
  • Name desktop, only GNU/Linux
    app.msgbox(app.DESKTOP)
  • Language
    app.msgbox(app.LANG)
  • Language with variant
    app.msgbox(app.LANGUAGE)
  • Application name
    app.msgbox(app.NAME)
  • LibreOffice version
    app.msgbox(app.VERSION)
  • Is windows
    app.msgbox(app.IS_WIN)

Size screen

    result = app.get_size_screen()
    app.msgbox(result)

Make question

    message = 'Is easy Python?'
    title = 'My App'
    result = app.question(message, title)
    app.msgbox(result)

Show warning

    message = 'Caution, this action is dangerous'
    title = 'My App'
    app.warning(message, title)

Show error box

    message = 'ERROR: Ask to technical support'
    title = 'My App'
    app.errorbox(message, title)

Create instances

  • Instances without context
    toolkit = app.create_instance("com.sun.star.awt.Toolkit")
    app.mri(toolkit)
  • Instances with context
    dialog = app.create_instance('com.sun.star.awt.DialogProvider2', True)
    app.mri(dialog)

Call dispatch

  • Call any dispatch command
    command = '.uno:Copy'
    args = ()
    app.call_dispatch(command, args)

Save and get configuration

  • Save value in key, always set your prefix
def main():
    key = 'user'
    value = 'teresa'
    prefix = 'myapp'
    app.set_config(key, value, prefix)

    app.msgbox('Save ok')

    # ~ If not found key, get default
    default = ''
    value = app.get_config(key, default, prefix)
    app.msgbox(value)

    return
  • Save any data
def main():
    key = 'server'
    value = {'name': 'localhost', 'user': 'valedor', 'pass': 'letmein'}
    prefix = 'myapp'
    app.set_config(key, value, prefix)

    app.msgbox('Save ok')

    # ~ If not found key, get default
    default = ''
    value = app.get_config(key, default, prefix)
    app.msgbox(value)

    return

Applications

  • Test if exists any application
    program = 'gnome-calculator'
    result = app.exists_app(program)
    app.msgbox(result)

    program = 'not-exists'
    result = app.exists_app(program)
    app.msgbox(result)
  • Execute any application or command and not wait
    program = 'gnome-calculator'
    app.run(program)
  • Execute command and wait response
    command = 'ls -la'
    result = app.run(command, True)
    app.msgbox(result)

Paths and files

  • Join path
    parts = ('/home/mau/', 'data', 'source.csv')
    path = app.join(*parts)
    app.msgbox(path)
  • Creates a temporary file
    with app.get_temp_file() as f:
        f.write(b'TEST')

    # Or if only need a path
    path = app.get_temp_file().name
    app.msgbox(path)
  • Get paths of Tools->Options, LibreOffice->Paths
    path = app.get_config_path('Work')
    app.msgbox(path)

    path = app.get_config_path('Temp')
    app.msgbox(path)
  • Get selected path of file
    init_dir = app.get_config_path('Work')
    multiple = False
    filters = (
        ('ODS', '*.ods'),
        ('ODT', '*.odt'),
    )
    path = app.get_file(init_dir, multiple, filters)
    app.msgbox(path)
  • Selected multiple files
    init_dir = app.get_config_path('Work')
    multiple = True
    filters = (
        ('TXT', '*.txt'),
        ('LOG' '*.log'),
        ('CER | KEY', '*.cer;*.key'),
    )
    path = app.get_file(init_dir, multiple, filters)
    app.msgbox(path)
  • Get new path of file for save
    init_dir = app.get_config_path('Work')
    filters = (
        ('ODS', '*.ods'),
        ('ODT', '*.odt'),
    )
    path = app.get_path(init_dir, filters)
    app.msgbox(path)
  • Get directory
    init_dir = app.get_config_path('Work')
    path = app.get_dir(init_dir)
    app.msgbox(path)
  • Get parent path, file name, file name without extension and extension from path.
    path = '/home/mau/test.md'
    parts = app.get_info_path(path)
    app.msgbox(parts)
  • Replace extension
    path = '/home/mau/test.md'
    ext = 'pdf'
    new_path = app.replace_ext(path, ext)
    app.msgbox(new_path)
  • Get content of path
    path = '/home/mau/Desktop/test'
    # ~ All content
    files = app.get_path_content(path)
    app.msgbox(files)

    filters = 'pdf'
    files = app.get_path_content(path, filters)
    app.msgbox(files)

    filters = 'ods|odt'
    files = app.get_path_content(path, filters)
    app.msgbox(files)
  • Open file with default application, not used con ODF documents.
    path = '/home/mau/doc.pdf'
    app.open_file(path)

Clipboard

  • Get content
    data = app.get_clipboard()
    app.msgbox(data)
  • Set content
    data = 'Hello pyUNO'
    app.set_clipboard(data)

    data = app.get_clipboard()
    app.msgbox(data)

Today

    app.msgbox(app.today())

Now

    app.msgbox(app.now())

Get date

    app.msgbox(app.get_date(1974, 1, 15))

    app.msgbox(app.get_date(2019, 1, 15, 9, 9, 9))

Sleep

    app.sleep(3)
    # or milliseconds
    app.sleep(0.5)

Unix time

Look https://en.wikipedia.org/wiki/Unix_time

    seconds = app.get_epoch()
    app.msgbox(seconds)

Measure time

def main():
    app.start()
    wait(3)
    app.msgbox(app.end())
    return


def wait(sec):
    app.sleep(sec)
    return

Run in thread

  • Execute any macro in other thread

First, normal execute.

def main():
    cell = app.get_cell()
    app.start()
    show_time(cell)
    app.msgbox(app.end())
    return


def show_time(cell):
    cell.value = str(app.now())
    app.sleep(3)
    return

Now, execute in thread

def main():
    # Active cell
    cell = app.get_cell()
    app.start()
    show_time(cell)
    app.msgbox(app.end())
    return


@app.run_in_thread
def show_time(cell):
    cell.value = str(app.now())
    app.sleep(3)
    return

Encrypt/Decrypt

IMPORTANT: You need copy library fernet into pythonpath

    data = 'Hello World'
    contra = 'letmein'

    token = app.encrypt(data, contra)
    app.msgbox(token)

    # ~ If password is wrong, get empty string
    data = app.decrypt(token, 'wrong')
    app.msgbox(data)

    data = app.decrypt(token, contra)
    app.msgbox(data)

Colors

  • Get integer value for color
    c = app.get_color('red')
    app.msgbox(c)

    # ~ RGB values
    c = app.get_color(255, 0, 0)
    app.msgbox(c)

    # ~ HTML value
    c = app.get_color('#FF0000')
    app.msgbox(c)

More info for color names and HTML values.

InputBox

  • Normal data
    message = 'Type your name'
    default = ''
    title = 'My App'

    result = app.inputbox(message, default, title)
    app.msgbox(result)
  • Private data
    message = 'Type your password'
    default = ''
    title = 'My App'
    echochar = "*"

    result = app.inputbox(message, default, title, echochar)
    app.msgbox(result)

Timer

  • Execute macro every seconds

TIMER_NAME is import for stop timer. Timer always execute in other thread.

CAUTION: Any arguments for macro, must be only objects pyUNO.

Execute macro stop for stop timer.

def main():
    cell = app.get_cell()

    timer_name = 'myclock'
    seconds = 1
    macro = {
        'library': 'mymacros',
        'name': 'myclock',
        'args': (cell.obj,)
    }
    app.timer(timer_name, seconds, macro)

    return


def myclock(cell):
    cell.String = app.now().strftime('%H:%M:%S')
    return


def stop():
    app.stop_timer('myclock')
    return