1"""Draw ink over an outline's text and in empty space with the mouse in OneNote 2010 on a
2Rust-written page, then capture the COM read: the oracle for handwriting held by paragraphs.
3
4Usage: native_handwriting.py NOTEBOOK_DIR OUTPUT_DIR
5"""
6import base64
7import json
8import sys
9from pathlib import Path
10import xml.etree.ElementTree as ET
11
12sys.path.insert(0, str(Path(__file__).resolve().parent))
13import native_runner as runner
14
15windows = runner.windows
16
17
18def interaction(name, output):
19 page = ET.parse(sorted((output / 'read').glob('page-*.xml'))[0]).getroot().attrib['ID']
20 script = '''ComObject("OneNote.Application").NavigateTo("PAGE_ID", "", false)
21WinWait("ahk_exe ONENOTE.EXE", , 30)
22WinActivate("ahk_exe ONENOTE.EXE")
23WinWaitActive("ahk_exe ONENOTE.EXE", , 30)
24WinMaximize("ahk_exe ONENOTE.EXE")
25Sleep(3000)
26SendMode("Event")
27CoordMode("Mouse", "Screen")
28Click(258,36)
29Sleep(600)
30Click(205,69)
31Sleep(600)
32; handwriting inside the outline, below its text line
33MouseMove(140,160)
34Click("Down")
35MouseMove(160,175,25)
36MouseMove(180,160,25)
37MouseMove(200,175,25)
38MouseMove(220,160,25)
39Click("Up")
40Sleep(300)
41MouseClickDrag("Left",240,158,300,178,25)
42Sleep(300)
43; a drawing in empty page space
44MouseMove(450,320)
45Click("Down")
46MouseMove(500,360,25)
47MouseMove(450,400,25)
48MouseMove(400,360,25)
49MouseMove(450,320,25)
50Click("Up")
51Sleep(1000)
52Click(21,74)
53Sleep(2000)
54'''.replace('PAGE_ID', page)
55 (output / 'handwriting.ahk').write_text(script)
56 result = windows.do_exec(script, target=name, timeout_ms=120000, shot_delay_ms=1500)
57 screenshot = result.pop('png_b64', None)
58 if screenshot:
59 (output / 'handwriting.png').write_bytes(base64.b64decode(screenshot))
60 (output / 'handwriting.json').write_text(json.dumps(result, indent=2))
61 if result.get('error') or result.get('exit') != 0:
62 raise RuntimeError(str(result))
63
64
65if __name__ == '__main__':
66 notebook, output = Path(sys.argv[1]), Path(sys.argv[2])
67 runner.capture(notebook, output, expected_pages=1, collect_notebook=True, interaction=interaction)