PoshCode Archive  Artifact Content

Artifact 91e0d8b69b6e0f6d1510e08d50d75c1ce14bfc137a944377869af4a4afc2d432:

  • File UIAutomation-V1.ps1 — part of check-in [2c098ef84d] at 2018-06-10 14:24:27 on branch trunk — Just some examples of stuff you can do with the System.Windows.Automation namespace … for more, you’re going to want to check out http://WASP.CodePlex.com (user: unknown size: 4805)

# encoding: ascii
# api: powershell
# title: UIAutomation V1
# description: Just some examples of stuff you can do with the System.Windows.Automation namespace … for more, you’re going to want to check out http://WASP.CodePlex.com
# version: 0.1
# type: module
# license: CC0
# function: New-UIAElement
# x-poshcode-id: 826
# x-archived: 2009-02-04T17:22:10
#
#
Add-Type -AssemblyName UIAutomationClient
Add-Type -AssemblyName UIAutomationTypes
##############################################################################################################
## REQUIRES -Module Accelerators -URI http://www.poshcode.org/762
Import-Module Accelerators

$SWA = "System.Windows.Automation"
Add-Accelerator AutoElement        "$SWA.AutomationElement"            -EA SilentlyContinue
Add-Accelerator InvokePattern      "$SWA.InvokePattern"                -EA SilentlyContinue
Add-Accelerator ValuePattern       "$SWA.ValuePattern"                 -EA SilentlyContinue
Add-Accelerator TextPattern        "$SWA.TextPattern"                  -EA SilentlyContinue

Add-Accelerator Condition          "$SWA.Condition"                    -EA SilentlyContinue
Add-Accelerator AndCondition       "$SWA.TextPattern"                  -EA SilentlyContinue
Add-Accelerator OrCondition        "$SWA.TextPattern"                  -EA SilentlyContinue
Add-Accelerator NotCondition       "$SWA.TextPattern"                  -EA SilentlyContinue
Add-Accelerator PropertyCondition  "$SWA.PropertyCondition"            -EA SilentlyContinue

Add-Accelerator AutoElementIds     "$SWA.AutomationElementIdentifiers" -EA SilentlyContinue
Add-Accelerator TransformIds       "$SWA.TransformPatternIdentifiers"  -EA SilentlyContinue


$FalseCondition = [Condition]::FalseCondition
$TrueCondition  = [Condition]::TrueCondition

Add-Type -AssemblyName System.Windows.Forms
Add-Accelerator SendKeys           System.Windows.Forms.SendKeys                     -EA SilentlyContinue

function New-UIAElement {
[CmdletBinding()]
PARAM(
   [Parameter(ValueFromPipeline=$true)]
   [AutoElement]$Element
) 
PROCESS {
   $Element | Add-Member -Name Text            -Type ScriptProperty -PassThru -Value {
                        $this.GetCurrentPropertyValue([AutoElementIds]::NameProperty) 
          } | Add-Member -Name ClassName       -Type ScriptProperty -Passthru -Value { 
                        $this.GetCurrentPropertyValue([AutoElementIds]::ClassNameProperty) 
          } | Add-Member -Name FrameworkId     -Type ScriptProperty -Passthru -Value { 
                        $this.GetCurrentPropertyValue([AutoElementIds]::FrameworkIdProperty) 
          } | Add-Member -Name ProcessId       -Type ScriptProperty -Passthru -Value { 
                        $this.GetCurrentPropertyValue([AutoElementIds]::ProcessIdProperty) 
          } | Add-Member -Name ControlTypeL18D -Type ScriptProperty -Passthru -Value { 
                        $this.GetCurrentPropertyValue([AutoElementIds]::LocalizedControlTypeProperty) 
          }
}
}

function Select-Window {
[CmdletBinding()]
PARAM(
   [Parameter()]
   [Alias("Name")]
   [string]$Text="*"
,
   [Parameter()]
   [string]$ClassName="*"
,
   [Parameter(ValueFromPipeline=$true)]
   [AutoElement]$Parent = [AutoElement]::RootElement
) 
   PROCESS {
      $Parent.FindAll( "Children", $TrueCondition ) | New-UIAElement |
      Where-Object { 
         ($_.ClassName -like $ClassName) -AND
         ($_.Text -like $Text) }
      
   }
}

function formatter  { END {
   $input | Format-Table @{l="Text";e={$_.Text.SubString(0,25)}}, ClassName, FrameworkId -Auto
}}


function Invoke-Element {
[CmdletBinding()]
PARAM(
   [Parameter(ValueFromPipeline=$true)]
   [AutoElement]$Target
)
   PROCESS {
      $Target.GetCurrentPattern([InvokePattern]::Pattern).Invoke()
   }
}

function Set-ElementText {
[CmdletBinding()]
PARAM(
   [Parameter()]
   [string]$Text
,
   [Parameter(ValueFromPipeline=$true)]
   [AutoElement]$Target
)
   PROCESS {
      $Target.SetFocus();
      $textPattern = $valuePattern = $null
      try {
         $textPattern = $Target.GetCurrentPattern([TextPattern]::Pattern)
         Write-Host "textPattern:`n$($textPattern | gm)"
      } catch { }
      try {
         $valuePattern = $Target.GetCurrentPattern([ValuePattern]::Pattern)
         Write-Host "valuePattern:`n$($valuePattern | gm)"
      } catch { }
      
      $Target.SetFocus();
      
      
      if($valuePattern) {
         $valuePattern.SetValue( $Text )
      }
      if($textPattern) {
         [SendKeys]::SendWait("^{HOME}");
         [SendKeys]::SendWait("^+{END}");
         [SendKeys]::SendWait("{DEL}");
         [SendKeys]::SendWait( $Text )
      }
   }
}