All credit for the original goes to: https://github.com/Descolada/UIAutomation <<
my modded code generator:
https://github.com/samfisherirl/UIAViewer.ahk-for-UIAutomation.ahk
edit: Caveat, I love the UIAViewer original solution, I only want to build on it because my frustration all root in my previous lack of knowledge and not the tool's pitfalls.
Problems I had with UIAViewer (I love the tool, but these can be challenging for new users):
1- Too specific. 99% of the time, if I want target VSCode, or Notepad, the concern of the class, title, etc for multiple windows I BELIEVE is down stream as a new user, from just trying to get UIAutomation to work. Once you get it working, which can take hours, then you can worry about multiple windows. Notepad, Chrome, VSCode, they all include document or page details in teh title and that muddies up results (especially for ametuer users).
2 - Defaults are set to case sensitive, exact title match, and waitelementexist. I find this also to be downstream from where new users should be entering. New users should be setup with a try{} catch e{} matchmode=2 and findallby or findfirstby.
The most discouraging thing was the dozens of hours spent pouring over documentation, not knowing that a simple title or window change rendered a code inoperable.
These are just opinions and to this day, a bit of chip on my shoulder for why I wanted to make some changes.
This was all done in the last couple days, I'll outline changes, and take feedback as well as continue to work on other things and todos. Please let me know of any mistakes.
changes:
- if title > 20 characters, it splits by "-", most popular windows split "%document_name% - %program_name%"
- WinExist reduced to not include class
- Try Catch added for FindAllBy
- Remove Defaults of WaitElementExist and Match Case Sensitive
later today and this week Ill be adding:
- Robust notes for new users, including comments on each line
- options to drill down or loop through just a given control
- credit to the original creator (sorry I will add asap)
- more ideas flowing
The [1] is going to be for findallby which I will be changing. Please ignore for now.
new code (subsection which includes changes to the macro creator only)
Code: Select all
if (MacroFunction != "No function") {
RegexMatch(MacroMatchMode, "\d(?=:)", match)
MacroContent .= ",," (match ? (match=3 ? "" : match) : "RegEx") ","
MacroContent .= (CaseSensitive ? "" : "False") ","
MacroContent .= StrReplace(MacroTimeout, " ") = "10000" ? "" : MacroTimeout ","
MacroContent := MacroFunction "(" MacroContent ")"
MacroContent := RegexReplace(MacroContent, ",*\)$", ")")
if MacroElementName
MacroContent := MacroElementName " := " . MacroElementName . "." MacroContent
if (MacroAction = "SetValue")
MacroContent .= ".Value := """ DDLMacroActionValue """"
else if (MacroAction != "Do nothing")
MacroContent .= "`n" . "try {`n " . MacroElementName . "[1]." MacroAction (SubStr(MacroAction, 0, 1) = ")" ? "" : "()") "`n}`n catch e{ `n" . MacroElementName . "." MacroAction (SubStr(MacroAction, 0, 1) = ")" ? "" : "()`n}")
}
if (Stored.WinClass != "#32768") && !(RegexMatch(ReverseContent(PreviousContent), "m`n)WinExist\(""(.*) ahk_exe (.*) ahk_class (.*)""\)$", match) && (match1 = Stored.WinTitle) && (match2 = Stored.WinExe) && (match3 = Stored.WinClass)) {
WinGet, dl, ID, Stored.WinTitle
wingetter=
(LTrim
WinGet, %MacroElementName%, ID,
)
if (StrLen(Stored.WinTitle) > 20) ;if title is more than 20 characters
{
try {
ar := StrSplit(Stored.WinTitle, "-")
indx := ar.MaxIndex()
Stored.WinTitle := ar[indx]
}
}
wingetter .= " " . Stored.WinTitle "`n"
MacroContent := wingetter
. "WinActivate, ahk_id %" . MacroElementName . "%`n"
. "WinWaitActive, ahk_id %" . MacroElementName . "%`n"
. MacroElementName " := UIA.ElementFromHandle(" MacroElementName ")`n`n"
. MacroContent
}
new code in totality doesnt fit so here it is on the git. https://github.com/samfisherirl/UIAViewer.ahk-for-UIAutomation.ahk/blob/main/UIAViewer.ahk
here's the section fo original code that got changed.
Code: Select all
~PrintScreen::
global DDLMacroActionValue
Gui Main: Default
GuiControlGet, FocusedTab,, TabsMain
if (FocusedTab != "Macro creator")
return
if IsObject(Stored.Element) {
GuiControlGet, PreviousContent,, EditMacroContent
GuiControlGet, MacroFunction,, DDLMacroFunction
GuiControlGet, MacroMatchMode,, DDLMacroMatchMode
GuiControlGet, CaseSensitive,, CBMacroCaseSensitive
GuiControlGet, MacroElementName,, EditMacroElementName
GuiControlGet, MacroTimeout,, EditMacroTimeout
GuiControlGet, MacroAction,, DDLMacroAction
MacroContent := """ControlType=" UIA_Enum.UIA_ControlTypeId(Stored.Element.CurrentControlType) ((elName := Stored.Element.CurrentName) ? " AND Name='" SanitizeInput(elName) "'" : "") ((elAID := Stored.Element.CurrentAutomationId) ? " AND AutomationId='" SanitizeInput(elAID) "'" : "") """"
if (MacroFunction != "No function") {
RegexMatch(MacroMatchMode, "\d(?=:)", match)
MacroContent .= ",," (match ? (match=3 ? "" : match) : "RegEx") ","
MacroContent .= (CaseSensitive ? "" : "False") ","
MacroContent .= StrReplace(MacroTimeout, " ") = "10000" ? "" : MacroTimeout ","
MacroContent := MacroFunction "(" MacroContent ")"
MacroContent := RegexReplace(MacroContent, ",*\)$", ")")
if MacroElementName
MacroContent := MacroElementName "." MacroContent
if (MacroAction = "SetValue")
MacroContent .= ".Value := """ DDLMacroActionValue """"
else if (MacroAction != "Do nothing")
MacroContent .= "." MacroAction (SubStr(MacroAction, 0, 1) = ")" ? "" : "()")
}
if (Stored.WinClass != "#32768") && !(RegexMatch(ReverseContent(PreviousContent), "m`n)WinExist\(""(.*) ahk_exe (.*) ahk_class (.*)""\)$", match) && (match1 = Stored.WinTitle) && (match2 = Stored.WinExe) && (match3 = Stored.WinClass)) {
MacroContent := MacroElementName " := WinExist("""Stored.WinTitle " ahk_exe " Stored.WinExe " ahk_class " Stored.WinClass """)`n"
. "WinActivate, ahk_id %" MacroElementName "%`n"
. "WinWaitActive, ahk_id %" MacroElementName "%`n"
. MacroElementName " := UIA.ElementFromHandle(" MacroElementName ")`n`n"
. MacroContent
}
GuiControl,, EditMacroContent, % PreviousContent ? PreviousContent "`n`n" MacroContent : MacroContent
}
return