Tools

Remember, always import 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)
  • Application version

app.msgbox(app.VERSION)
  • In Windows

app.msgbox(app.IS_WIN)
  • In Mac

app.msgbox(app.IS_MAC)

Message Box

app.msgbox(app.IS_WIN, 'My Macro')

Show warning

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

Show error box

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

Make question

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

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)

Date and times

  • Get today

app.msgbox(app.today())
  • Get now

app.msgbox(app.now())
  • Get now only time

app.msgbox(app.now(True))
app.msgbox(app.get_epoch())
  • Simple measure time

app.start()
app.sleep(5)
seconds = app.end(True)
app.msgbox(seconds)

Thread

You can execute any macro in thread

  • Normal execution

def show_time(seconds):
    app.sleep(seconds)
    app.msgbox(app.NAME)
    return

def main(args=None):
    show_time(5)
    app.msgbox('Finish...')
    return
  • Run in thread

@app.run_in_thread
def show_time(seconds):
    app.sleep(seconds)
    app.msgbox(app.NAME)
    return

def main(args=None):
    show_time(5)
    app.msgbox('Finish...')
    return

Dictionary <-> properties

args = {
    'Hidden': True,
    'Password': 'letmein',
}
properties = app.dict_to_property(args)

app.msgbox(properties)

data = app.data_to_dict(properties)

app.msgbox(data)

Tuples or lists to dictionary

tuple_of_tuples = (
    ('Hidden', True),
    ('Password', 'letmein'),
)
data = app.data_to_dict(tuple_of_tuples)
app.msgbox(data)

list_of_lists = [
    ['Hidden', True],
    ['Password', 'letmein'],
]
data = app.data_to_dict(list_of_lists)
app.msgbox(data)

Json

data = {
    'Hidden': True,
    'Password': 'letmein',
}

json = app.json_dumps(data)

app.msgbox(json)

data = app.json_loads(json)

app.msgbox(data)

Call Macros

You can any macro, for default call macros Python.

def show_message():
    app.msgbox(app.INFO_DEBUG)
    return

def main(args=None):
    args = {
        'library': 'test',
        'name': 'show_message',
    }
    app.call_macro(args)
    return

Of course is better call directly if both macros are the same languaje, but, you can call macro in Basic too.

Sub show_message()
    MsgBox "Basic from Python"
End Sub

Call from Python with.

args = {
    'language': 'Basic',
    'library': 'Standard',
    'module': 'Module1',
    'name': 'show_message',
}
app.call_macro(args)

Execute macro in other thread

app.call_macro(args, True)

Call external program

app_name = 'gnome-calculator'
app.run(app_name)
app.msgbox('ok')

Call command line and capture output

args = 'ls -lh ~'
result = app.run(args, True)
app.debug(result)
21/06/2021 22:27:22 - DEBUG - total 1.3M
drwxr-xr-x  5 mau mau 4.0K Jun 17 13:09 Desktop
drwxr-xr-x  6 mau mau 4.0K Jun 15 12:35 Documents
drwxr-xr-x  2 mau mau 4.0K Jun 21 20:26 Downloads
drwxr-xr-x  2 mau mau 4.0K Jun 21 16:18 Pictures
drwxr-xr-x 13 mau mau 4.0K Jun 21 15:34 Projects
drwxr-xr-x  2 mau mau 4.0K May 11 18:48 Templates
drwxr-xr-x  2 mau mau 4.0K Jun 20 23:27 Videos

Call command line and capture output line by line.

args = 'ls -lh /home/mau'
for line in app.popen(args):
    app.debug(line)
21/06/2021 22:34:42 - DEBUG - total 1.3M
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x  5 mau mau 4.0K Jun 17 13:09 Desktop
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x  6 mau mau 4.0K Jun 15 12:35 Documents
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x  2 mau mau 4.0K Jun 21 20:26 Downloads
21/06/2021 22:34:42 - DEBUG - -rw-r-----  1 mau mau 1.3M Jun 14 11:53 out.png
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x  2 mau mau 4.0K Jun 21 16:18 Pictures
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x 13 mau mau 4.0K Jun 21 15:34 Projects
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x  2 mau mau 4.0K May 11 18:48 Templates
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x  2 mau mau 4.0K Jun 20 23:27 Videos

Timer

Execute any macro every seconds.

TIMER_NAME = 'clock'

def show_time():
    app.debug(app.now(True))
    return

def start_clock():
    seconds = 1
    macro = {
        'library': 'test',
        'name': 'show_time',
    }
    app.start_timer(TIMER_NAME, seconds, macro)
    return

def stop_clock():
    app.stop_timer(TIMER_NAME)
    return

def main(args=None):
    start_clock()
    return

Execute stop_clock for stop timer.

21/06/2021 22:43:17 - INFO - Timer started... show_time
21/06/2021 22:43:18 - DEBUG - 22:43:18.080315
21/06/2021 22:43:19 - DEBUG - 22:43:19.082211
...
21/06/2021 22:43:46 - DEBUG - 22:43:46.126446
21/06/2021 22:43:47 - DEBUG - 22:43:47.128487
21/06/2021 22:43:47 - INFO - Timer stopped... show_time

Get digest

data = 'LibreOffice with Python'

digest = app.sha256(data)
app.msgbox(digest)

digest = app.sha512(data)
app.msgbox(digest)

Save and get configurations

You can save any data.

my_app = 'my_extension'
data = {
    'path': '/home/mau/work',
    'save_data': True,
}

app.set_config('config', data, my_app)

app.msgbox('Save config')

data = app.get_config('config', my_app)

app.msgbox(data)

Render string

template = """Hello $name

I send you this $file_name

Best regards
"""

data = {'name': 'Ingrid Bergman', 'file_name': 'letter_love.odt'}

render = app.render(template, data)

app.msgbox(render)

Encrypt decrypt

You need install library cryptography

import easymacro as app
from conf import PASSWORD

def encrypt_decrypt():

    data = 'My super secret data'
    token = app.encrypt(data, PASSWORD)
    app.msgbox(token)

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

    return

Simple url open

  • Get text data

url = 'https://api.ipify.org'
data = app.url_open(url)
app.msgbox(data)
  • Get json data

url = 'https://api.ipify.org?format=json'
data = app.url_open(url, get_json=True)
app.msgbox(data)

For more complex case, you can used requests or httpx