CustomPatternClient_Word_TextRangeProvider.ps1
<#PSScriptInfo .VERSION 0.1.0 .GUID 829e5c00-db49-468c-bfa0-d9a046d6e628 .AUTHOR ashishsr@microsoft.com .COMPANYNAME Microsoft Corp. .COPYRIGHT .TAGS .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES #> <# .SYNOPSIS This script is used to exercise the methods of ITextRange custom pattern .DESCRIPTION This script will provide the support for some custom attributes of text range like PageNumber, ColumnNumber LineNumber, SectionNumber, BookMark. This will also provide support for different text range move APIs for sentence unit. .EXAMPLE CustomPatternClient_Word_TextRangeProvider.ps1 -docName Test.docx -method CustomAttribute .EXAMPLE CustomPatternClient_Word_TextRangeProvider.ps1 -docName Test.docx -method MoveBySentence .EXAMPLE CustomPatternClient_Word_TextRangeProvider.ps1 -docName Test.docx -method MoveBySentence -units 2 .EXAMPLE CustomPatternClient_Word_TextRangeProvider.ps1 -docName Test.docx -method MoveEndpointBySentence -units 2 -endpoint end .PARAMETER docName Name of the document with extension. It is used to identify the window root. .PARAMETER method can be any one of the following: CustomAttribute - Get all the custom attribute information for text range of current selection. MoveBySentence - Move the current selected text range by sentence and specified units count (default unit count 1). MoveEndpointBySentence - Move the specified endpoint of selected text range by sentence and units count. (default unit count 1 and endpoint end). ExpandToSentence - Expand current selected text range to sentence boundary. .PARAMETER units Count of unit by which a text range should be moved. .PARAMETER endpoint EndPoint of the text range which should be moved. #> Param( [Parameter(Mandatory=$True)][string]$docName, [Parameter(Mandatory=$True)][string]$method, [Parameter(Mandatory=$false)][int]$units, [Parameter(Mandatory=$false)][string]$endpoint ) # Pattern Guids [Guid] $IID_ITextRangeCustomProvider = "93514122-ff04-4b2c-a4ad-4ab04587c129" # Method Guids [Guid] $IID_GetCustomAttributeValue = "081aca91-32f2-46f0-9fb9-017038bc45f8" [Guid] $IID_MoveBySentence = "F39655AC-133A-435B-A318-C197F0D3D203" [Guid] $IID_MoveEndpointBySentence = "368E89A2-1BC2-4402-8C58-33C63ECFFA3B" [Guid] $IID_ExpandToEnclosingSentence = "98FE8B34-F317-459A-9627-21123EA95BEA" #hash table to map method to method id $methodToMethodId = @{ "CustomAttribute" = $IID_GetCustomAttributeValue "MoveBySentence" = $IID_MoveBySentence "MoveEndpointBySentence" = $IID_MoveEndpointBySentence "ExpandToSentence" = $IID_ExpandToEnclosingSentence } if ($null -eq $methodToMethodId[$method]) { Throw "Invalid method $method specified" } #hash lookup to map endpoints $endPointNames = @{ "start" = 0 "end" = 1 } Write-Output "Setting up module..." # Setup Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted Install-Module -Name YellowBox -Scope CurrentUser -MinimumVersion 0.0.2.0 | Import-Module # Find Word Root Node which should have text pattern # root element is the top level Word element with control type as Document $windowName = "'$docName - Word'" $rootNodeName = "'$docName'" $rootNodeFullPath = "Window[@Name = " + $windowName + "]/Pane/Pane[@Name=" + $rootNodeName + "]/Document[@Name=" + $rootNodeName + "]" $rootElement = (slx $rootNodeFullPath) if ($rootElement -eq $null) { Throw "Failed to find Word root node" } # Root node's text pattern and it's current selection $textRange = $rootElement.GetPattern([YellowBox.PatternId]::Text).Selection[0] if ($textRange -eq $null) { Throw "Failed to find root node text pattern current selection" } # Get the custom pattern $customPattern = [YellowBox.Client.ExtensionMethodContainer]::new() $rootElement.CallExtensionMethod($IID_ITextRangeCustomProvider, <# out #> $customPattern) if ($IID_GetCustomAttributeValue -eq $methodToMethodId[$method]) { Write-Output "Getting custom arribute for current selected range" $lineNumber = [YellowBox.Client.ExtensionMethodArgument]::new() $customPattern.CallExtensionMethod($IID_GetCustomAttributeValue, $textRange, <# in #> 0, <# out #> $lineNumber) $pageNumber = [YellowBox.Client.ExtensionMethodArgument]::new() $customPattern.CallExtensionMethod($IID_GetCustomAttributeValue, $textRange, <# in #> 1, <# out #> $pageNumber) $columnNumber = [YellowBox.Client.ExtensionMethodArgument]::new() $customPattern.CallExtensionMethod($IID_GetCustomAttributeValue, $textRange, <# in #> 2, <# out #> $columnNumber) $sectionNumber = [YellowBox.Client.ExtensionMethodArgument]::new() $customPattern.CallExtensionMethod($IID_GetCustomAttributeValue, $textRange, <# in #> 3, <# out #> $sectionNumber) $bookMark = [YellowBox.Client.ExtensionMethodArgument]::new() $customPattern.CallExtensionMethod($IID_GetCustomAttributeValue, $textRange, <# in #> 4, <# out #> $bookMark) if ([string]::IsNullOrEmpty($bookMark.Value)) { $bookMark.Value = "No BookMark found" } Write-Host "PageNumber:$($pageNumber.Value) LineNumber:$($lineNumber.Value) ColumnNumber:$($columnNumber.Value) SectionNumber:$($sectionNumber.Value) BookmarkName:$($bookMark.Value)" } elseif ($IID_MoveBySentence -eq $methodToMethodId[$method]) { if (0 -eq $units) { Write-Output "Units set as default 1" $units = 1 } Write-Output "Moving current selection by sentence with units = $($units)" $unitsMoved = [YellowBox.Client.ExtensionMethodArgument]::new() $customPattern.CallExtensionMethod($IID_MoveBySentence, $textRange, $units <# no of units In #>, $unitsMoved <# out #>) Write-Host "Text Range Moved by units = $($unitsMoved.Value)" $textRange.Select(); } elseif ($IID_MoveEndpointBySentence -eq $methodToMethodId[$method]) { if ([string]::IsNullOrEmpty($endpoint)) { Write-Output "Setting default endpoint to move = start" $endpoint = "start"; } if (0 -eq $units) { Write-Output "Units set as default 1" $units = 1 } Write-Output "EndPoint to be Moved $($endPointNames[$endpoint])" Write-Output "Moving current selection's $($endpoint) endpoint by sentence with units = $($units)" $unitsMoved = [YellowBox.Client.ExtensionMethodArgument]::new() $customPattern.CallExtensionMethod($IID_MoveEndpointBySentence, $textRange, $endPointNames[$endpoint] <# start EndPoint In#>, $units <# no of units In #>, $unitsMoved <# out #>) Write-Host "Text Range Moved by units = $($unitsMoved.Value)" $textRange.Select(); } elseif ($IID_ExpandToEnclosingSentence -eq $methodToMethodId[$method]) { Write-Output "Expanding current selection to sentence" $customPattern.CallExtensionMethod($IID_ExpandToEnclosingSentence, $textRange) $textRange.Select(); } |