| 1 | """Type beside a file and a picture alone in their outlines in OneNote 2010 and capture what it |
| 2 | stores. The oracle for the canvas editor's provisional paragraph after an outline's objects. |
| 3 | |
| 4 | Usage: native_object_outline.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 | from native_xml import ns |
| 15 | |
| 16 | windows = runner.windows |
| 17 | |
| 18 | # Where a click lands beside each page's object, inside its outline (at 72, 108 pt). |
| 19 | CLICKS = {'File alone': (236, 301), 'Picture alone': (238, 282)} |
| 20 | |
| 21 | |
| 22 | def interaction(name, output): |
| 23 | pages = {} |
| 24 | for path in sorted((output / 'read').glob('page-*.xml')): |
| 25 | page = ET.parse(path).getroot() |
| 26 | title = page.find('one:Title/one:OE/one:T', ns) |
| 27 | if title is not None and title.text in CLICKS: |
| 28 | pages[title.text] = page.attrib['ID'] |
| 29 | for title, (x, y) in CLICKS.items(): |
| 30 | script = '''ComObject("OneNote.Application").NavigateTo("%s", "", false) |
| 31 | WinWait("ahk_exe ONENOTE.EXE", , 30) |
| 32 | WinActivate("ahk_exe ONENOTE.EXE") |
| 33 | WinWaitActive("ahk_exe ONENOTE.EXE", , 30) |
| 34 | WinMaximize("ahk_exe ONENOTE.EXE") |
| 35 | Sleep(3000) |
| 36 | CoordMode("Mouse", "Screen") |
| 37 | MouseMove(190, 260) |
| 38 | Sleep(800) |
| 39 | Click(%d, %d) |
| 40 | Sleep(500) |
| 41 | SendText("abc") |
| 42 | Sleep(2000) |
| 43 | ''' % (pages[title], x, y) |
| 44 | stem = title.lower().replace(' ', '-') |
| 45 | (output / (stem + '.ahk')).write_text(script) |
| 46 | result = windows.do_exec(script, target=name, timeout_ms=120000, shot_delay_ms=1500) |
| 47 | screenshot = result.pop('png_b64', None) |
| 48 | if screenshot: |
| 49 | (output / (stem + '.png')).write_bytes(base64.b64decode(screenshot)) |
| 50 | (output / (stem + '.json')).write_text(json.dumps(result, indent=2)) |
| 51 | if result.get('error') or result.get('exit') != 0: |
| 52 | raise RuntimeError(str(result)) |
| 53 | |
| 54 | |
| 55 | if __name__ == '__main__': |
| 56 | notebook, output = Path(sys.argv[1]), Path(sys.argv[2]) |
| 57 | runner.capture(notebook, output, expected_pages=-1, collect_notebook=True, interaction=interaction) |