I'm trying to click on paint's save button, but it's not recognizing the window controls. Am I doing something wrong? For example this code only recognizes the title of the window, but does not recognize the controls of that window.
Can anyone help?
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName UIAutomationClient
Add-Type -AssemblyName UIAutomationTypes
Add-Type -AssemblyName System.Xml.Linq
$nameProcess = "mspaint"
$windowTitle = "Untitled"
$processes = [System.Diagnostics.Process]::GetProcessesByName($nameProcess)
foreach ($process in $processes) {
if ($process.MainWindowTitle.Contains($windowTitle)) {
if ($process -ne $null) {
$root = [System.Windows.Automation.AutomationElement]::FromHandle($process.MainWindowHandle)
$elements = $root.FindAll([System.Windows.Automation.TreeScope]::Subtree, [System.Windows.Automation.Condition]::TrueCondition) | Select-Object -Unique
foreach ($item in $elements) {
if ($item -ne $null) {
$pattern = $null
if ($item.TryGetCurrentPattern([System.Windows.Automation.InvokePattern]::Pattern, [ref]$pattern)) {
write-host $item.Current.Name -ForegroundColor Green
if ($item.Current.Name -eq "Save") {
$pattern.Invoke()
}
}
}
}
}
}
}