| 1 | """Draw ink over an outline's text and in empty space with the mouse in OneNote 2010 on a |
| 2 | Rust-written page, then capture the COM read: the oracle for handwriting held by paragraphs. |
| 3 | |
| 4 | Usage: native_handwriting.py NOTEBOOK_DIR OUTPUT_DIR |
| 5 | """ |
| 6 | import base64 |
| 7 | import json |
| 8 | import sys |
| 9 | from pathlib import Path |
| 10 | import xml.etree.ElementTree as ET |
| 11 | |
| 12 | sys.path.insert(0, str(Path(__file__).resolve().parent)) |
| 13 | import native_runner as runner |
| 14 | |
| 15 | windows = runner.windows |
| 16 | |
| 17 | |
| 18 | def 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) |
| 21 | WinWait("ahk_exe ONENOTE.EXE", , 30) |
| 22 | WinActivate("ahk_exe ONENOTE.EXE") |
| 23 | WinWaitActive("ahk_exe ONENOTE.EXE", , 30) |
| 24 | WinMaximize("ahk_exe ONENOTE.EXE") |
| 25 | Sleep(3000) |
| 26 | SendMode("Event") |
| 27 | CoordMode("Mouse", "Screen") |
| 28 | Click(258,36) |
| 29 | Sleep(600) |
| 30 | Click(205,69) |
| 31 | Sleep(600) |
| 32 | ; handwriting inside the outline, below its text line |
| 33 | MouseMove(140,160) |
| 34 | Click("Down") |
| 35 | MouseMove(160,175,25) |
| 36 | MouseMove(180,160,25) |
| 37 | MouseMove(200,175,25) |
| 38 | MouseMove(220,160,25) |
| 39 | Click("Up") |
| 40 | Sleep(300) |
| 41 | MouseClickDrag("Left",240,158,300,178,25) |
| 42 | Sleep(300) |
| 43 | ; a drawing in empty page space |
| 44 | MouseMove(450,320) |
| 45 | Click("Down") |
| 46 | MouseMove(500,360,25) |
| 47 | MouseMove(450,400,25) |
| 48 | MouseMove(400,360,25) |
| 49 | MouseMove(450,320,25) |
| 50 | Click("Up") |
| 51 | Sleep(1000) |
| 52 | Click(21,74) |
| 53 | Sleep(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 | |
| 65 | if __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) |