July 26, 2012

Retrieving all Items and Indexes from a Listbox

Filed under: Announcements,Scripting — Marcus Tettmar @ 11:26 am

Some people have reported issue with the GetListItem command failing to work with some applications. While we investigate the issues we’ve created a small workaround. This is a .Net tool called ListInspector. It’s a command line tool which you can pass a listbox handle to and it will dump out a list of all the items and their indexes.

Here’s an example of use:

//get handle of putty and find handle of listbox
GetWindowHandle>PuTTY Configuration,pHwnd
FindObject>pHWnd,ListBox,,1,hWnd,L,T,R,B,res

//run ListInspector to get a list of items
Let>RP_WINDOWMODE=0
Let>RP_WAIT=1
Run>cmd.exe /C "%SCRIPT_DIR%\ListInspector.exe" %hWnd% > %TEMP_DIR%\items.txt
ReadFile>%TEMP_DIR%\items.txt,items

//find the index of the item we want
Let>item_required=Pluto Server
RegEx>\d*(?=:%item_required%),items,0,matches,nm,0
If>nm>0
  Let>index=matches_1
  MessageModal>Index of %item_required% is %index%
Endif

The script here gets the items of the session selection box in the Putty SSH client. The first thing it does is get Putty’s window handle, then find the handle of the list box we want to inspect. It then runs ListInspector.exe and passes the listbox handle to it, piping the results to a temporary file. It then reads in the file and uses a RegEx to extract the index for the item we are looking for.

This is useful for those listboxes which don’t allow us to “drill down” properly and where the items and order of items may be dynamic, leaving us with no easy way to automate the selection of an item. Using ListInspector we can get the item’s index and then we know how many times we need to “Press Down” (first using Home to make sure we’re at the top).

You can download ListInspector here. The zip file contains the example script and a small readme file. .Net 2.5 or higher is required.