PoshCode Archive  Check-in [69b61e2624]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:This is a further preview of what you can do with System.Windows.UIAutomation — The new WASP module is coming…
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:69b61e2624e1d3edf1c32c2bd618ab13dd2eb9a35aa15bfca5f78809b9a75969
User & Date: Joel Bennett 2018-06-10 14:27:59
Context
2018-06-10
14:28
Copy Data between Folders including a Progressbar check-in: 9a83e1547e user: unknown tags: trunk
14:27
This is a further preview of what you can do with System.Windows.UIAutomation — The new WASP module is coming… check-in: 69b61e2624 user: Joel Bennett tags: trunk
14:27
This script uses the text and XML PowerShell help files to generate HTML help for all PowerShell Cmdlets, PSProviders, and “about” topics. the help topics are compiled into a .chm file using HTML Help Workshop. check-in: 52b5fa3fd6 user: Jeff Hillman tags: trunk
Changes

Changes to UIAutomation.ps1.

1
2
3
4
5
6

7
8
9

10

11
12
13
14
15
16
17
..
27
28
29
30
31
32
33
34
35


36
37
38
39
40


41



42
43
44















































45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

68
69
70
71
72
73
74
75
76
77
78
79






80
81
82
83
84
85
86
87
88
89
90
91






92
93
94
95








96
97
98
99

100

101
102
103
104
105
106
107
# encoding: ascii
# api: powershell
# title: UIAutomation
# description: This is just a preview of what you can do with System.Windows.UIAutomation — Really you’re going to want to use the WASP module for stuff like this.
# version: 0.1
# type: module

# license: CC0
# function: New-UIAElement
# x-poshcode-id: 922

# x-archived: 2009-03-11T04:23:36

#
#
#                                                                                                   #
# Select-Window Notepad | Remove-Window -passthru                                                   #
# ## And later ...                                                                                  #
# Select-Window Notepad | Select-ChildWindow | Send-Keys "%N"                                       #
# ## OR ##                                                                                          #
................................................................................
# cp C:\Users\Joel\Projects\PowerShell\Wasp\trunk\WASP\bin\Debug\Wasp.dll .\Modules\WASP\           #
# Import-Module WASP

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

Add-Type -AssemblyName UIAutomationClient
Add-Type -AssemblyName UIAutomationTypes



$SWA = "System.Windows.Automation"

Import-Module Accelerators
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
}}




|
|

>


|
>
|
>







 







|
|
>
>





>
>
|
>
>
>
|
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|
|
|










|








>
|
|
|
|
|
|
|
|
|
|
|
|
>
>
>
>
>
>












>
>
>
>
>
>




>
>
>
>
>
>
>
>
|
|
|
|
>
|
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
..
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# encoding: ascii
# api: powershell
# title: UIAutomation
# description: This is a further preview of what you can do with System.Windows.UIAutomation — The new WASP module is coming…
# version: 1.3
# type: module
# author: Joel Bennett
# license: CC0
# function: New-UIAElement
# x-poshcode-id: 990
# x-derived-from-id: 1227
# x-archived: 2015-11-24T02:09:55
# x-published: 2009-04-01T22:31:00
#
#
#                                                                                                   #
# Select-Window Notepad | Remove-Window -passthru                                                   #
# ## And later ...                                                                                  #
# Select-Window Notepad | Select-ChildWindow | Send-Keys "%N"                                       #
# ## OR ##                                                                                          #
................................................................................
# cp C:\Users\Joel\Projects\PowerShell\Wasp\trunk\WASP\bin\Debug\Wasp.dll .\Modules\WASP\           #
# Import-Module WASP

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

# Add-Type -AssemblyName UIAutomationClient
# Add-Type -AssemblyName UIAutomationTypes
[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")

$SWA = "System.Windows.Automation"

Import-Module Accelerators
Add-Accelerator AutoElement        "$SWA.AutomationElement"            -EA SilentlyContinue

$UIAFunctions = @{}
#  Add-Accelerator InvokePattern      "$SWA.InvokePattern"                -EA SilentlyContinue
#  Add-Accelerator ExpandPattern      "$SWA.ExpandCollapsePattern"        -EA SilentlyContinue
#  Add-Accelerator WindowPattern      "$SWA.WindowPattern"                -EA SilentlyContinue
#  Add-Accelerator TransformPattern   "$SWA.TransformPattern"             -EA SilentlyContinue
#  Add-Accelerator ValuePattern       "$SWA.ValuePattern"                 -EA SilentlyContinue
#  Add-Accelerator TextPattern        "$SWA.TextPattern"                  -EA SilentlyContinue

$Patterns = [Reflection.Assembly]::GetAssembly([System.Windows.Automation.BasePattern]).GetTypes() | 
Where {$_.BaseType -like "System.Windows.Automation.BasePattern"} 

## TODO: Write Get-SupportedPatterns or rather ... 
## Get-SupportedFunctions (to return the names of the functions for the supported patterns)
## TODO: Support all the "Properties" too
## TODO: Figure out why Notepad doesn't support SetValue
## TODO: Figure out where the menus support went

ForEach($pattern in $Patterns){
   $FullName = $pattern.FullName
   
   $pattern.GetMethods() | 
   Where { $_.DeclaringType -eq $_.ReflectedType -and !$_.IsSpecialName } | 
   ForEach {
      $Name = $($_.Name -replace 'Pattern`$')
      $Func1 = "Function:Invoke-$($Name)"
      $Func = "Function:Global:Invoke-$($Name)"
      if (test-path $Func1) { remove-item $Func1 }
      New-Item $Func -value "
      Param( $( [string]::Join( ", ", @($_.GetParameters() | % { "[$($_.ParameterType.FullName)]`$$($_.Name)" })) ) )
      Process { 
         trap { Write-Warning `"`$(`$_)`"; continue }
         `$pattern = `$_.GetCurrentPattern([$FullName]::Pattern)
         if(`$pattern) {
            `$Pattern.GetType().InvokeMember('$($_.name)','Public,Instance,InvokeMethod', `$null, `$pattern, `$PSBoundParameters.Values) 
         }
      }"
   }
   
   $pattern.GetProperties() | 
   Where { $_.DeclaringType -eq $_.ReflectedType -and $_.Name -notmatch "Cached|Current"} |
   ForEach {
      $Name = $($_.Name -replace 'Pattern`$')
      $Func1 = "Function:Get-$($Name)"
      $Func = "Function:Global:Get-$($Name)"
      if (test-path $Func1) { remove-item $Func1 }
      New-Item $Func -value "Process { 
         trap { Write-Warning `"$FullName `$_`"; continue }
         `$pattern = `$_.GetCurrentPattern([$FullName]::Pattern)
         if(`$pattern) {
            `$pattern.'$($_.name)'
         }
      }"
   }
}

Add-Accelerator Condition          "$SWA.Condition"                    -EA SilentlyContinue
Add-Accelerator AndCondition       "$SWA.AndCondition"                 -EA SilentlyContinue
Add-Accelerator OrCondition        "$SWA.OrCondition"                  -EA SilentlyContinue
Add-Accelerator NotCondition       "$SWA.NotCondition"                 -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 ControlType     -Type ScriptProperty -Passthru -Value { 
                  $this.GetCurrentPropertyValue([AutoElementIds]::LocalizedControlTypeProperty) 
    }
    
   #  $e =  
   #  ForEach($method in $UIAFunctions.GetEnumerator()) { 
      #  $e = & $method.Value $e
   #  }
   #  $e
}     
}

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

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