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 $images = @($page.SelectNodes('//*[local-name()="Image"]'))
28 if ($images.Count -ne 1) { throw 'Expected one picture on the page.' }
29 [IO.File]::WriteAllText((Join-Path $Root 'before.xml'), $content, [Text.Encoding]::UTF8)
30 $image = $images[0]
31 $image.SetAttribute('alt', 'Resized by OneNote')
32 $existing = $image.SelectSingleNode('*[local-name()="Size"]')
33 if ($existing) { [void]$image.RemoveChild($existing) }
34 $size = $page.CreateElement('one', 'Size', $namespace)
35 $size.SetAttribute('width', '144')
36 $size.SetAttribute('height', '108')
37 $size.SetAttribute('isSetByUser', 'true')
38 [void]$image.PrependChild($size)
39 [IO.File]::WriteAllText((Join-Path $Root 'update.xml'), $page.OuterXml, [Text.Encoding]::UTF8)
40 $app.UpdatePageContent($page.OuterXml, [DateTime]::MinValue, 1, $false)
41 $app.SyncHierarchy($notebookId)
42 $app.CloseNotebook($notebookId, $false)
43 $notebookId = ''
44} finally {
45 if ($notebookId) { $app.CloseNotebook($notebookId, $false) }
46 [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app)
47 [GC]::Collect()
48 [GC]::WaitForPendingFinalizers()
49}