-
Notifications
You must be signed in to change notification settings - Fork 2
/
tasks.py
77 lines (59 loc) · 2.59 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import platform
from datetime import datetime
from pathlib import Path
from robocorp import windows
from robocorp.tasks import setup, task
desktop = windows.Desktop()
window = None # Notepad window object after being controlled
LOCATOR_NOTEPAD = 'class:"Notepad" subname:"Notepad"'
TEXT_FILE = str(os.getenv("FILE_TO_OPEN", Path("devdata") / "test.txt").absolute())
FONT_FAMILY = os.getenv("NOTEPAD_FONT_NAME", "Times New Roman")
FONT_STYLE = os.getenv("NOTEPAD_FONT_STYLE", "Regular")
FONT_SIZE = os.getenv("NOTEPAD_FONT_SIZE", "12")
@setup
def open_close_notepad(task):
"""Opens Notepad for automation, then closes it at the end."""
desktop.windows_run(f"notepad.exe {TEXT_FILE}")
yield
desktop.close_windows(LOCATOR_NOTEPAD)
def _set_property(unique_id: str, value: str):
field = window.find(f'id:"{unique_id}"')
field.click()
field.send_keys("{DOWN}", wait_time=0.5)
desktop.send_keys(value, send_enter=True, wait_time=0.5)
def _select_property(unique_id: str, value: str):
field = window.find(f'id:"{unique_id}"')
field.select(value)
def change_font_settings(family: str, style: str, size: str):
ver = desktop.get_win_version() # slightly different automation based on Windows version
# Opens the Edit menu and goes into Font.
if ver == "11":
window.send_keys("{LAlt}e")
window.send_keys("{LAlt}o", wait_time=3) # wait for the settings to load
else:
window.send_keys("{LAlt}of", wait_time=1)
# Now set different values for the font family, style and size.
if ver == "11":
_set_property("FontFamilyComboBox", family) # doesn't work with selection
_select_property("FontStyleComboBox", style)
_set_property("FontSizeComboBox", size) # doesn't work with selection
window.click('name:"Back"') # goes back and automatically saves the settings
else:
# Selection works everywhere now.
_select_property("1136", family)
_select_property("1137", style)
_select_property("1138", size)
desktop.send_keys("{Enter}") # saves the new settings
def write_text_in_editor(text: str):
# Sets text directly inside the text editing box of Notepad.
text_edit = window.find('regex:"Text (E|e)ditor"')
text_edit.set_value(text)
@task
def automate_notepad():
"""Writes some text in Notepad after changing the font."""
global window
window = windows.find_window(LOCATOR_NOTEPAD)
change_font_settings(FONT_FAMILY, FONT_STYLE, FONT_SIZE)
write_text_in_editor(f"Timestamp: {datetime.now()}")
window.send_keys("{Ctrl}s") # saves the new content