1param([Parameter(Mandatory=$true)][string]$Root, [Parameter(Mandatory=$true)][string]$CloneHost)
2Set-StrictMode -Version Latest
3$ErrorActionPreference = 'Stop'
4& "$PSScriptRoot\cold-current.ps1" -Root $Root -CloneHost $CloneHost
5$namespace = 'http://schemas.microsoft.com/office/onenote/2010/onenote'
6$app = New-Object -ComObject OneNote.Application
7$notebookId = ''
8try {
9 $app.OpenHierarchy((Join-Path $Root 'notebook'), '', [ref]$notebookId, 0)
10 $sectionId = ''
11 $app.OpenHierarchy('pictures.one', $notebookId, [ref]$sectionId, 0)
12 $deadline = [DateTime]::UtcNow.AddSeconds(30)
13 $page = $null
14 do {
15 $hierarchy = ''
16 $app.GetHierarchy($sectionId, 4, [ref]$hierarchy, 1)
17 [xml]$tree = $hierarchy
18 $pages = @($tree.SelectNodes('//*[local-name()="Page"]'))
19 if ($pages.Count -eq 1) {
20 $content = ''
21 $app.GetPageContent($pages[0].GetAttribute('ID'), [ref]$content, 3, 1)
22 [xml]$page = $content
23 if (@($page.SelectNodes('//*[local-name()="Image"]')).Count -eq 1) { break }
24 }
25 Start-Sleep -Milliseconds 250
26 } while ([DateTime]::UtcNow -lt $deadline)
27 if (@($page.SelectNodes('//*[local-name()="Image"]')).Count -ne 1) { throw 'Expected one picture on the page.' }
28 [IO.File]::WriteAllText((Join-Path $Root 'before.xml'), $content, [Text.Encoding]::UTF8)
29 $data = $page.SelectSingleNode('//*[local-name()="Image"]/*[local-name()="Data"]').InnerText
30 $image = $page.CreateElement('one', 'Image', $namespace)
31 $image.SetAttribute('format', 'png')
32 $image.SetAttribute('alt', 'Page-level picture')
33 $position = $page.CreateElement('one', 'Position', $namespace)
34 $position.SetAttribute('x', '360')
35 $position.SetAttribute('y', '240')
36 [void]$image.AppendChild($position)
37 $size = $page.CreateElement('one', 'Size', $namespace)
38 $size.SetAttribute('width', '96')
39 $size.SetAttribute('height', '72')
40 $size.SetAttribute('isSetByUser', 'true')
41 [void]$image.AppendChild($size)
42 $payload = $page.CreateElement('one', 'Data', $namespace)
43 $payload.InnerText = $data
44 [void]$image.AppendChild($payload)
45 [void]$page.DocumentElement.AppendChild($image)
46 [IO.File]::WriteAllText((Join-Path $Root 'update.xml'), $page.OuterXml, [Text.Encoding]::UTF8)
47 $app.UpdatePageContent($page.OuterXml, [DateTime]::MinValue, 1, $false)
48 $app.SyncHierarchy($notebookId)
49 $app.CloseNotebook($notebookId, $false)
50 $notebookId = ''
51} finally {
52 if ($notebookId) { $app.CloseNotebook($notebookId, $false) }
53 [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app)
54 [GC]::Collect()
55 [GC]::WaitForPendingFinalizers()
56}