Jump to content

Recommended Posts

Posted (edited)

I'm having a bit of an issue getting data from an ATL:SysListView32 class on a Window. AutoIt Window Info does not seem to be able to detect the text in it. I've searched the forums and the only alternatives I can find have had their links broken from forum conversions or are from 2012 or earlier. Is there a specific way I can do this or an alternative tool I can try?

 

Thanks,

-RC

Edited by rcmaehl

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects

WhyNotWin11
Cisco FinesseGithubIRC UDFWindowEx UDF

 

Posted (edited)

If the AutoIt Window Info tool isn't able to detect a control the standard way to proceed is to try with the UI Automation framework, which has been the Microsoft automation platform for the last 10 years.

You should start by checking if the UI Automation framework is able to detect your control. The easiest way to do that is to use Inspect.exe which is a Microsoft tool. You can find Inspect.exe in the bin-folder of the Windows 10 SDK (can also be installed on windows 7/8).

The Windows 10 SDK takes up about 2.5 GB of diskspace. If you only need Inspect.exe you can copy the 32 and 64 bit versions of the program and then uninstall the SDK again (installing the SDK is the only legal way to get a copy of Inspect.exe).

You can also find a copy of Inspect.exe here (the RAR-file can be extracted with 7-Zip).

Double click Inspect.exe to start it and hover the mouse pointer over the control. You'll probably see a lot of information.

In a Combo box in upper left corner of Inspect.exe you can switch between UI Automation mode (default) and MSAA mode (Microsoft Active Accessibility). MSAA mode can be useful if your program is very old (more than 10 years old).

If Inspect.exe can identify your control you can use the AutoIt implementation of the UI Automation framework to automate the control.

Here is an example with the TreeView in Windows/File Explorer left pane window.

The Intel folder on the C-drive is selected:

ZakvYMB.png

Inspect.exe can identify the TreeView item and extract a lot of information.

The Action menu is opened and the Expand action is highlighted:

2sZcrsY.png

When the Expand menu item is clicked the TreeView is expanded:

Tzq8Mwa.png

With Inspect.exe you can test if the TreeView can be automated with UI Automation code. And you can perform the test without writing a single line of code.

In the next two pictures the Intel folder is collapsed again.

SQdlGKQ.png

ST09LfV.png

With Inspect.exe you can relatively easily get an impression of whether you're able to automate the window or control. And you can figure it out before you start writing code.

The code box shows how to automate the manual procedure above with UI Automation code. Windows/File Explorer must be open and the C-drive must be expanded as shown in the last picture. The code expands and collapses the Windows folder:

#include "CUIAutomation2.au3"

Opt( "MustDeclareVars", 1 )

Example()


Func Example()
  ; Flag to handle Windows XP
  Local $fXP = ( @OSVersion = "WIN_XP" )

  ; Get window handle for Windows/File Explorer on XP, Vista, 7, 8, 10
  Local $hWindow = WinGetHandle( "[REGEXPCLASS:^(Cabinet|Explore)WClass$]" )
  If Not $hWindow Then Return ConsoleWrite( "$hWindow ERR" & @CRLF )
  ConsoleWrite( "$hWindow OK" & @CRLF )

  ; Activate the window
  WinActivate( $hWindow )

  ; Create UI Automation object
  Local $oUIAutomation = ObjCreateInterface( $sCLSID_CUIAutomation, $sIID_IUIAutomation, $dtagIUIAutomation )
  If Not IsObj( $oUIAutomation ) Then Return ConsoleWrite( "$oUIAutomation ERR" & @CRLF )
  ConsoleWrite( "$oUIAutomation OK" & @CRLF )

  ; Get UI Automation element from window handle
  Local $pWindow, $oWindow
  $oUIAutomation.ElementFromHandle( $hWindow, $pWindow )
  $oWindow = ObjCreateInterface( $pWindow, $sIID_IUIAutomationElement, $dtagIUIAutomationElement )
  If Not IsObj( $oWindow ) Then Return ConsoleWrite( "$oWindow ERR" & @CRLF )
  ConsoleWrite( "$oWindow OK" & @CRLF )

  ; Condition to find the Windows folder in the TreeView
  ; We use two conditions to find the Windows folder
  ; A condition to find the folder as a TreeView item
  ; A condition to find the folder by name (Windows)

  ; TreeView item
  Local $pCondition1
  $oUIAutomation.CreatePropertyCondition( $UIA_ControlTypePropertyId, $UIA_TreeItemControlTypeId, $pCondition1 )
  If Not $pCondition1 Then Return ConsoleWrite( "$pCondition1 ERR" & @CRLF )
  ConsoleWrite( "$pCondition1 OK" & @CRLF )

  ; Folder name
  Local $pCondition2 ; Note that $UIA_NamePropertyId ia a CASE SENSITIVE condition
  $oUIAutomation.CreatePropertyCondition( $UIA_NamePropertyId, ( $fXP ? "WINDOWS" : "Windows" ), $pCondition2 )
  If Not $pCondition2 Then Return ConsoleWrite( "$pCondition2 ERR" & @CRLF )
  ConsoleWrite( "$pCondition2 OK" & @CRLF )

  ; And condition
  Local $pCondition
  $oUIAutomation.CreateAndCondition( $pCondition1, $pCondition2, $pCondition )
  If Not $pCondition Then Return ConsoleWrite( "$pCondition ERR" & @CRLF )
  ConsoleWrite( "$pCondition OK" & @CRLF )

  ; Find folder
  Local $pFolder, $oFolder
  $oWindow.FindFirst( $TreeScope_Descendants, $pCondition, $pFolder )
  $oFolder = ObjCreateInterface( $pFolder, $sIID_IUIAutomationElement, $dtagIUIAutomationElement )
  If Not IsObj( $oFolder ) Then Return ConsoleWrite( "$oFolder ERR" & @CRLF )
  ConsoleWrite( "$oFolder OK" & @CRLF )

  If Not $fXP Then
    ; For visual verification
    ; XP: SetFocus expands the folder
    $oFolder.SetFocus() ; Not necessary
    ConsoleWrite( "SetFocus OK" & @CRLF )
  EndIf

  ; ExpandCollapse pattern object
  ; Pattern objects are used for ACTIONS
  Local $pExpandCollapse, $oExpandCollapse
  $oFolder.GetCurrentPattern( $UIA_ExpandCollapsePatternId, $pExpandCollapse )
  $oExpandCollapse = ObjCreateInterface( $pExpandCollapse, $sIID_IUIAutomationExpandCollapsePattern, $dtagIUIAutomationExpandCollapsePattern )
  If Not IsObj( $oExpandCollapse ) Then Return ConsoleWrite( "$oExpandCollapse ERR" & @CRLF )
  ConsoleWrite( "$oExpandCollapse OK" & @CRLF )

  ; Wait 5 seconds
  Sleep( 5000 )

  ; Expand folder
  $oExpandCollapse.Expand()
  ConsoleWrite( @CRLF & "Expand OK" & @CRLF )

  ; Wait 5 seconds
  Sleep( 5000 )

  ; Collapse folder
  $oExpandCollapse.Collapse()
  ConsoleWrite( @CRLF & "Collapse OK" & @CRLF )
EndFunc

Output in SciTE console:

$hWindow OK
$oUIAutomation OK
$oWindow OK
$pCondition1 OK
$pCondition2 OK
$pCondition OK
$oFolder OK
SetFocus OK
$oExpandCollapse OK

Expand OK

Collapse OK

The code is tested on Windows 10, 7 and XP. The zip file contains the example code and CUIAutomation2.au3. Run the code in SciTE.

Explorer.7z

 

An alternative to Inspect.exe is the simplespy.au3 script which is included in UIA_V0_63.zip in bottom of first post in the UI Automation thread.

Another alternative to both Inspect.exe and simplespy.au3 is UIASpy - UI Automation Spy Tool. Examples of UIASpy use are found in Using UI Automation Code in AutoIt.

With the code in post 71 of the UI Automation thread you can print information in SciTE console about the controls in a window. The information is printed in a hierarchical structure like a treeview.

Edited by LarsJ
New image links
  • 3 years later...
Posted (edited)

Thanks @LarsJ but i'm actually having a problem running the script on my Win10 (64). I receive the following output in SciiTE:

 

$hWindow OK
$oUIAutomation OK
$oWindow OK
$pCondition1 OK
$pCondition2 OK
$pCondition OK
$oFolder ERR
>Exit code: 0    Time: 0.719

 

It's failing here:

; Find folder
  Local $pFolder, $oFolder
  $oWindow.FindFirst( $TreeScope_Descendants, $pCondition2, $pFolder )
  $oFolder = ObjCreateInterface( $pFolder, $sIID_IUIAutomationElement, $dtagIUIAutomationElement )
  If Not IsObj( $oFolder ) Then Return ConsoleWrite( "$oFolder ERR" & @CRLF )
  ConsoleWrite( "$oFolder OK" & @CRLF )

 

Sorry about that. I can't figure out what's happening.

Edited by NassauSky
Posted

It works for me : Win 10 (64)

$hWindow OK
$oUIAutomation OK
$oWindow OK
$pCondition1 OK
$pCondition2 OK
$pCondition OK
$oFolder OK
SetFocus OK
$oExpandCollapse OK
Expand OK
Collapse OK

 

 

  • 2 years later...
Posted

I was getting

$oFolder ERR

Fixed it by EXPANDING the folder tree of the C:\ drive before running the script (in the folder tree pane on the left, click the small arrow next to C drive).

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...