PoshCode Archive  Artifact [b9e2ded415]

Artifact b9e2ded415152dabb9e4db670496a03a8d04f77a2432ff530b70d401f9ff0949:

  • File UIAutomation-Simple.ps1 — part of check-in [4c41c862e3] at 2018-06-10 12:56:41 on branch trunk — A cleanup for @kryten68 without all that code-generation cruft. (user: Joel Bennett size: 1773)

# encoding: ascii
# api: powershell
# title: UIAutomation Simple
# description: A cleanup for @kryten68 without all that code-generation cruft.
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# function: Select-Window
# x-poshcode-id: 1227
# x-derived-from-id: 1231
# x-archived: 2012-12-26T21:25:14
# x-published: 2009-07-23T14:01:00
#
#
[Reflection.Assembly]::Load("UIAutomationClient, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
[Reflection.Assembly]::Load("UIAutomationTypes, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")

function Select-Window {
PARAM( [string]$Text="*", [switch]$Recurse,
       [System.Windows.Automation.AutomationElement]$Parent = [System.Windows.Automation.AutomationElement]::RootElement ) 
PROCESS {
   if($Recurse) {
      $Parent.FindAll( "Descendants", [System.Windows.Automation.Condition]::TrueCondition ) | 
      Where-Object { $_.GetCurrentPropertyValue([System.Windows.Automation.AutomationElementIdentifiers]::NameProperty)  -like $Text } |
      Add-Member -Name Window -Type ScriptProperty -Value {$this.GetCurrentPattern( [System.Windows.Automation.WindowPattern]::Pattern )} -Passthru
   } else {
      $Parent.FindAll( "Children", [System.Windows.Automation.Condition]::TrueCondition ) | 
      Where-Object { $_.GetCurrentPropertyValue([System.Windows.Automation.AutomationElementIdentifiers]::NameProperty)  -like $Text }|
      Add-Member -Name Window -Type ScriptProperty -Value {$this.GetCurrentPattern( [System.Windows.Automation.WindowPattern]::Pattern )} -Passthru
   }
}}

### To minimize notepad, for example
# $notepad = select-window *notepad
# $notepad | ForEach { $_.Window.SetWindowVisualState("Minimized") }