| 1 | param( |
| 2 | [Parameter(Mandatory=$true)][string]$Root, |
| 3 | [Parameter(Mandatory=$true)][string]$Section, |
| 4 | [switch]$Edit |
| 5 | ) |
| 6 | # Shows the section file $Section of the run's notebook in OneNote, or with -Edit appends a |
| 7 | # paragraph to the first outline of its first page with one, as an add-in may in an unlocked |
| 8 | # section. |
| 9 | Set-StrictMode -Version Latest |
| 10 | $ErrorActionPreference = 'Stop' |
| 11 | $namespace = 'http://schemas.microsoft.com/office/onenote/2010/onenote' |
| 12 | $app = New-Object -ComObject OneNote.Application |
| 13 | $id = '' |
| 14 | $app.OpenHierarchy((Join-Path (Join-Path $Root 'notebook') $Section), '', [ref]$id, 0) |
| 15 | if (-not $Edit) { |
| 16 | $app.NavigateTo($id, '', $false) |
| 17 | Write-Output $id |
| 18 | exit 0 |
| 19 | } |
| 20 | $hierarchy = '' |
| 21 | $app.GetHierarchy($id, 4, [ref]$hierarchy, 1) |
| 22 | [xml]$tree = $hierarchy |
| 23 | $pages = @($tree.SelectNodes('//*[local-name()="Page"]')) |
| 24 | if ($pages.Count -eq 0) { throw 'The section shows no pages: it is still locked.' } |
| 25 | $xml = $null |
| 26 | $children = $null |
| 27 | foreach ($page in $pages) { |
| 28 | $content = '' |
| 29 | $app.GetPageContent($page.GetAttribute('ID'), [ref]$content, 0, 1) |
| 30 | [xml]$xml = $content |
| 31 | $children = $xml.SelectSingleNode('//*[local-name()="Outline"]/*[local-name()="OEChildren"]') |
| 32 | if ($children) { break } |
| 33 | } |
| 34 | if (-not $children) { throw 'No page of the section has an outline to type into.' } |
| 35 | $element = $xml.CreateElement('one', 'OE', $namespace) |
| 36 | $text = $xml.CreateElement('one', 'T', $namespace) |
| 37 | [void]$text.AppendChild($xml.CreateCDataSection('Typed by OneNote 2010 under the key.')) |
| 38 | [void]$element.AppendChild($text) |
| 39 | [void]$children.AppendChild($element) |
| 40 | $app.UpdatePageContent($xml.OuterXml, [DateTime]::MinValue, 1, $false) |
| 41 | Start-Sleep -Seconds 15 |
| 42 | Write-Output 'edited' |