r/PowerShell • u/Deep_Ad1959 • Jun 05 '26
Misc automating apps with no api: UI Automation tree vs sendkeys and pixel matching
For years my move for an app with no api was SendKeys plus Start-Sleep and hope. Works fine until the window opens half a second slower, or someone runs it on a laptop with different DPI, and suddenly the script is typing into the wrong field. Pixel matching had the same problem with a fancier coat on.
what actually fixed it was leaning on the UI Automation tree instead of screen coordinates. [System.Windows.Automation] is clunky from PowerShell, no argument there, but once you locate elements by control type and name instead of position, the script stops caring where the window landed or how slow the box is that morning. Lookups are structural, so they survive layout shuffles that wreck any coordinate-based approach.
one trap I hit early: AutomationId looks like the clean stable handle to match on, but plenty of apps generate it at runtime, so it shifts between machines or even between launches. matching on role plus name held up across boxes way better than any id did.
still see a ton of AutoIt and raw SendKeys in LOB runbooks though. half wonder if the accessibility-tree route just never caught on because the .NET surface for it from powershell is so miserable to write. written with ai
fwiw the role+name over coordinates approach is what i built terminator on, you target elements like role:Button && name:Save off the accessibility tree so DPI and layout shifts stop breaking scripts, https://t8r.tech/r/scvu2kw5
3
u/DirectInvestigator66 Jun 05 '26
Says written with ai but it doesn’t read like it? Second language I guess?
Either way, seems like a good topic for discussion but I just Python for everything desktop automation so unfortunately can’t offer much of an opinion.
2
u/BirdsHaveUglyFeet Jun 05 '26
How about some examples code?
2
u/Goldarr85 Jun 05 '26
How are you locating the accessibility tree route? Are you using a tool?
1
u/420GB Jun 06 '26
If you find System.windows.automation too clunky try FlaUI library
1
u/Federal_Ad2455 Jun 05 '26
Autoit has powershell module that can do all those staff btw
1
u/My_Big_Black_Hawk Jun 06 '26
UiPath or a tool like it will solve that problem. You can setup wildcards and use multi level smart validation to check if the dialog box/item appeared before sending click. Check it out. I think there’s an alternative with similar functionality that’s free. You can also embed other scripts/languages inside the automations.
1
u/mnelson10000 Jun 05 '26
Back in the day I used to use this really amazing scripting language called WinBatch that was amazing for that kind of stuff. I just checked and it's still around, WinBatch.com. might want to check it out!