Jump to content

SetFocus on list item - Don't know what I'm doing wrong


Recommended Posts

Good Morning,

I'm trying to use the code I had previously to select an item from a list. I believe I want to "setfocus". However, I'm getting and error and I believe it's my code. I don't know what Global Variables I should be using really - and I'm not sure if "setfocus" is the right function there at the bottom of my code... Thank you all as always!!!

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

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

    ; Condition to find text "C:\Program Files (x86)" UIA_ListItemControlTypeId
    Local $pCondition, $pCondition1, $pCondition2
    $oUIAutomation.CreatePropertyCondition($UIA_ControlTypePropertyId, $UIA_ListItemControlTypeId, $pCondition1)
    $oUIAutomation.CreatePropertyCondition($UIA_NamePropertyId, "C:\Program Files (x86)", $pCondition2)
    $oUIAutomation.CreateAndCondition($pCondition1, $pCondition2, $pCondition)
    If Not $pCondition Then Return ConsoleWrite("C:\Program Files (x86) condition ERR" & @CRLF)
    ConsoleWrite("C:\Program Files (x86) condition OK" & @CRLF)

    ; Find "C:\Program Files (x86)" List Item
    Local $pListItem, $oListItem
    $oWindow.FindFirst($TreeScope_Descendants, $pCondition, $pListItem)
    $oListItem = ObjCreateInterface($pListItem, $sIID_IUIAutomationElement, $dtagIUIAutomationElement)
    If Not IsObj($oListItem) Then MsgBox(0, "", "Found List Item ERR")
    MsgBox(0, "", "Found List Item OK")

    ; Focus "C:\Program Files (x86)" ListItem
    Local $pFocus, $oFocus
    $oListItem.GetCurrentPattern($UIA_ListItemControlTypeId, $pFocus)
    $oFocus = ObjCreateInterface($pFocus, $sIID_IUIAutomationFocusChangedEventHandler, $dtagIUIAutomationFocusChangedEventHandler)
    If Not IsObj($oFocus) Then MsgBox(0, "", "Focus List Item ERR")
    MsgBox(0, "", "Focus List Item OK")
    $oFocus.setfocus()
    Sleep(2000)

FYI - I was trying the "invoke" method and the list item was actually being double clicked on - which wont work - doing invoke would bring up a new window and all I wanted to do was select the item from the list.

Edited by souldjer777

"Maybe I'm on a road that ain't been paved yet. And maybe I see a sign that ain't been made yet"
Song Title: I guess you could say
Artist: Middle Class Rut

Link to comment
Share on other sites

I'm messing around with the variables now... believe I had wrong variable - looking at "pattern" now...

; Focus "C:\Program Files (x86)" ListItem
    Local $pFocus, $oFocus
    $oListItem.GetCurrentPattern($UIA_SelectionItemPatternId, $pFocus)
    $oFocus = ObjCreateInterface($pFocus, $sIID_IUIAutomationSelectionItemPattern, $dtagIUIAutomationSelectionItemPattern)
    If Not IsObj($oFocus) Then MsgBox(0, "", "Focus List Item ERR")
    MsgBox(0, "", "Focus List Item OK")
    $oFocus.setfocus()
    Sleep(2000)

Good that I was able to get an object now - but getting error... "The requested action with this object has failed."

Edited by souldjer777

"Maybe I'm on a road that ain't been paved yet. And maybe I see a sign that ain't been made yet"
Song Title: I guess you could say
Artist: Middle Class Rut

Link to comment
Share on other sites

Trying the COM error function to get more details... lol... not a lot to go on imo.

---------------------------
AutoItCOM Test
---------------------------
We intercepted a COM Error !

err.description is:     

err.windescription:    Unknown name.

err.number is:     80020006

err.lastdllerror is:     0

err.scriptline is:     -1

err.source is:     

err.helpfile is:     

err.helpcontext is:     
---------------------------
OK   
---------------------------

"Maybe I'm on a road that ain't been paved yet. And maybe I see a sign that ain't been made yet"
Song Title: I guess you could say
Artist: Middle Class Rut

Link to comment
Share on other sites

Dang, may have still had wrong Global variables - doh!

$UIA_SelectionPatternId, $sIID_IUIAutomationSelectionPattern, $dtagIUIAutomationSelectionPattern

 

"Maybe I'm on a road that ain't been paved yet. And maybe I see a sign that ain't been made yet"
Song Title: I guess you could say
Artist: Middle Class Rut

Link to comment
Share on other sites

Note that focused item and selected item may not necessarily be the same, but often it is. The focused item is the item with the dotted rectangle. Only one item can have focus. A selected item is drawn on dark blue background with white text. Many items can be selected at once. When you click an item, it's usually both selected and focused.

The code. Replace the Focus section with this:

; Select "C:\Program Files (x86)" ListItem
Local $pSelection, $oSelection
$oListItem.GetCurrentPattern( $UIA_SelectionItemPatternId, $pSelection )
$oSelection = ObjCreateInterface( $pSelection, $sIID_IUIAutomationSelectionItemPattern, $dtagIUIAutomationSelectionItemPattern )
$oSelection.AddToSelection()

 

Link to comment
Share on other sites

Thank you LarsJ - I'm tryin' here :) I'm really trying - been a while since I've automated stuff and I like your code / error checking the most.

:bye:

Edited by souldjer777

"Maybe I'm on a road that ain't been paved yet. And maybe I see a sign that ain't been made yet"
Song Title: I guess you could say
Artist: Middle Class Rut

Link to comment
Share on other sites

Create an account or sign in to comment

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

Create an account

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

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...