UIAutomation For Dummy's. A concept to help beginners with UIAutomation.

Post your working scripts, libraries and tools for AHK v1.1 and older
sashaatx
Posts: 333
Joined: 27 May 2021, 08:27
Contact:

UIAutomation For Dummy's. A concept to help beginners with UIAutomation.

Post by sashaatx » 07 Feb 2023, 05:09

please ignore and visit this post instead: viewtopic.php?f=6&t=113641
-
-
-

-
-
-
-
-
-
-
-

I struggled so long with UIAutomation. Its tough. I want to help people like myself who didn't know the depths of dicts and lists and all that.

I'll dig up the credit, but this is something I found, looping through elements by type or name. Highlighting the various Control Types. Making it dummy proof (me).

I love the UIAutomation Viewer but the code generator is so so. It's very tough to get good code from UIAutomation's generator without knowing what you're doing. I want to bridge that gap. Credit to the creator, he put in a lot of work, I just want to build on that.

This is just the start and by no means finished. Before i continue I wanted to test the interest of the community. Heres a vid of how it works, and the code. I have to get some sleep, Ill be working on this and checking responses.

https://streamable.com/w9fhqs


Image
image.png
image.png (32.5 KiB) Viewed 515 times

Code: Select all

#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
#SingleInstance, Force
#Include lib\UIA_Interface.ahk
#Include lib\WinTitleSearch.ahk
#Include Code_Generator.ahk
SetTitleMatchMode, 2
DetectHiddenWindows, On
; Generated by Auto-GUI 3.0.1
 

          ; MsgBox 0x3, Loop Through Elements - UIAutomation_For_Dummies, 
          
          ;Is this the correct element?`n`nFor each loop`, there will be a red 
      
          ;border-highlight around elements within the selected window. If you 
          
          ;see the element you want to click highlighted`, select Yes.


global UIA := UIA_Interface()
Gui Main: New, +LabelMain +hWndhWndMain
Gui Font, s9, Segoe UI
Gui Add, Edit, hWndhTitle vTitle x24 y80 w201 h21, WinTitle
Gui Add, Edit, hWndhValue vValue x24 y176 w202 h21, Value_or_ControlType_to_Search_for
Gui Add, DropDownList, hWndhValueOrControl vValueOrControl x24 y128 w203, Select_One||Search_by_ControlType|Search_by_Value
Gui Add, Button, hWndhBtnGo vBtnGo gBtnGo x24 y232 w75 h23, Go

Gui Show, w268 h387, Window
Return

BtnGo(CtrlHwnd, GuiEvent, EventInfo, ErrLevel := "") {
  GuiControlGet, Title
  GuiControlGet, Value
  GuiControlGet, ValueOrControl
  ;msgbox % Title Value
  ;VorC := UI.checkdropdown(ValueOrControl)
  ;UI.msg()
  msgbox % el
  UI.Main(trim(Title), ValueOrControl, Value)

}

MainEscape(GuiHwnd) {
  ExitApp
}

MainClose(GuiHwnd) {
  ExitApp
}

class UI
{

  Main(app, kind, value){
    global 
    vals := value
    handle := UI.App(app)
    kind_int := UI.checkdropdown(kind)
    if (kind_int = "1")
        action := handle.FindAllByType(value)
    else if (kind_int = "2")
        action := handle.FindAllByName(value)
    index := UI.looper(action)
    UI.write_code(index, action, kind)
  }
  
  
      write_code(index, action, kind){
        global
        if (kind = "Search_by_ControlType"){
          what_do := "FindAllByType"
        }
        if (kind = "Search_by_Value"){
          what_do := "FindAllByName"
        }
        
        
        what_to_write =
        ( LTrim
        #NoEnv
        SendMode Input
        SetWorkingDir `%A_ScriptDir`% 
        #Include  lib\UIA_Interface.ahk
        SetTitleMatchMode, 2
        UIA := UIA_Interface()
        el := WinExist("%PID%")

        WinActivate, %PID%
        el := UIA.ElementFromHandle(el)
        di := el.%what_do%("%vals%")
        di[%index%].Click()
        )
        Gui, Destroy 
        GenerateCode(what_to_write)
      }

      looper(action){
        global
        loop % action.Length() {

          cur := action[A_Index]
          pos := cur.GetCurrentPos()
          str:="<= x -- y =>"
          cur.Highlight(2000) ; 1s
          ;MsgBox, % A_Index " - index -" pos.x str pos.y cur

          MsgBox 0x3, Loop Through Elements - UIAutomation_For_Dummies, Is this the correct element?`n`nFor each loop`, there will be a red border-highlight around elements within the selected window. If you see the element you want to click highlighted`, select Yes.

          IfMsgBox Yes, {
            
            return A_Index
          } Else IfMsgBox No, {
            continue
          } Else IfMsgBox Cancel, {
            return 0
          }

        }
      }
      
      Post_Loop_Index_Handler(index){
        if (index > 0){
          
        }
        else {
          msgbox, no element found or canceled 
        }
      }

      App(app){
        global
        loop, 5
        {
          try {
            PID := WinTitleSearch("steam", "PID")
            el := UIA.ElementFromHandle(WinTitleSearch("steam", "PID"))
            if (el)
              break
            else
            {
              el := UIA.ElementFromHandle(WinExist("ahk_exe " app))
            }
          }
        }
        ; el := WinExist(app)
        WinActivate, ahk_id %el%
        el := UIA.ElementFromHandle(el)
        return el
      }
      
      

      msg(){
        global
        Gui +OwnDialogs
        MsgBox 0x4, Is this correct?, Is this correct?
        IfMsgBox Yes, {

        } Else IfMsgBox No, {

        }
      }

      checkdropdown(ValueOrControl){
        
        if (ValueOrControl = "Search_by_ControlType")
        {
          return "1"
        }
        if (ValueOrControl = "Search_by_Value")
        {
          return "2"
        }
        else
        {
          msgbox, error no selection from drop down
          return "0"
        }
        return kind
      }
}
code generator

Code: Select all

; Generated by Auto-GUI 3.0.1
#SingleInstance Force
#NoEnv
SetWorkingDir %A_ScriptDir%
SetBatchLines -1

GenerateCode(val){
  Gui Font, s9, Segoe UI
  Gui Add, Edit, x32 y16 w244 h334, %val%
  Gui Add, Button, hWndhBtnCopy gxxx x40 y376 w82 h28, Copy

  Gui Show, w309 h420, Window
  Return

  xxx:
  return
  GuiEscape:
  GuiClose:
      ExitApp
  }
winttitlesearch viewtopic.php?f=6&t=113584
Last edited by sashaatx on 08 Feb 2023, 08:42, edited 3 times in total.
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

sashaatx
Posts: 333
Joined: 27 May 2021, 08:27
Contact:

Re: UIAutomation For Dummy's. A concept to help beginners with UIAutomation.

Post by sashaatx » 07 Feb 2023, 19:59

fixed gif
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

Post Reply

Return to “Scripts and Functions (v1)”