Leaderboard
Popular Content
Showing content with the highest reputation on 11/29/2023 in all areas
-
RegisterActiveObject
argumentum reacted to Andreik for a topic
I am not a fan of COM programming not because I want to avoid an ugly side or something like that but because I don't usually have to work with COM to achieve my goals. Sometimes I use UDFs that use COM programming, dictionaries or other stuffs like that but in most cases I don't need anything more than native AutoIt. So I am not against COM programming, I just don't have many projects in this area.1 point -
RegisterActiveObject
argumentum reacted to ioa747 for a topic
in the effort to understand and take advantage of what @jugador simply shares with us. I try to combine two @jugador works (Thanks for sharing) 47048-scripting-dictionary/#comment-1516044 211008-registeractiveobject/#comment-1525860 the idea is to make CrossDictionaryUDF as a communication channel, to share information between multiple scripts a first approach CrossDictionaryUDF.zip many things I used them as is. I hope in time to understand them better and reformulate them1 point -
yea... guess so.. Yea there is no need to that... will have look at that ... tnx1 point
-
I want to make the information update when selected.
ERIC_DEUCHER reacted to Andreik for a topic
As long you are happy with the result and the job it's done, everything else are just details.1 point -
I want to make the information update when selected.
ERIC_DEUCHER reacted to Andreik for a topic
Of course you can do something like this but it's not as good as the code above: #include <ListViewConstants.au3> #include <GUIConstants.au3> Global $cLastSelection = Null, $cCurrentSelection, $aRead $hMain = GUICreate('Example', 400, 400) $cListview = GUICtrlCreateListView('ID|Name', 10, 10, 380, 300) $cInput = GUICtrlCreateInput('', 10, 350, 150, 30) GUICtrlCreateListViewItem("64|Human Male", $cListview) GUICtrlCreateListViewItem("65|Human Female", $cListview) GUICtrlCreateListViewItem("66|Saiyan Male", $cListview) GUICtrlCreateListViewItem("67|Saiyan Female", $cListview) GUICtrlCreateListViewItem("68|Namekian", $cListview) GUICtrlCreateListViewItem("69|Frieza Race", $cListview) GUICtrlCreateListViewItem("6a|Majin Male", $cListview) GUICtrlCreateListViewItem("6b|Majin Female", $cListview) GUICtrlSendMsg($cListview, $LVM_SETCOLUMNWIDTH, 0, 40) GUICtrlSendMsg($cListview, $LVM_SETCOLUMNWIDTH, 1, 100) GUISetState(@SW_SHOW, $hMain) While True If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit $cCurrentSelection = GUICtrlRead($cListview) If $cCurrentSelection <> $cLastSelection Then $cLastSelection = $cCurrentSelection $aRead = StringSplit(GUICtrlRead($cCurrentSelection), '|') If IsArray($aRead) Then GUICtrlSetData($cInput, $aRead[0] > 1 ? $aRead[1] : '') EndIf WEnd1 point -
I want to make the information update when selected.
ERIC_DEUCHER reacted to Andreik for a topic
Both are correct but using -1 is not as using a specific control ID. In case you later add code to your script, using the last created control ID (-1) might lead to undesired behavior. As you can see in the second example posted (using native functions), if you intend to set these properties to the list view, logically would be to place these just after the creation of the list view. $ListView2_Auras = GUICtrlCreateListView("ID|Name", 144, 208, 154, 214) ; <<< --- this is the last created control ; If a new control is created here, the message and the font are assigned to this new control ; but since there is no control created here the following code is equivalent GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 40) ; <<< --- this is equivalent of GUICtrlSendMsg($ListView2_Auras, $LVM_SETCOLUMNWIDTH, 0, 40) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 60) ; <<< --- this is equivalent of GUICtrlSendMsg($ListView2_Auras, $LVM_SETCOLUMNWIDTH, 1, 60) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") ; <<< --- this is equivalent of GUICtrlSetFont(ListView2_Auras, 10, 400, 0, "MS Sans Serif") The safer way it's to set the intended control ID like below: $ListView2_Auras = GUICtrlCreateListView("ID|Name", 144, 208, 154, 214) ... ; If here is some more code, even if a new control is created ; the messages below and the font will apply to $ListView2_Auras ... GUICtrlSendMsg($ListView2_Auras, $LVM_SETCOLUMNWIDTH, 0, 40) GUICtrlSendMsg($ListView2_Auras, $LVM_SETCOLUMNWIDTH, 1, 60) GUICtrlSetFont($ListView2_Auras, 10, 400, 0, "MS Sans Serif")1 point -
As per help file of TCPSend : Remarks If Unicode strings need to be transmitted they must be encoded/decoded with StringToBinary()/BinaryToString().1 point
-
I want to make the information update when selected.
ERIC_DEUCHER reacted to Andreik for a topic
If you pass -1 it is like you would pass the ID of the last created control. In the example above it doesn't make any sense. $RE_aura_ID = GUICtrlCreateInput("", 16, 312, 105, 24) ; <<< --- this is the last created control GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") ;ListView GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 40) ; <<< --- LVM_SETCOLUMNWIDTH is sent to $RE_aura_ID which doesn't make any sense In my opinion it's better to pass the exact control ID so there is no confusion and to avoid possible issues if another control it's created meanwhile.1 point -
I want to make the information update when selected.
ERIC_DEUCHER reacted to Andreik for a topic
You can achieve the same result with native functions as well: #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <ListViewConstants.au3> #include <StructureConstants.au3> Opt("TrayMenuMode", 1) #Region ### START Koda GUI section ### $Form1 = GUICreate("Iniciar", 317, 608, 417, 120) Global $bone_id, $aur_id, $RE_cac_ID, $RE_aura_ID, $hListview_Bon, $hListview_Auras $Write_screen_1 = GUICtrlCreateLabel("CAC ID", 16, 64, 105, 20) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") $Write_screen_2 = GUICtrlCreateLabel("Aura ID", 40, 288, 48, 20) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") $Start_Convert = GUICtrlCreateButton("Start_Convert", 16, 432, 283, 105) $Dire_Tel = GUICtrlCreateLabel("...", 8, 552, 305, 52) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") $RE_cac_ID = GUICtrlCreateInput("", 16, 88, 105, 24) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") $RE_aura_ID = GUICtrlCreateInput("", 16, 312, 105, 24) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") ;ListView $ListView1_Bon = GUICtrlCreateListView("ID|Name", 144, 8, 154, 193) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 40) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 100) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") GUICtrlSetResizing(-1, $GUI_DOCKRIGHT) $ListView1_CAC_1 = GUICtrlCreateListViewItem("64|Human Male", $ListView1_Bon) $ListView1_CAC_2 = GUICtrlCreateListViewItem("65|Human Female", $ListView1_Bon) $ListView1_CAC_3 = GUICtrlCreateListViewItem("66|Saiyan Male", $ListView1_Bon) $ListView1_CAC_4 = GUICtrlCreateListViewItem("67|Saiyan Female", $ListView1_Bon) $ListView1_CAC_5 = GUICtrlCreateListViewItem("68|Namekian", $ListView1_Bon) $ListView1_CAC_6 = GUICtrlCreateListViewItem("69|Frieza Race", $ListView1_Bon) $ListView1_CAC_7 = GUICtrlCreateListViewItem("6a|Majin Male", $ListView1_Bon) $ListView1_CAC_8 = GUICtrlCreateListViewItem("6b|Majin Female", $ListView1_Bon) $hListview_Bon = GUICtrlGetHandle($ListView1_Bon) $ListView2_Auras = GUICtrlCreateListView("ID|Name", 144, 208, 154, 214) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 40) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 60) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") $ListView2_aura_1 = GUICtrlCreateListViewItem("0|Blue", $ListView2_Auras) $ListView2_aura_2 = GUICtrlCreateListViewItem("1|Violet", $ListView2_Auras) $ListView2_aura_3 = GUICtrlCreateListViewItem("2|Purple", $ListView2_Auras) $ListView2_aura_4 = GUICtrlCreateListViewItem("3|Pink", $ListView2_Auras) $ListView2_aura_5 = GUICtrlCreateListViewItem("4|Red", $ListView2_Auras) $ListView2_aura_6 = GUICtrlCreateListViewItem("5|Yellow", $ListView2_Auras) $ListView2_aura_7 = GUICtrlCreateListViewItem("A|Green", $ListView2_Auras) $ListView2_aura_8 = GUICtrlCreateListViewItem("F|white", $ListView2_Auras) $ListView2_aura_9 = GUICtrlCreateListViewItem("C|Empty", $ListView2_Auras) $hListview_Auras = GUICtrlGetHandle($ListView2_Auras) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY') While True Switch GUIGetMsg() Case $ListView1_CAC_1 $bone_id = 777 Case $GUI_EVENT_CLOSE, $Start_Convert Exit EndSwitch WEnd Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam) #forceref $hWnd, $iMsg, $wParam Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam) Local $hWndFrom = HWnd($tNMHDR.hWndFrom) Local $aData Switch $tNMHDR.Code Case $NM_CLICK Switch $hWndFrom Case $hListview_Bon $aData = StringSplit(GUICtrlRead(GUICtrlRead($ListView1_Bon)), '|') If IsArray($aData) Then GUICtrlSetData($RE_cac_ID, $aData[0] > 1 ? $aData[1] : '') Case $hListview_Auras $aData = StringSplit(GUICtrlRead(GUICtrlRead($ListView2_Auras)), '|') If IsArray($aData) Then GUICtrlSetData($RE_aura_ID, $aData[0] > 1 ? $aData[1] : '') EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc1 point -
I want to make the information update when selected.
ERIC_DEUCHER reacted to Melba23 for a topic
ERIC_DEUCHER, Welcome to the AutoIt forums. When you post code in future please use Code tags - see here how to do it. Then you get a scrolling box and syntax colouring as you can see above now I have added the tags. Thanks in advance for your cooperation. And looking at the code it seems that you might not yet have read the Forum rules. Please read them now - particularly the bit about not discussing game automation. As this question is purely a data handling matter I have decided to let it run, but I suggest you think carefuly before asking more questions about game-related code. M231 point -
I want to make the information update when selected.
ERIC_DEUCHER reacted to Andreik for a topic
This can be a way to do it: ;~ #include <FileConstants.au3> ;~ #include <MsgBoxConstants.au3> ;~ #include <ButtonConstants.au3> ;~ #include <EditConstants.au3> #include <GUIConstantsEx.au3> ;~ #include <GUIListBox.au3> ;~ #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <ListViewConstants.au3> #include <StructureConstants.au3> #include <GuiListView.au3> Opt("TrayMenuMode", 1) #Region ### START Koda GUI section ### $Form1 = GUICreate("Iniciar", 317, 608, 417, 120) local $bone_id, $aur_id $Write_screen_1 = GUICtrlCreateLabel("CAC ID", 16, 64, 105, 20) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") $Write_screen_2 = GUICtrlCreateLabel("Aura ID", 40, 288, 48, 20) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") $Start_Convert = GUICtrlCreateButton("Start_Convert", 16, 432, 283, 105) $Dire_Tel = GUICtrlCreateLabel("...", 8, 552, 305, 52) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") Global $RE_cac_ID = GUICtrlCreateInput("", 16, 88, 105, 24) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") Global $RE_aura_ID = GUICtrlCreateInput("", 16, 312, 105, 24) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") ;ListView GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 40) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 100) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") GUICtrlSetResizing(-1, $GUI_DOCKRIGHT) $ListView1_Bon = GUICtrlCreateListView("ID|Name", 144, 8, 154, 193) $ListView1_CAC_1 = GUICtrlCreateListViewItem("64|Human Male", $ListView1_Bon) $ListView1_CAC_2 = GUICtrlCreateListViewItem("65|Human Female", $ListView1_Bon) $ListView1_CAC_3 = GUICtrlCreateListViewItem("66|Saiyan Male", $ListView1_Bon) $ListView1_CAC_4 = GUICtrlCreateListViewItem("67|Saiyan Female", $ListView1_Bon) $ListView1_CAC_5 = GUICtrlCreateListViewItem("68|Namekian", $ListView1_Bon) $ListView1_CAC_6 = GUICtrlCreateListViewItem("69|Frieza Race", $ListView1_Bon) $ListView1_CAC_7 = GUICtrlCreateListViewItem("6a|Majin Male", $ListView1_Bon) $ListView1_CAC_8 = GUICtrlCreateListViewItem("6b|Majin Female", $ListView1_Bon) Global $hListview_Bon = GUICtrlGetHandle($ListView1_Bon) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 40) GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 60) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") $ListView2_Auras = GUICtrlCreateListView("ID|Name", 144, 208, 154, 214) $ListView2_aura_1 = GUICtrlCreateListViewItem("0|Blue", $ListView2_Auras) $ListView2_aura_2 = GUICtrlCreateListViewItem("1|Violet", $ListView2_Auras) $ListView2_aura_3 = GUICtrlCreateListViewItem("2|Purple", $ListView2_Auras) $ListView2_aura_4 = GUICtrlCreateListViewItem("3|Pink", $ListView2_Auras) $ListView2_aura_5 = GUICtrlCreateListViewItem("4|Red", $ListView2_Auras) $ListView2_aura_6 = GUICtrlCreateListViewItem("5|Yellow", $ListView2_Auras) $ListView2_aura_7 = GUICtrlCreateListViewItem("A|Green", $ListView2_Auras) $ListView2_aura_8 = GUICtrlCreateListViewItem("F|white", $ListView2_Auras) $ListView2_aura_9 = GUICtrlCreateListViewItem("C|Empty", $ListView2_Auras) Global $hListview_Auras = GUICtrlGetHandle($ListView2_Auras) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY') While True Switch GUIGetMsg() Case $ListView1_CAC_1 $bone_id = 777 Case $GUI_EVENT_CLOSE, $Start_Convert Exit EndSwitch WEnd Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam) #forceref $hWnd, $iMsg, $wParam Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam) Local $hWndFrom = HWnd($tNMHDR.hWndFrom) Local $tNMITEMACTIVATE Switch $tNMHDR.Code Case $NM_CLICK Switch $hWndFrom Case $hListview_Bon $tNMITEMACTIVATE = DllStructCreate($tagNMITEMACTIVATE, $lParam) GUICtrlSetData($RE_cac_ID, _GUICtrlListView_GetItemText($hWndFrom, $tNMITEMACTIVATE.Index)) Case $hListview_Auras $tNMITEMACTIVATE = DllStructCreate($tagNMITEMACTIVATE, $lParam) GUICtrlSetData($RE_aura_ID, _GUICtrlListView_GetItemText($hWndFrom, $tNMITEMACTIVATE.Index)) EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc By the way, I don't think you fully understand how -1 works as parameter for control IDs so it would be better to use real control IDs for now. For example you have this portion of code: $Dire_Tel = GUICtrlCreateLabel("...", 8, 552, 305, 52) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") Global $RE_cac_ID = GUICtrlCreateInput("", 16, 88, 105, 24) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") Global $RE_aura_ID = GUICtrlCreateInput("", 16, 312, 105, 24) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") ;ListView GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 40) ; <<< ----- what do you think it happens here? GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 100) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif") GUICtrlSetResizing(-1, $GUI_DOCKRIGHT) $ListView1_Bon = GUICtrlCreateListView("ID|Name", 144, 8, 154, 193)1 point -
Call a Function in compiled EXE or in AU3 script from outside
Andreik reacted to argumentum for a topic
Not very "@CreativeMind" to ask such silly question. If you need help with a script of yours, make a post in the help area.1 point -
Request for a jump-start on my project requirement - (Moved)
somdcomputerguy reacted to Andreik for a topic
If you are an experienced C/C++ programmer you don't need months to read the help file. Basically you need a small range of windows and controls functions like WinExists(), WinWait(), WinWaitActivate(), ControlClick(). ControlSend() and maybe Send(). For all these functions you have basic examples in the help file. PS: not sure why you post this thread here and not in general help forum1 point -
How to topics, 8 - 15 How to topics is a guide to use UIASpy to identify windows and controls and to create sample code to obtain information and perform actions. Topics 1 - 7: Introductory topics Topics 8 - 15: Sample code creation Top of next post (bottom of this post) Topics 16 - 17: Other How to topics How to perform UI Automation tasks How to use UIASpy to detect elements How to use UIASpy to create sample code How to create sample code through Detail info page How to create sample code through Sample code menu How to create sample code by copying listview rows How to create objects with ObjCreateInterface How to create UI Automation main objects ($oUIAutomation.GetRootElement()) How to identify and find application top window ($UIA_ClassNamePropertyId, $oUIAutomation.CreatePropertyCondition()) How to identify and find UI elements (controls) (Edit control, $UIA_AutomationIdPropertyId, $oUIElement.FindFirst()) How to get UI element property values ($UIA_NativeWindowHandlePropertyId, $UIA_BoundingRectanglePropertyId) How to create element action objects (Create $oValuePattern action object) How to get pattern property values ($UIA_ValueValuePropertyId, $oUIElement.GetCurrentPropertyValue()) How to perform element actions ($oValuePattern.SetValue()) How to create executable code How to perform a mouse click in middle of bounding rectangle How to perform a control click via PostMessage and $UIA_AutomationIdPropertyId Notepad is used as a general example: The Notepad window is identified, the Edit control is identified, some properties are extracted, the ValuePattern action object is created, the existing text in the Edit control is extracted and a new text is set. It looks like this in pseudo code: ; 8. Create UI Automation main objects Create $oUIAutomation and $oDesktop objects ; 9. Identify and find application top window Create condition to identify window through $UIA_ClassNamePropertyId Find window through $oDesktop.FindFirst() with the condition as input ; 10. Identify and find Edit control from top window Create condition to identify Edit control with $UIA_AutomationIdPropertyId Find Edit control through $oWindow.FindFirst() with the condition as input ; 11. Extract some properties from the Edit control Get $UIA_NativeWindowHandlePropertyId with $oEdit.GetCurrentPropertyValue() Get $UIA_BoundingRectanglePropertyId with $oEdit.GetCurrentPropertyValue() ; 12. Create element action object for the Edit control to set a text value Create $oValuePattern with $oEdit.GetCurrentPattern() and $UIA_ValuePatternId as input ; 13. Extract the existing text in the Edit control as a pattern property Get $UIA_ValueValuePropertyId with $oEdit.GetCurrentPropertyValue() ; 14. Perform an action on the Edit control to set a new text Set new text with $oValuePattern.SetValue() and the new text as input ; 15. Create executable code from the code generated by UIASpy Modify the somewhat generic code to be executable Open an empty Notepad and type/paste Hello into the Edit control. Open UIASpy. Click Left pane | Delete top windows to delete all treeview top windows. Place mouse cursor over Notepad title bar and press F2. Notepad and UIASpy should look like shown in the picture in topic 9. The Edit control should contain the word Hello (not shown in the picture). Let UIASpy be open throughout the entire example. In topic 15 all code will be copied from the listview Code page into SciTE editor. 8. How to create UI Automation main objects Click Sample code | Initial code | Complete code to create complete startup code. The code should look like this in the listview Code page: #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 ;#AutoIt3Wrapper_UseX64=n ; If target application is running as 32 bit code ;#AutoIt3Wrapper_UseX64=y ; If target application is running as 64 bit code #include "CUIAutomation2.au3" ; Get proper version in UIASpy Includes folder ;#include "UIA_Functions.au3" ; Can be copied from UIASpy Includes folder Opt( "MustDeclareVars", 1 ) Example() Func Example() ; 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 Desktop element Local $pDesktop, $oDesktop $oUIAutomation.GetRootElement( $pDesktop ) $oDesktop = ObjCreateInterface( $pDesktop, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oDesktop ) Then Return ConsoleWrite( "$oDesktop ERR" & @CRLF ) ConsoleWrite( "$oDesktop OK" & @CRLF ) EndFunc 9. How to identify and find application top window Click Notepad window in the treeview to return to the Detail info listview page. Select the row as shown, right-click and click Create sample code. The new code that's added to the bottom of the existing code should look like this in the listview Code page: ; --- Find window/control --- ConsoleWrite( "--- Find window/control ---" & @CRLF ) Local $pCondition0 $oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "Notepad", $pCondition0 ) If Not $pCondition0 Then Return ConsoleWrite( "$pCondition0 ERR" & @CRLF ) ConsoleWrite( "$pCondition0 OK" & @CRLF ) Local $pWindow1, $oWindow1 $oDesktop.FindFirst( $TreeScope_Descendants, $pCondition0, $pWindow1 ) $oWindow1 = ObjCreateInterface( $pWindow1, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oWindow1 ) Then Return ConsoleWrite( "$oWindow1 ERR" & @CRLF ) ConsoleWrite( "$oWindow1 OK" & @CRLF ) If you accidentally selected a wrong row in the Detail info listview page, then click Sample code | Undo code, click treeview, select the right row in the listview, right-click and click Create sample code. 10. How to identify and find UI elements (controls) Click the Edit control in the treeview. Select the row as shown, right-click and click Create sample code. New code: ; --- Find window/control --- ConsoleWrite( "--- Find window/control ---" & @CRLF ) Local $pCondition1 $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "15", $pCondition1 ) If Not $pCondition1 Then Return ConsoleWrite( "$pCondition1 ERR" & @CRLF ) ConsoleWrite( "$pCondition1 OK" & @CRLF ) Local $pEdit1, $oEdit1 $oWindow1.FindFirst( $TreeScope_Descendants, $pCondition1, $pEdit1 ) $oEdit1 = ObjCreateInterface( $pEdit1, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oEdit1 ) Then Return ConsoleWrite( "$oEdit1 ERR" & @CRLF ) ConsoleWrite( "$oEdit1 OK" & @CRLF ) 11. How to get UI element property values Extract window handle and bounding rectangle for the Edit control. The far easiest way to extract properties is through Sample code | Properties... Select the row as shown, right-click and click Create sample code. New code: ; --- Element Properties --- ConsoleWrite( "--- Element Properties ---" & @CRLF ) Local $hNativeWindowHandle1 $oEdit1.GetCurrentPropertyValue( $UIA_NativeWindowHandlePropertyId, $hNativeWindowHandle1 ) ConsoleWrite( "$hNativeWindowHandle1 = " & $hNativeWindowHandle1 & @CRLF ) Local $asBoundingRectangle1 $oEdit1.GetCurrentPropertyValue( $UIA_BoundingRectanglePropertyId, $asBoundingRectangle1 ) UIA_GetArrayPropertyValue( $asBoundingRectangle1 ) ConsoleWrite( "$asBoundingRectangle1 = " & $asBoundingRectangle1 & @CRLF ) UIA_GetArrayPropertyValue() in UIA_Functions.au3 converts a property returned as an array to a comma separated string. 12. How to create element action objects Create the ValuePattern action object. It'll be used in topic 14 to set a new text in the Edit control. Click the Edit control in the treeview. Select the row as shown (scroll down), right-click and click Create sample code. New code: ; --- Value Pattern (action) Object --- ConsoleWrite( "--- Value Pattern (action) Object ---" & @CRLF ) Local $pValuePattern1, $oValuePattern1 $oEdit1.GetCurrentPattern( $UIA_ValuePatternId, $pValuePattern1 ) $oValuePattern1 = ObjCreateInterface( $pValuePattern1, $sIID_IUIAutomationValuePattern, $dtagIUIAutomationValuePattern ) If Not IsObj( $oValuePattern1 ) Then Return ConsoleWrite( "$oValuePattern1 ERR" & @CRLF ) ConsoleWrite( "$oValuePattern1 OK" & @CRLF ) 13. How to get pattern property values Extract the existing text in the Edit control. That's the $UIA_ValueValuePropertyId. Click the Edit control in the treeview. Select the row as shown (scroll down), right-click and click Create sample code. New code: ; --- Control Pattern Properties --- ConsoleWrite( "--- Control Pattern Properties ---" & @CRLF ) Local $sValueValue1 $oEdit1.GetCurrentPropertyValue( $UIA_ValueValuePropertyId, $sValueValue1 ) ConsoleWrite( "$sValueValue1 = " & $sValueValue1 & @CRLF ) 14. How to perform element actions Set a new text, HelloWorld, in the Edit control with the SetValue() method of the ValuePattern action object. Click the Edit control in the treeview. Select the row as shown (scroll down), right-click and click Create sample code. New code: ; --- Value Pattern (action) Methods --- ConsoleWrite( "--- Value Pattern (action) Methods ---" & @CRLF ) $oValuePattern1.SetValue(bstr) ConsoleWrite( "$oValuePattern1.SetValue()" & @CRLF ) bstr must be replaced with "HelloWorld". We'll do that in topic 15. 15. How to create executable code The code in the listview Code page isn't immediately executable. A few necessary corrections must be made before the code is executable. Click Sample code | Corrections to add a list of necessary and nice corrections. New code: ; Code corrections: ; CUIAutomation2.au3 path ; UIA_Functions.au3 path ; Code to open target appl ; Create undeclared variables ; Delete double declared variables ; Window, Pane, Parent and Control names ; Fill out Pattern (action) Method parameters All code directly from the listview Code page (Examples\2) How to topics\Notepad\ListViewCodePage.au3): #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 ;#AutoIt3Wrapper_UseX64=n ; If target application is running as 32 bit code ;#AutoIt3Wrapper_UseX64=y ; If target application is running as 64 bit code #include "CUIAutomation2.au3" ; Get proper version in UIASpy Includes folder ;#include "UIA_Functions.au3" ; Can be copied from UIASpy Includes folder Opt( "MustDeclareVars", 1 ) Example() Func Example() ; 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 Desktop element Local $pDesktop, $oDesktop $oUIAutomation.GetRootElement( $pDesktop ) $oDesktop = ObjCreateInterface( $pDesktop, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oDesktop ) Then Return ConsoleWrite( "$oDesktop ERR" & @CRLF ) ConsoleWrite( "$oDesktop OK" & @CRLF ) EndFunc ; --- Find window/control --- ConsoleWrite( "--- Find window/control ---" & @CRLF ) Local $pCondition0 $oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "Notepad", $pCondition0 ) If Not $pCondition0 Then Return ConsoleWrite( "$pCondition0 ERR" & @CRLF ) ConsoleWrite( "$pCondition0 OK" & @CRLF ) Local $pWindow1, $oWindow1 $oDesktop.FindFirst( $TreeScope_Descendants, $pCondition0, $pWindow1 ) $oWindow1 = ObjCreateInterface( $pWindow1, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oWindow1 ) Then Return ConsoleWrite( "$oWindow1 ERR" & @CRLF ) ConsoleWrite( "$oWindow1 OK" & @CRLF ) ; --- Find window/control --- ConsoleWrite( "--- Find window/control ---" & @CRLF ) Local $pCondition1 $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "15", $pCondition1 ) If Not $pCondition1 Then Return ConsoleWrite( "$pCondition1 ERR" & @CRLF ) ConsoleWrite( "$pCondition1 OK" & @CRLF ) Local $pEdit1, $oEdit1 $oWindow1.FindFirst( $TreeScope_Descendants, $pCondition1, $pEdit1 ) $oEdit1 = ObjCreateInterface( $pEdit1, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oEdit1 ) Then Return ConsoleWrite( "$oEdit1 ERR" & @CRLF ) ConsoleWrite( "$oEdit1 OK" & @CRLF ) ; --- Element Properties --- ConsoleWrite( "--- Element Properties ---" & @CRLF ) Local $hNativeWindowHandle1 $oEdit1.GetCurrentPropertyValue( $UIA_NativeWindowHandlePropertyId, $hNativeWindowHandle1 ) ConsoleWrite( "$hNativeWindowHandle1 = " & $hNativeWindowHandle1 & @CRLF ) Local $asBoundingRectangle1 $oEdit1.GetCurrentPropertyValue( $UIA_BoundingRectanglePropertyId, $asBoundingRectangle1 ) UIA_GetArrayPropertyValue( $asBoundingRectangle1 ) ConsoleWrite( "$asBoundingRectangle1 = " & $asBoundingRectangle1 & @CRLF ) ; --- Value Pattern (action) Object --- ConsoleWrite( "--- Value Pattern (action) Object ---" & @CRLF ) Local $pValuePattern1, $oValuePattern1 $oEdit1.GetCurrentPattern( $UIA_ValuePatternId, $pValuePattern1 ) $oValuePattern1 = ObjCreateInterface( $pValuePattern1, $sIID_IUIAutomationValuePattern, $dtagIUIAutomationValuePattern ) If Not IsObj( $oValuePattern1 ) Then Return ConsoleWrite( "$oValuePattern1 ERR" & @CRLF ) ConsoleWrite( "$oValuePattern1 OK" & @CRLF ) ; --- Control Pattern Properties --- ConsoleWrite( "--- Control Pattern Properties ---" & @CRLF ) Local $sValueValue1 $oEdit1.GetCurrentPropertyValue( $UIA_ValueValuePropertyId, $sValueValue1 ) ConsoleWrite( "$sValueValue1 = " & $sValueValue1 & @CRLF ) ; --- Value Pattern (action) Methods --- ConsoleWrite( "--- Value Pattern (action) Methods ---" & @CRLF ) $oValuePattern1.SetValue(bstr) ConsoleWrite( "$oValuePattern1.SetValue()" & @CRLF ) ; Code corrections: ; CUIAutomation2.au3 path ; UIA_Functions.au3 path ; Code to open target appl ; Create undeclared variables ; Delete double declared variables ; Window, Pane, Parent and Control names ; Fill out Pattern (action) Method parameters Necessary corrections: ; CUIAutomation2.au3 path ; UIA_Functions.au3 path ; Fill out Pattern (action) Method parameters And the code from line 26 must be moved into the function. All code after necessary corrections (Examples\2) How to topics\Notepad\NecessaryCorrections.au3): #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 ;#AutoIt3Wrapper_UseX64=n ; If target application is running as 32 bit code ;#AutoIt3Wrapper_UseX64=y ; If target application is running as 64 bit code #include "..\..\..\Includes\CUIAutomation2.au3" ; Get proper version in UIASpy Includes folder #include "..\..\..\Includes\UIA_Functions.au3" ; Can be copied from UIASpy Includes folder Opt( "MustDeclareVars", 1 ) Example() Func Example() ; 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 Desktop element Local $pDesktop, $oDesktop $oUIAutomation.GetRootElement( $pDesktop ) $oDesktop = ObjCreateInterface( $pDesktop, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oDesktop ) Then Return ConsoleWrite( "$oDesktop ERR" & @CRLF ) ConsoleWrite( "$oDesktop OK" & @CRLF ) ; --- Find window/control --- ConsoleWrite( "--- Find window/control ---" & @CRLF ) Local $pCondition0 $oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "Notepad", $pCondition0 ) If Not $pCondition0 Then Return ConsoleWrite( "$pCondition0 ERR" & @CRLF ) ConsoleWrite( "$pCondition0 OK" & @CRLF ) Local $pWindow1, $oWindow1 $oDesktop.FindFirst( $TreeScope_Descendants, $pCondition0, $pWindow1 ) $oWindow1 = ObjCreateInterface( $pWindow1, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oWindow1 ) Then Return ConsoleWrite( "$oWindow1 ERR" & @CRLF ) ConsoleWrite( "$oWindow1 OK" & @CRLF ) ; --- Find window/control --- ConsoleWrite( "--- Find window/control ---" & @CRLF ) Local $pCondition1 $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "15", $pCondition1 ) If Not $pCondition1 Then Return ConsoleWrite( "$pCondition1 ERR" & @CRLF ) ConsoleWrite( "$pCondition1 OK" & @CRLF ) Local $pEdit1, $oEdit1 $oWindow1.FindFirst( $TreeScope_Descendants, $pCondition1, $pEdit1 ) $oEdit1 = ObjCreateInterface( $pEdit1, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oEdit1 ) Then Return ConsoleWrite( "$oEdit1 ERR" & @CRLF ) ConsoleWrite( "$oEdit1 OK" & @CRLF ) ; --- Element Properties --- ConsoleWrite( "--- Element Properties ---" & @CRLF ) Local $hNativeWindowHandle1 $oEdit1.GetCurrentPropertyValue( $UIA_NativeWindowHandlePropertyId, $hNativeWindowHandle1 ) ConsoleWrite( "$hNativeWindowHandle1 = " & $hNativeWindowHandle1 & @CRLF ) Local $asBoundingRectangle1 $oEdit1.GetCurrentPropertyValue( $UIA_BoundingRectanglePropertyId, $asBoundingRectangle1 ) UIA_GetArrayPropertyValue( $asBoundingRectangle1 ) ConsoleWrite( "$asBoundingRectangle1 = " & $asBoundingRectangle1 & @CRLF ) ; --- Value Pattern (action) Object --- ConsoleWrite( "--- Value Pattern (action) Object ---" & @CRLF ) Local $pValuePattern1, $oValuePattern1 $oEdit1.GetCurrentPattern( $UIA_ValuePatternId, $pValuePattern1 ) $oValuePattern1 = ObjCreateInterface( $pValuePattern1, $sIID_IUIAutomationValuePattern, $dtagIUIAutomationValuePattern ) If Not IsObj( $oValuePattern1 ) Then Return ConsoleWrite( "$oValuePattern1 ERR" & @CRLF ) ConsoleWrite( "$oValuePattern1 OK" & @CRLF ) ; --- Control Pattern Properties --- ConsoleWrite( "--- Control Pattern Properties ---" & @CRLF ) Local $sValueValue1 $oEdit1.GetCurrentPropertyValue( $UIA_ValueValuePropertyId, $sValueValue1 ) ConsoleWrite( "$sValueValue1 = " & $sValueValue1 & @CRLF ) ; --- Value Pattern (action) Methods --- ConsoleWrite( "--- Value Pattern (action) Methods ---" & @CRLF ) $oValuePattern1.SetValue( "HelloWorld" ) ConsoleWrite( "$oValuePattern1.SetValue()" & @CRLF ) ; Code corrections: ; CUIAutomation2.au3 path ; UIA_Functions.au3 path ; Fill out Pattern (action) Method parameters EndFunc Run the code. SciTE output: $oUIAutomation OK $oDesktop OK --- Find window/control --- $pCondition0 OK $oWindow1 OK --- Find window/control --- $pCondition1 OK $oEdit1 OK --- Element Properties --- $hNativeWindowHandle1 = 197258 $asBoundingRectangle1 = 258,100,984,842 --- Value Pattern (action) Object --- $oValuePattern1 OK --- Control Pattern Properties --- $sValueValue1 = Hello --- Value Pattern (action) Methods --- $oValuePattern1.SetValue() Nice corrections: Update comments. All code after nice corrections (Examples\2) How to topics\Notepad\NiceCorrections.au3): #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 ;#AutoIt3Wrapper_UseX64=n ; If target application is running as 32 bit code ;#AutoIt3Wrapper_UseX64=y ; If target application is running as 64 bit code #include "..\..\..\Includes\CUIAutomation2.au3" ; Get proper version in UIASpy Includes folder #include "..\..\..\Includes\UIA_Functions.au3" ; Can be copied from UIASpy Includes folder Opt( "MustDeclareVars", 1 ) Example() Func Example() ; 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 Desktop element Local $pDesktop, $oDesktop $oUIAutomation.GetRootElement( $pDesktop ) $oDesktop = ObjCreateInterface( $pDesktop, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oDesktop ) Then Return ConsoleWrite( "$oDesktop ERR" & @CRLF ) ConsoleWrite( "$oDesktop OK" & @CRLF ) ; --- Find Notepad --- ConsoleWrite( "--- Find Notepad ---" & @CRLF ) Local $pCondition0 $oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "Notepad", $pCondition0 ) If Not $pCondition0 Then Return ConsoleWrite( "$pCondition0 ERR" & @CRLF ) ConsoleWrite( "$pCondition0 OK" & @CRLF ) Local $pWindow1, $oWindow1 $oDesktop.FindFirst( $TreeScope_Descendants, $pCondition0, $pWindow1 ) $oWindow1 = ObjCreateInterface( $pWindow1, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oWindow1 ) Then Return ConsoleWrite( "$oWindow1 ERR" & @CRLF ) ConsoleWrite( "$oWindow1 OK" & @CRLF ) ; --- Find Edit control --- ConsoleWrite( "--- Find Edit control ---" & @CRLF ) Local $pCondition1 $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "15", $pCondition1 ) If Not $pCondition1 Then Return ConsoleWrite( "$pCondition1 ERR" & @CRLF ) ConsoleWrite( "$pCondition1 OK" & @CRLF ) Local $pEdit1, $oEdit1 $oWindow1.FindFirst( $TreeScope_Descendants, $pCondition1, $pEdit1 ) $oEdit1 = ObjCreateInterface( $pEdit1, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oEdit1 ) Then Return ConsoleWrite( "$oEdit1 ERR" & @CRLF ) ConsoleWrite( "$oEdit1 OK" & @CRLF ) ; --- Element Properties --- ConsoleWrite( "--- Element Properties ---" & @CRLF ) Local $hNativeWindowHandle1 $oEdit1.GetCurrentPropertyValue( $UIA_NativeWindowHandlePropertyId, $hNativeWindowHandle1 ) ConsoleWrite( "$hNativeWindowHandle1 = " & $hNativeWindowHandle1 & @CRLF ) Local $asBoundingRectangle1 $oEdit1.GetCurrentPropertyValue( $UIA_BoundingRectanglePropertyId, $asBoundingRectangle1 ) UIA_GetArrayPropertyValue( $asBoundingRectangle1 ) ConsoleWrite( "$asBoundingRectangle1 = " & $asBoundingRectangle1 & @CRLF ) ; --- Value Pattern (action) Object --- ConsoleWrite( "--- Value Pattern (action) Object ---" & @CRLF ) Local $pValuePattern1, $oValuePattern1 $oEdit1.GetCurrentPattern( $UIA_ValuePatternId, $pValuePattern1 ) $oValuePattern1 = ObjCreateInterface( $pValuePattern1, $sIID_IUIAutomationValuePattern, $dtagIUIAutomationValuePattern ) If Not IsObj( $oValuePattern1 ) Then Return ConsoleWrite( "$oValuePattern1 ERR" & @CRLF ) ConsoleWrite( "$oValuePattern1 OK" & @CRLF ) ; --- Control Pattern Properties --- ConsoleWrite( "--- Control Pattern Properties ---" & @CRLF ) Local $sValueValue1 $oEdit1.GetCurrentPropertyValue( $UIA_ValueValuePropertyId, $sValueValue1 ) ConsoleWrite( "$sValueValue1 = " & $sValueValue1 & @CRLF ) ; --- Value Pattern (action) Methods --- ConsoleWrite( "--- Value Pattern (action) Methods ---" & @CRLF ) $oValuePattern1.SetValue( "HelloWorld" ) ConsoleWrite( "$oValuePattern1.SetValue()" & @CRLF ) ; Code corrections: ; CUIAutomation2.au3 path ; UIA_Functions.au3 path ; Fill out Pattern (action) Method parameters ; Nice corrections: Update comments EndFunc Run the code. SciTE output: $oUIAutomation OK $oDesktop OK --- Find Notepad --- $pCondition0 OK $oWindow1 OK --- Find Edit control --- $pCondition1 OK $oEdit1 OK --- Element Properties --- $hNativeWindowHandle1 = 197258 $asBoundingRectangle1 = 258,100,984,842 --- Value Pattern (action) Object --- $oValuePattern1 OK --- Control Pattern Properties --- $sValueValue1 = Hello --- Value Pattern (action) Methods --- $oValuePattern1.SetValue() Top of post1 point
-
Automating Notepad with Sample code - all at once In an automation task like the Notepad example above, it's much faster to create all sample code at once than it is to create the sample code in a step-by-step procedure. For pattern methods, it's also faster to create sample code by selecting the method through the Pattern... item in the Sample code menu in UIASpy than it is to create pattern method sample code through the detail information listview page. But to use the pattern listview page you need to know which pattern to use. The advantage of using the detail information listview page is that it only shows the patterns and pattern methods that are applicable to the specific control. These are all the steps in the Notepad example: Add initial code Open Notepad Sleep( 1000 ) Get Notepad window Fill Edit control Identify Edit control Execute SetValue pattern method Open File menu Identify File menu Execute Invoke pattern method Sleep( 100 ) Click Save As... menu item Identify Save As... menu item Execute Invoke pattern method Sleep( 1000 ) Get Save As window Fill Edit control (File name) Identify Edit control Execute SetValue pattern method Click Save button Identify Save button Execute Invoke pattern method Corrections And this is sample code for all the steps in the entire Notepad example. Examples\Notepad\Windows 7, 10\3) Sample code - all at once\1) Before corrections.au3: #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 ;#AutoIt3Wrapper_UseX64=n ; If target application is running as 32 bit code ;#AutoIt3Wrapper_UseX64=y ; If target application is running as 64 bit code #include "CUIAutomation2.au3" Opt( "MustDeclareVars", 1 ) Example() Func Example() ; 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 Desktop element Local $pDesktop, $oDesktop $oUIAutomation.GetRootElement( $pDesktop ) $oDesktop = ObjCreateInterface( $pDesktop, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oDesktop ) Then Return ConsoleWrite( "$oDesktop ERR" & @CRLF ) ConsoleWrite( "$oDesktop OK" & @CRLF ) EndFunc ; --- Find window/control --- ConsoleWrite( "--- Find window/control ---" & @CRLF ) Local $pCondition0 $oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "Notepad", $pCondition0 ) If Not $pCondition0 Then Return ConsoleWrite( "$pCondition0 ERR" & @CRLF ) ConsoleWrite( "$pCondition0 OK" & @CRLF ) Local $pWindow, $oWindow $oDesktop.FindFirst( $TreeScope_Descendants, $pCondition0, $pWindow ) $oWindow = ObjCreateInterface( $pWindow, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oWindow ) Then Return ConsoleWrite( "$oWindow ERR" & @CRLF ) ConsoleWrite( "$oWindow OK" & @CRLF ) ; --- Find window/control --- ConsoleWrite( "--- Find window/control ---" & @CRLF ) Local $pCondition1 $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "15", $pCondition1 ) If Not $pCondition1 Then Return ConsoleWrite( "$pCondition1 ERR" & @CRLF ) ConsoleWrite( "$pCondition1 OK" & @CRLF ) Local $pEdit, $oEdit $oParent.FindFirst( $TreeScope_Descendants, $pCondition1, $pEdit ) $oEdit = ObjCreateInterface( $pEdit, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oEdit ) Then Return ConsoleWrite( "$oEdit ERR" & @CRLF ) ConsoleWrite( "$oEdit OK" & @CRLF ) ; --- Value Pattern (action) Object --- ConsoleWrite( "--- Value Pattern (action) Object ---" & @CRLF ) Local $pValuePattern, $oValuePattern $oEdit.GetCurrentPattern( $UIA_ValuePatternId, $pValuePattern ) $oValuePattern = ObjCreateInterface( $pValuePattern, $sIID_IUIAutomationValuePattern, $dtagIUIAutomationValuePattern ) If Not IsObj( $oValuePattern ) Then Return ConsoleWrite( "$oValuePattern ERR" & @CRLF ) ConsoleWrite( "$oValuePattern OK" & @CRLF ) $oValuePattern.SetValue(bstr) ; --- Find window/control --- ConsoleWrite( "--- Find window/control ---" & @CRLF ) Local $pCondition2, $pCondition3, $pAndCondition3 $oUIAutomation.CreatePropertyCondition( $UIA_ControlTypePropertyId, $UIA_MenuItemControlTypeId, $pCondition2 ) $oUIAutomation.CreatePropertyCondition( $UIA_NamePropertyId, "File", $pCondition3 ) $oUIAutomation.CreateAndCondition( $pCondition2, $pCondition3, $pAndCondition3 ) If Not $pAndCondition3 Then Return ConsoleWrite( "$pAndCondition3 ERR" & @CRLF ) ConsoleWrite( "$pAndCondition3 OK" & @CRLF ) Local $pMenuItem, $oMenuItem $oParent.FindFirst( $TreeScope_Descendants, $pAndCondition3, $pMenuItem ) $oMenuItem = ObjCreateInterface( $pMenuItem, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oMenuItem ) Then Return ConsoleWrite( "$oMenuItem ERR" & @CRLF ) ConsoleWrite( "$oMenuItem OK" & @CRLF ) ; --- Invoke Pattern (action) Object --- ConsoleWrite( "--- Invoke Pattern (action) Object ---" & @CRLF ) Local $pInvokePattern, $oInvokePattern $oMenuItem.GetCurrentPattern( $UIA_InvokePatternId, $pInvokePattern ) $oInvokePattern = ObjCreateInterface( $pInvokePattern, $sIID_IUIAutomationInvokePattern, $dtagIUIAutomationInvokePattern ) If Not IsObj( $oInvokePattern ) Then Return ConsoleWrite( "$oInvokePattern ERR" & @CRLF ) ConsoleWrite( "$oInvokePattern OK" & @CRLF ) $oInvokePattern.Invoke() Sleep( 1000 ) ; --- Find window/control --- ConsoleWrite( "--- Find window/control ---" & @CRLF ) Local $pCondition4, $pCondition5, $pAndCondition5 $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "4", $pCondition4 ) $oUIAutomation.CreatePropertyCondition( $UIA_ControlTypePropertyId, $UIA_MenuItemControlTypeId, $pCondition5 ) $oUIAutomation.CreateAndCondition( $pCondition4, $pCondition5, $pAndCondition5 ) If Not $pAndCondition5 Then Return ConsoleWrite( "$pAndCondition5 ERR" & @CRLF ) ConsoleWrite( "$pAndCondition5 OK" & @CRLF ) Local $pMenuItem, $oMenuItem $oParent.FindFirst( $TreeScope_Descendants, $pAndCondition5, $pMenuItem ) $oMenuItem = ObjCreateInterface( $pMenuItem, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oMenuItem ) Then Return ConsoleWrite( "$oMenuItem ERR" & @CRLF ) ConsoleWrite( "$oMenuItem OK" & @CRLF ) ; --- Invoke Pattern (action) Object --- ConsoleWrite( "--- Invoke Pattern (action) Object ---" & @CRLF ) Local $pInvokePattern, $oInvokePattern $oMenuItem.GetCurrentPattern( $UIA_InvokePatternId, $pInvokePattern ) $oInvokePattern = ObjCreateInterface( $pInvokePattern, $sIID_IUIAutomationInvokePattern, $dtagIUIAutomationInvokePattern ) If Not IsObj( $oInvokePattern ) Then Return ConsoleWrite( "$oInvokePattern ERR" & @CRLF ) ConsoleWrite( "$oInvokePattern OK" & @CRLF ) $oInvokePattern.Invoke() Sleep( 1000 ) ; --- Find window/control --- ConsoleWrite( "--- Find window/control ---" & @CRLF ) Local $pCondition6 $oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "#32770", $pCondition6 ) If Not $pCondition6 Then Return ConsoleWrite( "$pCondition6 ERR" & @CRLF ) ConsoleWrite( "$pCondition6 OK" & @CRLF ) Local $pWindow, $oWindow $oParent.FindFirst( $TreeScope_Descendants, $pCondition6, $pWindow ) $oWindow = ObjCreateInterface( $pWindow, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oWindow ) Then Return ConsoleWrite( "$oWindow ERR" & @CRLF ) ConsoleWrite( "$oWindow OK" & @CRLF ) ; --- Find window/control --- ConsoleWrite( "--- Find window/control ---" & @CRLF ) Local $pCondition7 $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "1001", $pCondition7 ) If Not $pCondition7 Then Return ConsoleWrite( "$pCondition7 ERR" & @CRLF ) ConsoleWrite( "$pCondition7 OK" & @CRLF ) Local $pEdit, $oEdit $oParent.FindFirst( $TreeScope_Descendants, $pCondition7, $pEdit ) $oEdit = ObjCreateInterface( $pEdit, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oEdit ) Then Return ConsoleWrite( "$oEdit ERR" & @CRLF ) ConsoleWrite( "$oEdit OK" & @CRLF ) ; --- Value Pattern (action) Object --- ConsoleWrite( "--- Value Pattern (action) Object ---" & @CRLF ) Local $pValuePattern, $oValuePattern $oEdit.GetCurrentPattern( $UIA_ValuePatternId, $pValuePattern ) $oValuePattern = ObjCreateInterface( $pValuePattern, $sIID_IUIAutomationValuePattern, $dtagIUIAutomationValuePattern ) If Not IsObj( $oValuePattern ) Then Return ConsoleWrite( "$oValuePattern ERR" & @CRLF ) ConsoleWrite( "$oValuePattern OK" & @CRLF ) $oValuePattern.SetValue(bstr) ; --- Find window/control --- ConsoleWrite( "--- Find window/control ---" & @CRLF ) Local $pCondition8, $pCondition9, $pAndCondition9 $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "1", $pCondition8 ) $oUIAutomation.CreatePropertyCondition( $UIA_ControlTypePropertyId, $UIA_ButtonControlTypeId, $pCondition9 ) $oUIAutomation.CreateAndCondition( $pCondition8, $pCondition9, $pAndCondition9 ) If Not $pAndCondition9 Then Return ConsoleWrite( "$pAndCondition9 ERR" & @CRLF ) ConsoleWrite( "$pAndCondition9 OK" & @CRLF ) Local $pButton, $oButton $oParent.FindFirst( $TreeScope_Descendants, $pAndCondition9, $pButton ) $oButton = ObjCreateInterface( $pButton, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oButton ) Then Return ConsoleWrite( "$oButton ERR" & @CRLF ) ConsoleWrite( "$oButton OK" & @CRLF ) ; --- Invoke Pattern (action) Object --- ConsoleWrite( "--- Invoke Pattern (action) Object ---" & @CRLF ) Local $pInvokePattern, $oInvokePattern $oButton.GetCurrentPattern( $UIA_InvokePatternId, $pInvokePattern ) $oInvokePattern = ObjCreateInterface( $pInvokePattern, $sIID_IUIAutomationInvokePattern, $dtagIUIAutomationInvokePattern ) If Not IsObj( $oInvokePattern ) Then Return ConsoleWrite( "$oInvokePattern ERR" & @CRLF ) ConsoleWrite( "$oInvokePattern OK" & @CRLF ) $oInvokePattern.Invoke() ; Code corrections: ; CUIAutomation2.au3 path ; Code to open target appl ; Create undeclared variables ; Delete double declared variables ; Correct $pWindow, $pParent and $pControl names ; Correct $oWindow, $oParent and $oControl names The code isn't immediately executable. It's necessary to make some corrections. Most corrections are listed at the bottom of the code as comments. Note that all condition variables are created properly. They need no corrections. This is the code after corrections. Examples\Notepad\Windows 7, 10\3) Sample code - all at once\2) After corrections.au3: #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 ;#AutoIt3Wrapper_UseX64=n ; If target application is running as 32 bit code ;#AutoIt3Wrapper_UseX64=y ; If target application is running as 64 bit code #include "..\..\..\..\Includes\CUIAutomation2.au3" Opt( "MustDeclareVars", 1 ) Example() Func Example() ; Open Notepad Run( "Notepad" ) Sleep( 1000 ) ; 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 Desktop element Local $pDesktop, $oDesktop $oUIAutomation.GetRootElement( $pDesktop ) $oDesktop = ObjCreateInterface( $pDesktop, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oDesktop ) Then Return ConsoleWrite( "$oDesktop ERR" & @CRLF ) ConsoleWrite( "$oDesktop OK" & @CRLF ) ; --- Get Notepad window --- ConsoleWrite( "--- Get Notepad window ---" & @CRLF ) Local $pCondition0 $oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "Notepad", $pCondition0 ) If Not $pCondition0 Then Return ConsoleWrite( "$pCondition0 ERR" & @CRLF ) ConsoleWrite( "$pCondition0 OK" & @CRLF ) Local $pNotepad, $oNotepad $oDesktop.FindFirst( $TreeScope_Descendants, $pCondition0, $pNotepad ) $oNotepad = ObjCreateInterface( $pNotepad, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oNotepad ) Then Return ConsoleWrite( "$oNotepad ERR" & @CRLF ) ConsoleWrite( "$oNotepad OK" & @CRLF ) ; --- Fill Edit control --- ConsoleWrite( "--- Fill Edit control ---" & @CRLF ) Local $pCondition1 $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "15", $pCondition1 ) If Not $pCondition1 Then Return ConsoleWrite( "$pCondition1 ERR" & @CRLF ) ConsoleWrite( "$pCondition1 OK" & @CRLF ) Local $pEdit, $oEdit $oNotepad.FindFirst( $TreeScope_Descendants, $pCondition1, $pEdit ) $oEdit = ObjCreateInterface( $pEdit, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oEdit ) Then Return ConsoleWrite( "$oEdit ERR" & @CRLF ) ConsoleWrite( "$oEdit OK" & @CRLF ) Local $pValuePattern, $oValuePattern $oEdit.GetCurrentPattern( $UIA_ValuePatternId, $pValuePattern ) $oValuePattern = ObjCreateInterface( $pValuePattern, $sIID_IUIAutomationValuePattern, $dtagIUIAutomationValuePattern ) If Not IsObj( $oValuePattern ) Then Return ConsoleWrite( "$oValuePattern ERR" & @CRLF ) ConsoleWrite( "$oValuePattern OK" & @CRLF ) $oValuePattern.SetValue( "HelloWorld" ) ; --- Open File menu --- ConsoleWrite( "--- Open File menu ---" & @CRLF ) Local $pCondition2, $pCondition3, $pAndCondition3 $oUIAutomation.CreatePropertyCondition( $UIA_ControlTypePropertyId, $UIA_MenuItemControlTypeId, $pCondition2 ) $oUIAutomation.CreatePropertyCondition( $UIA_NamePropertyId, "File", $pCondition3 ) ; <<<<<<<<<<<<<<<<<<<<<<<< $oUIAutomation.CreateAndCondition( $pCondition2, $pCondition3, $pAndCondition3 ) If Not $pAndCondition3 Then Return ConsoleWrite( "$pAndCondition3 ERR" & @CRLF ) ConsoleWrite( "$pAndCondition3 OK" & @CRLF ) Local $pMenuItem, $oMenuItem $oNotepad.FindFirst( $TreeScope_Descendants, $pAndCondition3, $pMenuItem ) $oMenuItem = ObjCreateInterface( $pMenuItem, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oMenuItem ) Then Return ConsoleWrite( "$oMenuItem ERR" & @CRLF ) ConsoleWrite( "$oMenuItem OK" & @CRLF ) Local $pInvokePattern, $oInvokePattern $oMenuItem.GetCurrentPattern( $UIA_InvokePatternId, $pInvokePattern ) $oInvokePattern = ObjCreateInterface( $pInvokePattern, $sIID_IUIAutomationInvokePattern, $dtagIUIAutomationInvokePattern ) If Not IsObj( $oInvokePattern ) Then Return ConsoleWrite( "$oInvokePattern ERR" & @CRLF ) ConsoleWrite( "$oInvokePattern OK" & @CRLF ) $oInvokePattern.Invoke() Sleep( 100 ) ; --- Click Save As... menu item --- ConsoleWrite( "--- Click Save As... menu item ---" & @CRLF ) Local $pCondition4, $pCondition5, $pAndCondition5 $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "4", $pCondition4 ) $oUIAutomation.CreatePropertyCondition( $UIA_ControlTypePropertyId, $UIA_MenuItemControlTypeId, $pCondition5 ) $oUIAutomation.CreateAndCondition( $pCondition4, $pCondition5, $pAndCondition5 ) If Not $pAndCondition5 Then Return ConsoleWrite( "$pAndCondition5 ERR" & @CRLF ) ConsoleWrite( "$pAndCondition5 OK" & @CRLF ) $oNotepad.FindFirst( $TreeScope_Descendants, $pAndCondition5, $pMenuItem ) $oMenuItem = ObjCreateInterface( $pMenuItem, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oMenuItem ) Then Return ConsoleWrite( "$oMenuItem ERR" & @CRLF ) ConsoleWrite( "$oMenuItem OK" & @CRLF ) $oMenuItem.GetCurrentPattern( $UIA_InvokePatternId, $pInvokePattern ) $oInvokePattern = ObjCreateInterface( $pInvokePattern, $sIID_IUIAutomationInvokePattern, $dtagIUIAutomationInvokePattern ) If Not IsObj( $oInvokePattern ) Then Return ConsoleWrite( "$oInvokePattern ERR" & @CRLF ) ConsoleWrite( "$oInvokePattern OK" & @CRLF ) $oInvokePattern.Invoke() Sleep( 1000 ) ; --- Get Save As window --- ConsoleWrite( "--- Get Save As window ---" & @CRLF ) Local $pCondition6 $oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "#32770", $pCondition6 ) If Not $pCondition6 Then Return ConsoleWrite( "$pCondition6 ERR" & @CRLF ) ConsoleWrite( "$pCondition6 OK" & @CRLF ) Local $pSaveAsWin, $oSaveAsWin $oNotepad.FindFirst( $TreeScope_Descendants, $pCondition6, $pSaveAsWin ) $oSaveAsWin = ObjCreateInterface( $pSaveAsWin, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oSaveAsWin ) Then Return ConsoleWrite( "$oSaveAsWin ERR" & @CRLF ) ConsoleWrite( "$oSaveAsWin OK" & @CRLF ) ; --- Fill Edit control (File name) --- ConsoleWrite( "--- Fill Edit control (File name) ---" & @CRLF ) Local $pCondition7 $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "1001", $pCondition7 ) If Not $pCondition7 Then Return ConsoleWrite( "$pCondition7 ERR" & @CRLF ) ConsoleWrite( "$pCondition7 OK" & @CRLF ) $oSaveAsWin.FindFirst( $TreeScope_Descendants, $pCondition7, $pEdit ) $oEdit = ObjCreateInterface( $pEdit, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oEdit ) Then Return ConsoleWrite( "$oEdit ERR" & @CRLF ) ConsoleWrite( "$oEdit OK" & @CRLF ) $oEdit.GetCurrentPattern( $UIA_ValuePatternId, $pValuePattern ) $oValuePattern = ObjCreateInterface( $pValuePattern, $sIID_IUIAutomationValuePattern, $dtagIUIAutomationValuePattern ) If Not IsObj( $oValuePattern ) Then Return ConsoleWrite( "$oValuePattern ERR" & @CRLF ) ConsoleWrite( "$oValuePattern OK" & @CRLF ) $oValuePattern.SetValue( "HelloWorld.txt" ) ; --- Click Save button --- ConsoleWrite( "--- Click Save button ---" & @CRLF ) Local $pCondition8, $pCondition9, $pAndCondition9 $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "1", $pCondition8 ) $oUIAutomation.CreatePropertyCondition( $UIA_ControlTypePropertyId, $UIA_ButtonControlTypeId, $pCondition9 ) $oUIAutomation.CreateAndCondition( $pCondition8, $pCondition9, $pAndCondition9 ) If Not $pAndCondition9 Then Return ConsoleWrite( "$pAndCondition9 ERR" & @CRLF ) ConsoleWrite( "$pAndCondition9 OK" & @CRLF ) Local $pButton, $oButton $oSaveAsWin.FindFirst( $TreeScope_Descendants, $pAndCondition9, $pButton ) $oButton = ObjCreateInterface( $pButton, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oButton ) Then Return ConsoleWrite( "$oButton ERR" & @CRLF ) ConsoleWrite( "$oButton OK" & @CRLF ) $oButton.GetCurrentPattern( $UIA_InvokePatternId, $pInvokePattern ) $oInvokePattern = ObjCreateInterface( $pInvokePattern, $sIID_IUIAutomationInvokePattern, $dtagIUIAutomationInvokePattern ) If Not IsObj( $oInvokePattern ) Then Return ConsoleWrite( "$oInvokePattern ERR" & @CRLF ) ConsoleWrite( "$oInvokePattern OK" & @CRLF ) $oInvokePattern.Invoke() EndFunc When executing the code, it should perform all Notepad automation tasks and generate this output in SciTE. SciTE output: $oUIAutomation OK $oDesktop OK --- Get Notepad window --- $pCondition0 OK $oNotepad OK --- Fill Edit control --- $pCondition1 OK $oEdit OK $oValuePattern OK --- Open File menu --- $pAndCondition3 OK $oMenuItem OK $oInvokePattern OK --- Click Save As... menu item --- $pAndCondition5 OK $oMenuItem OK $oInvokePattern OK --- Get Save As window --- $pCondition6 OK $oSaveAsWin OK --- Fill Edit control (File name) --- $pCondition7 OK $oEdit OK $oValuePattern OK --- Click Save button --- $pAndCondition9 OK $oButton OK $oInvokePattern OK1 point
-
Automating Notepad with Sample code - step by step This is a repetition of the Notepad example above. But this time, the possibilities for creating Sample code in UIASpy will be exploited. Only the first part of the example will be reviewed once more in detail. But the pictures will not be repeated. So please open the previous example in a new tab in the browser. Open "Examples\Notepad\Windows 7, 10\2) Sample code - step by step\Notepad.au3" (empty) in SciTE. The file is included in the zip-file in bottom of first post. Open UIASpy (also included in zip-file) and keep UIASpy open throughout the entire example. Open Left pane main menu and click Delete top windows to delete all treeview windows except Desktop. First step is to add initial code to create the UI Automation object and the Desktop element. Open Sample code main menu in UIASpy and click Initial code. Open the menu once more and click Corrections to get an idea of the corrections that have to be made to this generic code. Paste the code from clipboard into SciTE (Shift+Insert). The code should look like this: #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 ;#AutoIt3Wrapper_UseX64=n ; If target application is running as 32 bit code ;#AutoIt3Wrapper_UseX64=y ; If target application is running as 64 bit code #include "CUIAutomation2.au3" Opt( "MustDeclareVars", 1 ) Example() Func Example() ; 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 Desktop element Local $pDesktop, $oDesktop $oUIAutomation.GetRootElement( $pDesktop ) $oDesktop = ObjCreateInterface( $pDesktop, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oDesktop ) Then Return ConsoleWrite( "$oDesktop ERR" & @CRLF ) ConsoleWrite( "$oDesktop OK" & @CRLF ) EndFunc ; Code corrections: ; CUIAutomation2.au3 path ; Code to open target appl ; Create undeclared variables ; Delete double declared variables ; Correct $pWindow, $pParent and $pControl names ; Correct $oWindow, $oParent and $oControl names Correct the path to CUIAutomation2.au3. Add code to open Notepad. Delete correction comments. Now the code should look like this: #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 ;#AutoIt3Wrapper_UseX64=n ; If target application is running as 32 bit code ;#AutoIt3Wrapper_UseX64=y ; If target application is running as 64 bit code #include "..\..\..\..\Includes\CUIAutomation2.au3" Opt( "MustDeclareVars", 1 ) Example() Func Example() ; Open Notepad Run( "Notepad" ) Sleep( 1000 ) ; 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 Desktop element Local $pDesktop, $oDesktop $oUIAutomation.GetRootElement( $pDesktop ) $oDesktop = ObjCreateInterface( $pDesktop, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oDesktop ) Then Return ConsoleWrite( "$oDesktop ERR" & @CRLF ) ConsoleWrite( "$oDesktop OK" & @CRLF ) EndFunc Run the code with F5 and leave Notepad open. Check that SciTE output is OK. Next step is to get the Notepad window as an UI Automation element. Place mouse cursor over Notepad title bar and press F2. Select the row in the Notepad detail information listview page in UIASpy that matches the row with the red arrow in the first picture in the previous example. Right-click and click Clear sample code in the context menu. Right-click once more and click Create sample code. Open Sample code main menu and click Corrections to get an idea of the corrections that have to be made to the code. Paste the code from clipboard into SciTE as the very last code (after EndFunc). The code should look like this: ; --- Find window/control --- ConsoleWrite( "--- Find window/control ---" & @CRLF ) Local $pCondition0 $oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "Notepad", $pCondition0 ) If Not $pCondition0 Then Return ConsoleWrite( "$pCondition0 ERR" & @CRLF ) ConsoleWrite( "$pCondition0 OK" & @CRLF ) Local $pWindow, $oWindow $oDesktop.FindFirst( $TreeScope_Descendants, $pCondition0, $pWindow ) $oWindow = ObjCreateInterface( $pWindow, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oWindow ) Then Return ConsoleWrite( "$oWindow ERR" & @CRLF ) ConsoleWrite( "$oWindow OK" & @CRLF ) ; Code corrections: ; CUIAutomation2.au3 path ; Code to open target appl ; Create undeclared variables ; Delete double declared variables ; Correct $pWindow, $pParent and $pControl names ; Correct $oWindow, $oParent and $oControl names Replace "window/control" with "Notepad window". Replace "Window" with "Notepad". Delete correction comments. Move code into function: ; --- Find Notepad window --- ConsoleWrite( "--- Find Notepad window ---" & @CRLF ) Local $pCondition0 $oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "Notepad", $pCondition0 ) If Not $pCondition0 Then Return ConsoleWrite( "$pCondition0 ERR" & @CRLF ) ConsoleWrite( "$pCondition0 OK" & @CRLF ) Local $pNotepad, $oNotepad $oDesktop.FindFirst( $TreeScope_Descendants, $pCondition0, $pNotepad ) $oNotepad = ObjCreateInterface( $pNotepad, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oNotepad ) Then Return ConsoleWrite( "$oNotepad ERR" & @CRLF ) ConsoleWrite( "$oNotepad OK" & @CRLF ) Close the previous Notepad, run the code and leave Notepad open. Check that SciTE output is OK. Third step is to fill out the Edit control with "HelloWorld". An UI Automation task is almost always done in two or three steps: 1. Create condition to find element 2. Find the UI Automation element 3. Extract info or perform action Each of these steps can usually be done in 1 - 3 lines of UI Automation code. All three steps can usually be done in less that 10 lines of UI Automation code (common AutoIt debug/info code not included). To get the Notepad window above we only used two steps. Place mouse cursor in the Edit control in Notepad and press F1. Select the row in the Edit detail information listview page in UIASpy that matches the row with the red arrow in the second picture in the previous example. Right-click and click Clear sample code in the context menu. Right-click once more and click Create sample code. Open Sample code main menu and click Corrections to get an idea of the corrections that have to be made to the code. Paste the code from clipboard into SciTE as the very last code (after EndFunc). The code should look like this: ; --- Find window/control --- ConsoleWrite( "--- Find window/control ---" & @CRLF ) Local $pCondition0 $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "15", $pCondition0 ) If Not $pCondition0 Then Return ConsoleWrite( "$pCondition0 ERR" & @CRLF ) ConsoleWrite( "$pCondition0 OK" & @CRLF ) Local $pEdit, $oEdit $oParent.FindFirst( $TreeScope_Descendants, $pCondition0, $pEdit ) $oEdit = ObjCreateInterface( $pEdit, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oEdit ) Then Return ConsoleWrite( "$oEdit ERR" & @CRLF ) ConsoleWrite( "$oEdit OK" & @CRLF ) ; Code corrections: ; CUIAutomation2.au3 path ; Code to open target appl ; Create undeclared variables ; Delete double declared variables ; Correct $pWindow, $pParent and $pControl names ; Correct $oWindow, $oParent and $oControl names Replace "Find window/control" with "Fill Edit control". Delete the line "Local $pCondition0". Replace "$oParent" with "$oNotepad". Delete correction comments. Move code into function: ; --- Fill Edit control --- ConsoleWrite( "--- Fill Edit control ---" & @CRLF ) $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "15", $pCondition0 ) If Not $pCondition0 Then Return ConsoleWrite( "$pCondition0 ERR" & @CRLF ) ConsoleWrite( "$pCondition0 OK" & @CRLF ) Local $pEdit, $oEdit $oNotepad.FindFirst( $TreeScope_Descendants, $pCondition0, $pEdit ) $oEdit = ObjCreateInterface( $pEdit, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oEdit ) Then Return ConsoleWrite( "$oEdit ERR" & @CRLF ) ConsoleWrite( "$oEdit OK" & @CRLF ) Close the previous Notepad, run the code and leave Notepad open. Check that SciTE output is OK. Place mouse cursor in the Edit control in Notepad and press F1. If there is a red (invalid) Notepad window in the treeview then delete it (Right-click the red element and click Delete element and all childs). Scroll down to the Control Patterns (element actions) section in the Edit detail information listview page in UIASpy. Select the row "$UIA_IsValuePatternAvailablePropertyId" that matches the row with the red arrow in the third picture in the previous example. Right-click and click Clear sample code in the context menu. Right-click once more and click Create sample code. Click the Edit item in the treeview to return to the Edit detail information listview page. Scroll down to the bottom of the listview page. The Value Pattern Methods subsection should be visible. Select the row with the SetValue(bstr) method in second column: Right-click and click Create sample code (don't clear the code this time). Paste the code from clipboard into SciTE as the very last code (after EndFunc). The code should look like this: ; --- Value Pattern (action) Object --- ConsoleWrite( "--- Value Pattern (action) Object ---" & @CRLF ) Local $pValuePattern, $oValuePattern $oEdit.GetCurrentPattern( $UIA_ValuePatternId, $pValuePattern ) $oValuePattern = ObjCreateInterface( $pValuePattern, $sIID_IUIAutomationValuePattern, $dtagIUIAutomationValuePattern ) If Not IsObj( $oValuePattern ) Then Return ConsoleWrite( "$oValuePattern ERR" & @CRLF ) ConsoleWrite( "$oValuePattern OK" & @CRLF ) ; --- Value Pattern (action) Methods --- ConsoleWrite( "--- Value Pattern (action) Methods ---" & @CRLF ) $oValuePattern.SetValue(bstr) Delete the two comments, two ConsoleWrites and empty lines. Replace "bstr" with "HelloWorld". Move code into function: Local $pValuePattern, $oValuePattern $oEdit.GetCurrentPattern( $UIA_ValuePatternId, $pValuePattern ) $oValuePattern = ObjCreateInterface( $pValuePattern, $sIID_IUIAutomationValuePattern, $dtagIUIAutomationValuePattern ) If Not IsObj( $oValuePattern ) Then Return ConsoleWrite( "$oValuePattern ERR" & @CRLF ) ConsoleWrite( "$oValuePattern OK" & @CRLF ) $oValuePattern.SetValue( "HelloWorld" ) Close the previous Notepad, run the code, check that SciTE output is OK, check "HelloWorld" in Notepad Edit control. All code after the third step: #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 ;#AutoIt3Wrapper_UseX64=n ; If target application is running as 32 bit code ;#AutoIt3Wrapper_UseX64=y ; If target application is running as 64 bit code #include "..\..\..\..\Includes\CUIAutomation2.au3" Opt( "MustDeclareVars", 1 ) Example() Func Example() ; Open Notepad Run( "Notepad" ) Sleep( 1000 ) ; 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 Desktop element Local $pDesktop, $oDesktop $oUIAutomation.GetRootElement( $pDesktop ) $oDesktop = ObjCreateInterface( $pDesktop, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oDesktop ) Then Return ConsoleWrite( "$oDesktop ERR" & @CRLF ) ConsoleWrite( "$oDesktop OK" & @CRLF ) ; --- Find Notepad window --- ConsoleWrite( "--- Find Notepad window ---" & @CRLF ) Local $pCondition0 $oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "Notepad", $pCondition0 ) If Not $pCondition0 Then Return ConsoleWrite( "$pCondition0 ERR" & @CRLF ) ConsoleWrite( "$pCondition0 OK" & @CRLF ) Local $pNotepad, $oNotepad $oDesktop.FindFirst( $TreeScope_Descendants, $pCondition0, $pNotepad ) $oNotepad = ObjCreateInterface( $pNotepad, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oNotepad ) Then Return ConsoleWrite( "$oNotepad ERR" & @CRLF ) ConsoleWrite( "$oNotepad OK" & @CRLF ) ; --- Fill Edit control --- ConsoleWrite( "--- Fill Edit control ---" & @CRLF ) $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "15", $pCondition0 ) If Not $pCondition0 Then Return ConsoleWrite( "$pCondition0 ERR" & @CRLF ) ConsoleWrite( "$pCondition0 OK" & @CRLF ) Local $pEdit, $oEdit $oNotepad.FindFirst( $TreeScope_Descendants, $pCondition0, $pEdit ) $oEdit = ObjCreateInterface( $pEdit, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oEdit ) Then Return ConsoleWrite( "$oEdit ERR" & @CRLF ) ConsoleWrite( "$oEdit OK" & @CRLF ) Local $pValuePattern, $oValuePattern $oEdit.GetCurrentPattern( $UIA_ValuePatternId, $pValuePattern ) $oValuePattern = ObjCreateInterface( $pValuePattern, $sIID_IUIAutomationValuePattern, $dtagIUIAutomationValuePattern ) If Not IsObj( $oValuePattern ) Then Return ConsoleWrite( "$oValuePattern ERR" & @CRLF ) ConsoleWrite( "$oValuePattern OK" & @CRLF ) $oValuePattern.SetValue( "HelloWorld" ) EndFunc SciTE output: $oUIAutomation OK $oDesktop OK --- Find Notepad window --- $pCondition0 OK $oNotepad OK --- Fill Edit control --- $pCondition0 OK $oEdit OK $oValuePattern OK1 point
-
Automating Notepad This example is a response to a request by mLipok for a simple Notepad automation. Task: Create a simple script to fill up "Edit1" with HelloWorld, click "Save As...", enter filename and finally save it to disc. This involves automating Notepad or more generally an Edit control, it involves automating a menu and a Save As dialog. Note that "Edit1" (from AutoIt Window Info tool) is not a valid control identification in UI Automation code. Do not open Notepad in advance. This will be done in the code. Start the code by opening Notepad and create the UI Automation object through IUIAutomation interface. I almost always create the Desktop element. Note how you first get a pointer to the element (GetRootElement method of $oUIAutomation object), and then creates the object that represents the element through IUIAutomationElement interface. UI Automation elements are nearly always created this way through a pointer. #include "..\..\Includes\CUIAutomation2.au3" Example() Func Example() ; Open Notepad Run( "Notepad" ) Sleep( 1000 ) ; 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 Desktop element Local $pDesktop, $oDesktop $oUIAutomation.GetRootElement( $pDesktop ) $oDesktop = ObjCreateInterface( $pDesktop, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oDesktop ) Then Return ConsoleWrite( "$oDesktop ERR" & @CRLF ) ConsoleWrite( "$oDesktop OK" & @CRLF ) EndFunc Open Examples\Notepad\Notepad.au3 (empty) in SciTE. Add the code, run the code with F5 and leave Notepad open. Check that SciTE output is OK. Next step is to get the Notepad window as an UI Automation element. Open UIASpy (included in zip-file, keep UIASpy open throughout the entire example), place mouse cursor over Notepad title bar and press F2: (You can delete other windows in UIASpy by right-clicking treeview top windows (level one items).) $UIA_ClassNamePropertyId = "Notepad" is used to create a property condition to identify Notepad. Note that you can copy this line in UIASpy listview: Select the line in the listview, click "Right pane" item in main menu, click "Copy all or selected items to clipboard", paste the line into your code editor. Add this code to the existing code: ; --- Notepad window --- ConsoleWrite( "--- Notepad window ---" & @CRLF ) Local $pCondition ; Note that $UIA_ClassNamePropertyId maybe ia a CASE SENSITIVE condition $oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "Notepad", $pCondition ) If Not $pCondition Then Return ConsoleWrite( "$pCondition ERR" & @CRLF ) ConsoleWrite( "$pCondition OK" & @CRLF ) Local $pNotepad, $oNotepad $oDesktop.FindFirst( $TreeScope_Descendants, $pCondition, $pNotepad ) $oNotepad = ObjCreateInterface( $pNotepad, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oNotepad ) Then Return ConsoleWrite( "$oNotepad ERR" & @CRLF ) ConsoleWrite( "$oNotepad OK" & @CRLF ) EndFunc Note again how we first get a pointer to Notepad (FindFirst method of $oDesktop object, $oDesktop because the Desktop is the parent element of Notepad) and then creates the object. Also note that $oDesktop and $oNotepad are created in exactly the same way. Close the previous Notepad, run the code and leave Notepad open. Check that SciTE output is OK. Now, the actual automation starts: Fill up "Edit1" with HelloWorld. An automation task is almost always done in two or three steps: 1. Create condition to find element 2. Find the UI Automation element 3. Extract info or perform action Each of these steps can usually be done in 1 - 3 lines of UI Automation code. All three steps can usually be done in less that 10 lines of UI Automation code (common AutoIt debug/info code not included). To get the Notepad window above we only used two steps. The first automation task is to perform an action to fill out the Edit control with "HelloWorld". Place mouse cursor in the Edit control and press F1: (If there are two or more Notepad windows in UIASpy, you can delete the old windows (red) by right-clicking the treeview.) This time we can use $UIA_AutomationIdPropertyId = "15" to create a property condition to identify the Edit control. Add this code: ; --- Fill Edit element --- ConsoleWrite( "--- Fill Edit element ---" & @CRLF ) ; Note that $UIA_AutomationIdPropertyId is a STRING and maybe a CASE SENSITIVE condition $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "15", $pCondition ) If Not $pCondition Then Return ConsoleWrite( "$pCondition ERR" & @CRLF ) ConsoleWrite( "$pCondition OK" & @CRLF ) Local $pEdit, $oEdit $oNotepad.FindFirst( $TreeScope_Descendants, $pCondition, $pEdit ) $oEdit = ObjCreateInterface( $pEdit, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oEdit ) Then Return ConsoleWrite( "$oEdit ERR" & @CRLF ) ConsoleWrite( "$oEdit OK" & @CRLF ) EndFunc $oNotepad.FindFirst means that the search for the Edit control starts in the Notepad window. $oEdit reprecents the Edit control. Close the previous Notepad, run the code and leave Notepad open. Check that SciTE output is OK. The Edit control must be filled with "HelloWorld". This is an action that has to be performed on the Edit control. The following actions are available: (Scroll down UIASpy listview to see Control Patterns (actions).) The interesting action or pattern here is the ValuePattern. The action is created and performed this way: Local $pValue, $oValue $oEdit.GetCurrentPattern( $UIA_ValuePatternId, $pValue ) $oValue = ObjCreateInterface( $pValue, $sIID_IUIAutomationValuePattern, $dtagIUIAutomationValuePattern ) If Not IsObj( $oValue ) Then Return ConsoleWrite( "$oValue ERR" & @CRLF ) ConsoleWrite( "$oValue OK" & @CRLF ) $oValue.SetValue( "HelloWorld" ) EndFunc $oEdit.GetCurrentPattern means that a pattern is created that performs actions on the Edit control. GetCurrentPattern method returns a pointer where from the ValuePattern object ($oValue) can be created. $UIA_ValuePatternId and the ValuePattern interface is defined in CUIAutomation2.au3. The $oValue object represents the ValuePattern interface for the Edit control. $oValue.SetValue( "HelloWorld" ) sets the text in the Edit control. An action or pattern is almost always created and performed this way. Add the code, close the previous Notepad, run the code and leave Notepad open. Open File menu. Place mouse cursor over the File menu and press F1: (Scroll down UIASpy listview to see Control Patterns (actions) in bottom of image.) ; --- Open File menu --- ConsoleWrite( "--- Open File menu ---" & @CRLF ) Local $pCondition1 $oUIAutomation.CreatePropertyCondition( $UIA_ControlTypePropertyId, $UIA_MenuItemControlTypeId, $pCondition1 ) If Not $pCondition1 Then Return ConsoleWrite( "$pCondition1 ERR" & @CRLF ) ConsoleWrite( "$pCondition1 OK" & @CRLF ) Local $pCondition2 ; $UIA_NamePropertyId is LOCALIZED and maybe CASE SENSITIVE $oUIAutomation.CreatePropertyCondition( $UIA_NamePropertyId, "File", $pCondition2 ) ; File <<<<<<<<<<<<<<<<<<<< If Not $pCondition2 Then Return ConsoleWrite( "$pCondition2 ERR" & @CRLF ) ConsoleWrite( "$pCondition2 OK" & @CRLF ) ; And condition $oUIAutomation.CreateAndCondition( $pCondition1, $pCondition2, $pCondition ) If Not $pCondition Then Return ConsoleWrite( "$pCondition ERR" & @CRLF ) ConsoleWrite( "$pCondition OK" & @CRLF ) Local $pFile, $oFile $oNotepad.FindFirst( $TreeScope_Descendants, $pCondition, $pFile ) $oFile = ObjCreateInterface( $pFile, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oFile ) Then Return ConsoleWrite( "$oFile ERR" & @CRLF ) ConsoleWrite( "$oFile OK" & @CRLF ) Local $pInvoke, $oInvoke $oFile.GetCurrentPattern( $UIA_InvokePatternId, $pInvoke ) $oInvoke = ObjCreateInterface( $pInvoke, $sIID_IUIAutomationInvokePattern, $dtagIUIAutomationInvokePattern ) If Not IsObj( $oInvoke ) Then Return ConsoleWrite( "$oInvoke ERR" & @CRLF ) ConsoleWrite( "$oInvoke OK" & @CRLF ) $oInvoke.Invoke() Sleep( 100 ) EndFunc The Invoke action or pattern is used to click and open the File menu. Note the similarity between the code for InvokePattern and ValuePattern above. When a menu, dialog box or child window is opened it need some time to open. You must add a Sleep statement. Add the code, change "File" to the corresponding word in your own language (copy the word from the listview), close the previous Notepad (don't save), run the code and leave Notepad open. Click "Save As..." menu. If the File menu that was opened above is closed, then open it manually with a mouse click. Place mouse cursor over the "Save As..." menu and press F1: ; --- Click "Save As..." menu --- ConsoleWrite( "--- Click ""Save As..."" menu ---" & @CRLF ) ; Reuse $pCondition1 above ; Note that $UIA_AutomationIdPropertyId is a STRING and maybe a CASE SENSITIVE condition $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "4", $pCondition2 ) If Not $pCondition2 Then Return ConsoleWrite( "$pCondition2 ERR" & @CRLF ) ConsoleWrite( "$pCondition2 OK" & @CRLF ) ; And condition $oUIAutomation.CreateAndCondition( $pCondition1, $pCondition2, $pCondition ) If Not $pCondition Then Return ConsoleWrite( "$pCondition ERR" & @CRLF ) ConsoleWrite( "$pCondition OK" & @CRLF ) Local $pSaveAs, $oSaveAs $oNotepad.FindFirst( $TreeScope_Descendants, $pCondition, $pSaveAs ) $oSaveAs = ObjCreateInterface( $pSaveAs, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oSaveAs ) Then Return ConsoleWrite( "$oSaveAs ERR" & @CRLF ) ConsoleWrite( "$oSaveAs OK" & @CRLF ) $oSaveAs.GetCurrentPattern( $UIA_InvokePatternId, $pInvoke ) $oInvoke = ObjCreateInterface( $pInvoke, $sIID_IUIAutomationInvokePattern, $dtagIUIAutomationInvokePattern ) If Not IsObj( $oInvoke ) Then Return ConsoleWrite( "$oInvoke ERR" & @CRLF ) ConsoleWrite( "$oInvoke OK" & @CRLF ) $oInvoke.Invoke() Sleep( 1000 ) EndFunc Note how similar the "Open File menu" code and the "Click Save As... menu" code is. Note also the long Sleep because it's a "Save As" dialog with a large number of elements. Add the code, close the previous Notepad (don't save), run the code and leave Notepad and Save As open. Save As window. Place mouse cursor over Save As title bar and press F2: ; --- Save As window --- ConsoleWrite( "--- Save As window ---" & @CRLF ) ; Note that $UIA_ClassNamePropertyId maybe ia a CASE SENSITIVE condition $oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "#32770", $pCondition ) If Not $pCondition Then Return ConsoleWrite( "$pCondition ERR" & @CRLF ) ConsoleWrite( "$pCondition OK" & @CRLF ) Local $pSaveAsWin, $oSaveAsWin $oNotepad.FindFirst( $TreeScope_Descendants, $pCondition, $pSaveAsWin ) $oSaveAsWin = ObjCreateInterface( $pSaveAsWin, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oSaveAsWin ) Then Return ConsoleWrite( "$oSaveAsWin ERR" & @CRLF ) ConsoleWrite( "$oSaveAsWin OK" & @CRLF ) EndFunc The rest of the automation tasks is about the Save As window. Therefore we want to use the Save As window as the starting point. Not Notepad. Add the code, close the previous Save As and Notepad (don't save), run the code and leave Notepad and Save As open. Set File name. Place mouse cursor over File name Edit control and press F1: ; --- Set File name --- ConsoleWrite( "--- Set File name ---" & @CRLF ) ; Note that $UIA_AutomationIdPropertyId is a STRING and maybe a CASE SENSITIVE condition $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "1001", $pCondition ) If Not $pCondition Then Return ConsoleWrite( "$pCondition ERR" & @CRLF ) ConsoleWrite( "$pCondition OK" & @CRLF ) $oSaveAsWin.FindFirst( $TreeScope_Descendants, $pCondition, $pEdit ) $oEdit = ObjCreateInterface( $pEdit, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oEdit ) Then Return ConsoleWrite( "$oEdit ERR" & @CRLF ) ConsoleWrite( "$oEdit OK" & @CRLF ) $oEdit.GetCurrentPattern( $UIA_ValuePatternId, $pValue ) $oValue = ObjCreateInterface( $pValue, $sIID_IUIAutomationValuePattern, $dtagIUIAutomationValuePattern ) If Not IsObj( $oValue ) Then Return ConsoleWrite( "$oValue ERR" & @CRLF ) ConsoleWrite( "$oValue OK" & @CRLF ) $oValue.SetValue( "HelloWorld.txt" ) EndFunc $oSaveAsWin.FindFirst searches in the Save As window. $oEdit is now the File name Edit control in the Save As window. The file name is set to "HelloWorld.txt". Add the code, close the previous Save As and Notepad (don't save), run the code and leave Notepad and Save As open. Click Save button. Place mouse cursor over Save button and press F1: ; --- Click Save button --- ConsoleWrite( "--- Click Save button ---" & @CRLF ) $oUIAutomation.CreatePropertyCondition( $UIA_ControlTypePropertyId, $UIA_ButtonControlTypeId, $pCondition1 ) If Not $pCondition1 Then Return ConsoleWrite( "$pCondition1 ERR" & @CRLF ) ConsoleWrite( "$pCondition1 OK" & @CRLF ) ; Note that $UIA_AutomationIdPropertyId is a STRING and maybe a CASE SENSITIVE condition $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "1", $pCondition2 ) If Not $pCondition2 Then Return ConsoleWrite( "$pCondition2 ERR" & @CRLF ) ConsoleWrite( "$pCondition2 OK" & @CRLF ) ; And condition $oUIAutomation.CreateAndCondition( $pCondition1, $pCondition2, $pCondition ) If Not $pCondition Then Return ConsoleWrite( "$pCondition ERR" & @CRLF ) ConsoleWrite( "$pCondition OK" & @CRLF ) Local $pSave, $oSave $oSaveAsWin.FindFirst( $TreeScope_Descendants, $pCondition, $pSave ) $oSave = ObjCreateInterface( $pSave, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oSave ) Then Return ConsoleWrite( "$oSave ERR" & @CRLF ) ConsoleWrite( "$oSave OK" & @CRLF ) $oSave.GetCurrentPattern( $UIA_InvokePatternId, $pInvoke ) $oInvoke = ObjCreateInterface( $pInvoke, $sIID_IUIAutomationInvokePattern, $dtagIUIAutomationInvokePattern ) If Not IsObj( $oInvoke ) Then Return ConsoleWrite( "$oInvoke ERR" & @CRLF ) ConsoleWrite( "$oInvoke OK" & @CRLF ) $oInvoke.Invoke() EndFunc Add the code, close the previous Save As and Notepad (don't save) and run the code. All code (Examples\Notepad\NotepadAll.au3): #include "..\..\Includes\CUIAutomation2.au3" Example() Func Example() ; Open Notepad Run( "Notepad" ) Sleep( 1000 ) ; 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 Desktop element Local $pDesktop, $oDesktop $oUIAutomation.GetRootElement( $pDesktop ) $oDesktop = ObjCreateInterface( $pDesktop, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oDesktop ) Then Return ConsoleWrite( "$oDesktop ERR" & @CRLF ) ConsoleWrite( "$oDesktop OK" & @CRLF ) ; --- Notepad window --- ConsoleWrite( "--- Notepad window ---" & @CRLF ) Local $pCondition ; Note that $UIA_ClassNamePropertyId maybe ia a CASE SENSITIVE condition $oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "Notepad", $pCondition ) If Not $pCondition Then Return ConsoleWrite( "$pCondition ERR" & @CRLF ) ConsoleWrite( "$pCondition OK" & @CRLF ) Local $pNotepad, $oNotepad $oDesktop.FindFirst( $TreeScope_Descendants, $pCondition, $pNotepad ) $oNotepad = ObjCreateInterface( $pNotepad, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oNotepad ) Then Return ConsoleWrite( "$oNotepad ERR" & @CRLF ) ConsoleWrite( "$oNotepad OK" & @CRLF ) ; --- Fill Edit element --- ConsoleWrite( "--- Fill Edit element ---" & @CRLF ) ; Note that $UIA_AutomationIdPropertyId is a STRING and maybe a CASE SENSITIVE condition $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "15", $pCondition ) If Not $pCondition Then Return ConsoleWrite( "$pCondition ERR" & @CRLF ) ConsoleWrite( "$pCondition OK" & @CRLF ) Local $pEdit, $oEdit $oNotepad.FindFirst( $TreeScope_Descendants, $pCondition, $pEdit ) $oEdit = ObjCreateInterface( $pEdit, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oEdit ) Then Return ConsoleWrite( "$oEdit ERR" & @CRLF ) ConsoleWrite( "$oEdit OK" & @CRLF ) Local $pValue, $oValue $oEdit.GetCurrentPattern( $UIA_ValuePatternId, $pValue ) $oValue = ObjCreateInterface( $pValue, $sIID_IUIAutomationValuePattern, $dtagIUIAutomationValuePattern ) If Not IsObj( $oValue ) Then Return ConsoleWrite( "$oValue ERR" & @CRLF ) ConsoleWrite( "$oValue OK" & @CRLF ) $oValue.SetValue( "HelloWorld" ) ; --- Open File menu --- ConsoleWrite( "--- Open File menu ---" & @CRLF ) Local $pCondition1 $oUIAutomation.CreatePropertyCondition( $UIA_ControlTypePropertyId, $UIA_MenuItemControlTypeId, $pCondition1 ) If Not $pCondition1 Then Return ConsoleWrite( "$pCondition1 ERR" & @CRLF ) ConsoleWrite( "$pCondition1 OK" & @CRLF ) Local $pCondition2 ; $UIA_NamePropertyId is LOCALIZED and maybe CASE SENSITIVE $oUIAutomation.CreatePropertyCondition( $UIA_NamePropertyId, "File", $pCondition2 ) ; File <<<<<<<<<<<<<<<<<<<< If Not $pCondition2 Then Return ConsoleWrite( "$pCondition2 ERR" & @CRLF ) ConsoleWrite( "$pCondition2 OK" & @CRLF ) ; And condition $oUIAutomation.CreateAndCondition( $pCondition1, $pCondition2, $pCondition ) If Not $pCondition Then Return ConsoleWrite( "$pCondition ERR" & @CRLF ) ConsoleWrite( "$pCondition OK" & @CRLF ) Local $pFile, $oFile $oNotepad.FindFirst( $TreeScope_Descendants, $pCondition, $pFile ) $oFile = ObjCreateInterface( $pFile, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oFile ) Then Return ConsoleWrite( "$oFile ERR" & @CRLF ) ConsoleWrite( "$oFile OK" & @CRLF ) Local $pInvoke, $oInvoke $oFile.GetCurrentPattern( $UIA_InvokePatternId, $pInvoke ) $oInvoke = ObjCreateInterface( $pInvoke, $sIID_IUIAutomationInvokePattern, $dtagIUIAutomationInvokePattern ) If Not IsObj( $oInvoke ) Then Return ConsoleWrite( "$oInvoke ERR" & @CRLF ) ConsoleWrite( "$oInvoke OK" & @CRLF ) $oInvoke.Invoke() Sleep( 100 ) ; --- Click "Save As..." menu --- ConsoleWrite( "--- Click ""Save As..."" menu ---" & @CRLF ) ; Reuse $pCondition1 above ; Note that $UIA_AutomationIdPropertyId is a STRING and maybe a CASE SENSITIVE condition $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "4", $pCondition2 ) If Not $pCondition2 Then Return ConsoleWrite( "$pCondition2 ERR" & @CRLF ) ConsoleWrite( "$pCondition2 OK" & @CRLF ) ; And condition $oUIAutomation.CreateAndCondition( $pCondition1, $pCondition2, $pCondition ) If Not $pCondition Then Return ConsoleWrite( "$pCondition ERR" & @CRLF ) ConsoleWrite( "$pCondition OK" & @CRLF ) Local $pSaveAs, $oSaveAs $oNotepad.FindFirst( $TreeScope_Descendants, $pCondition, $pSaveAs ) $oSaveAs = ObjCreateInterface( $pSaveAs, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oSaveAs ) Then Return ConsoleWrite( "$oSaveAs ERR" & @CRLF ) ConsoleWrite( "$oSaveAs OK" & @CRLF ) $oSaveAs.GetCurrentPattern( $UIA_InvokePatternId, $pInvoke ) $oInvoke = ObjCreateInterface( $pInvoke, $sIID_IUIAutomationInvokePattern, $dtagIUIAutomationInvokePattern ) If Not IsObj( $oInvoke ) Then Return ConsoleWrite( "$oInvoke ERR" & @CRLF ) ConsoleWrite( "$oInvoke OK" & @CRLF ) $oInvoke.Invoke() Sleep( 1000 ) ; --- Save As window --- ConsoleWrite( "--- Save As window ---" & @CRLF ) ; Note that $UIA_ClassNamePropertyId maybe ia a CASE SENSITIVE condition $oUIAutomation.CreatePropertyCondition( $UIA_ClassNamePropertyId, "#32770", $pCondition ) If Not $pCondition Then Return ConsoleWrite( "$pCondition ERR" & @CRLF ) ConsoleWrite( "$pCondition OK" & @CRLF ) Local $pSaveAsWin, $oSaveAsWin $oNotepad.FindFirst( $TreeScope_Descendants, $pCondition, $pSaveAsWin ) $oSaveAsWin = ObjCreateInterface( $pSaveAsWin, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oSaveAsWin ) Then Return ConsoleWrite( "$oSaveAsWin ERR" & @CRLF ) ConsoleWrite( "$oSaveAsWin OK" & @CRLF ) ; --- Set File name --- ConsoleWrite( "--- Set File name ---" & @CRLF ) ; Note that $UIA_AutomationIdPropertyId is a STRING and maybe a CASE SENSITIVE condition $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "1001", $pCondition ) If Not $pCondition Then Return ConsoleWrite( "$pCondition ERR" & @CRLF ) ConsoleWrite( "$pCondition OK" & @CRLF ) $oSaveAsWin.FindFirst( $TreeScope_Descendants, $pCondition, $pEdit ) $oEdit = ObjCreateInterface( $pEdit, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oEdit ) Then Return ConsoleWrite( "$oEdit ERR" & @CRLF ) ConsoleWrite( "$oEdit OK" & @CRLF ) $oEdit.GetCurrentPattern( $UIA_ValuePatternId, $pValue ) $oValue = ObjCreateInterface( $pValue, $sIID_IUIAutomationValuePattern, $dtagIUIAutomationValuePattern ) If Not IsObj( $oValue ) Then Return ConsoleWrite( "$oValue ERR" & @CRLF ) ConsoleWrite( "$oValue OK" & @CRLF ) $oValue.SetValue( "HelloWorld.txt" ) ; --- Click Save button --- ConsoleWrite( "--- Click Save button ---" & @CRLF ) $oUIAutomation.CreatePropertyCondition( $UIA_ControlTypePropertyId, $UIA_ButtonControlTypeId, $pCondition1 ) If Not $pCondition1 Then Return ConsoleWrite( "$pCondition1 ERR" & @CRLF ) ConsoleWrite( "$pCondition1 OK" & @CRLF ) ; Note that $UIA_AutomationIdPropertyId is a STRING and maybe a CASE SENSITIVE condition $oUIAutomation.CreatePropertyCondition( $UIA_AutomationIdPropertyId, "1", $pCondition2 ) If Not $pCondition2 Then Return ConsoleWrite( "$pCondition2 ERR" & @CRLF ) ConsoleWrite( "$pCondition2 OK" & @CRLF ) ; And condition $oUIAutomation.CreateAndCondition( $pCondition1, $pCondition2, $pCondition ) If Not $pCondition Then Return ConsoleWrite( "$pCondition ERR" & @CRLF ) ConsoleWrite( "$pCondition OK" & @CRLF ) Local $pSave, $oSave $oSaveAsWin.FindFirst( $TreeScope_Descendants, $pCondition, $pSave ) $oSave = ObjCreateInterface( $pSave, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) If Not IsObj( $oSave ) Then Return ConsoleWrite( "$oSave ERR" & @CRLF ) ConsoleWrite( "$oSave OK" & @CRLF ) $oSave.GetCurrentPattern( $UIA_InvokePatternId, $pInvoke ) $oInvoke = ObjCreateInterface( $pInvoke, $sIID_IUIAutomationInvokePattern, $dtagIUIAutomationInvokePattern ) If Not IsObj( $oInvoke ) Then Return ConsoleWrite( "$oInvoke ERR" & @CRLF ) ConsoleWrite( "$oInvoke OK" & @CRLF ) $oInvoke.Invoke() EndFunc SciTE output: $oUIAutomation OK $oDesktop OK --- Notepad window --- $pCondition OK $oNotepad OK --- Fill Edit element --- $pCondition OK $oEdit OK $oValue OK --- Open File menu --- $pCondition1 OK $pCondition2 OK $pCondition OK $oFile OK $oInvoke OK --- Click "Save As..." menu --- $pCondition2 OK $pCondition OK $oSaveAs OK $oInvoke OK --- Save As window --- $pCondition OK $oSaveAsWin OK --- Set File name --- $pCondition OK $oEdit OK $oValue OK --- Click Save button --- $pCondition1 OK $pCondition2 OK $pCondition OK $oSave OK $oInvoke OK Maybe somebody can show how to implement such a Notepad automation with the functions in UIAWrappers.au3 and with classic code. Summary The two most important objects in UI Automation code are the $oUIAutomation and $oUIElement objects created from IUIAutomation and IUIAutomationElement interfaces. An UI Automation element object that represents a window or control is always created with the IUIAutomationElement interface through a pointer to the element object: $oDesktop = ObjCreateInterface( $pDesktop, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) $oNotepad = ObjCreateInterface( $pNotepad, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) $oEdit = ObjCreateInterface( $pEdit, $sIID_IUIAutomationElement, $dtagIUIAutomationElement ) An UI Automation task is almost always done in two or three steps: 1. Create condition to find element 2. Find the UI Automation element 3. Extract info or perform action Each of these steps can usually be done in 1 - 3 lines of UI Automation code. All three steps can usually be done in less that 10 lines of UI Automation code (common AutoIt debug/info code not included). UI Automation actions (patterns) are performed through pattern interfaces, objects and methods. The code is nearly always implemented this way: Fill an Edit control: Local $pValue, $oValue $oEdit.GetCurrentPattern( $UIA_ValuePatternId, $pValue ) $oValue = ObjCreateInterface( $pValue, $sIID_IUIAutomationValuePattern, $dtagIUIAutomationValuePattern ) If Not IsObj( $oValue ) Then Return ConsoleWrite( "$oValue ERR" & @CRLF ) ConsoleWrite( "$oValue OK" & @CRLF ) $oValue.SetValue( "HelloWorld" ) $oEdit is an Edit control. The $oValue object represents the ValuePattern interface for the Edit control. $oValue.SetValue() sets the text in the Edit control. Click a menu item: Local $pInvoke, $oInvoke $oFile.GetCurrentPattern( $UIA_InvokePatternId, $pInvoke ) $oInvoke = ObjCreateInterface( $pInvoke, $sIID_IUIAutomationInvokePattern, $dtagIUIAutomationInvokePattern ) If Not IsObj( $oInvoke ) Then Return ConsoleWrite( "$oInvoke ERR" & @CRLF ) ConsoleWrite( "$oInvoke OK" & @CRLF ) $oInvoke.Invoke() Sleep( 100 ) $oFile is a File menu. The $oInvoke object represents the InvokePattern interface for the File menu. $oInvoke.Invoke() clicks the File menu. Sleep( 100 ) gives the File menu time to open. Click a button control: $oSave.GetCurrentPattern( $UIA_InvokePatternId, $pInvoke ) $oInvoke = ObjCreateInterface( $pInvoke, $sIID_IUIAutomationInvokePattern, $dtagIUIAutomationInvokePattern ) If Not IsObj( $oInvoke ) Then Return ConsoleWrite( "$oInvoke ERR" & @CRLF ) ConsoleWrite( "$oInvoke OK" & @CRLF ) $oInvoke.Invoke() $oSave is a Save button. The $oInvoke object represents the InvokePattern interface for the Save button. $oInvoke.Invoke() clicks the Save button. When writing UI Automation code, it's easiest to write the code in small steps and test the code after each step. Error management based on checking objects with IsObj() and ConsoleWrite() is required and sufficient. It's very rare necessary to use an actual object (COM) error handler. Too small Sleep times after a menu, dialog or window is opened is a common error.1 point