| 1 | """Press Enter at the edges of and inside equations and links in OneNote 2010 on a |
| 2 | Rust-written page and capture what it stores. The oracle for the canvas editor's Enter. |
| 3 | |
| 4 | Usage: native_enter.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 text(value): |
| 19 | return 'SendText("%s")' % value.replace('"', '""') |
| 20 | |
| 21 | |
| 22 | def interaction(name, output): |
| 23 | lines = [ |
| 24 | # Enter at an equation's start moves it to a paragraph of its own. |
| 25 | text('Before '), 'Send("!=")', 'Sleep(600)', text('y^2+1'), 'Send(" ")', 'Sleep(700)', |
| 26 | 'Send("{Home}")', 'Send("{Right 7}")', 'Sleep(300)', 'Send("{Enter}")', 'Sleep(600)', |
| 27 | # Enter inside the equation's own row breaks its line. |
| 28 | 'Send("{End}")', 'Send("{Left 2}")', 'Sleep(300)', 'Send("{Enter}")', 'Sleep(600)', |
| 29 | # Enter inside a fraction's denominator makes it an equation array. |
| 30 | 'Send("{End}{Enter}")', 'Send("!=")', 'Sleep(600)', text('(a+b)/(c+d)'), 'Send(" ")', |
| 31 | 'Sleep(700)', 'Send("{Left 3}")', 'Sleep(300)', 'Send("{Enter}")', 'Sleep(600)', |
| 32 | # Enter at a link's start moves the link down whole (inside one, it follows the link). |
| 33 | 'Send("^{End}{Enter}")', text('Select me please'), 'Send("+^{Left}+^{Left}")', 'Send("^k")', |
| 34 | 'Sleep(1500)', text('https://example.com/selected'), 'Send("{Enter}")', 'Sleep(800)', |
| 35 | 'Send("{End}")', 'Send("{Left 9}")', 'Sleep(300)', 'Send("{Enter}")', 'Sleep(600)', |
| 36 | ] |
| 37 | page = ET.parse(sorted((output / 'read').glob('page-*.xml'))[0]).getroot().attrib['ID'] |
| 38 | script = '''ComObject("OneNote.Application").NavigateTo("PAGE_ID", "", false) |
| 39 | WinWait("ahk_exe ONENOTE.EXE", , 30) |
| 40 | WinActivate("ahk_exe ONENOTE.EXE") |
| 41 | WinWaitActive("ahk_exe ONENOTE.EXE", , 30) |
| 42 | WinMaximize("ahk_exe ONENOTE.EXE") |
| 43 | Sleep(3000) |
| 44 | CoordMode("Mouse", "Screen") |
| 45 | Click(200, 140) |
| 46 | Sleep(500) |
| 47 | Send("{End}") |
| 48 | Sleep(300) |
| 49 | Send("{Enter}") |
| 50 | Sleep(500) |
| 51 | %s |
| 52 | Sleep(2000) |
| 53 | ''' % '\n'.join(lines) |
| 54 | script = script.replace('PAGE_ID', page) |
| 55 | (output / 'enter.ahk').write_text(script) |
| 56 | result = windows.do_exec(script, target=name, timeout_ms=240000, shot_delay_ms=1500) |
| 57 | screenshot = result.pop('png_b64', None) |
| 58 | if screenshot: |
| 59 | (output / 'enter.png').write_bytes(base64.b64decode(screenshot)) |
| 60 | (output / 'enter.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) |