Get window handles of multiple instances

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
timi2fly
Newbie
Posts: 15
Joined: Sun Mar 23, 2014 5:29 am
Location: Adelaide, South Australia

Get window handles of multiple instances

Post by timi2fly » Wed Jan 27, 2016 9:56 am

I have a program that has multiple instances open.

I want to retrieve the handles of each instance so that I can interact with each in my macroscheduler script. Each window has the same window name.

I was wondering if searching for window handles by window name was possible however I understand this will only retrieve the handle of the first one found.


Any other ideas?

timi2fly
Newbie
Posts: 15
Joined: Sun Mar 23, 2014 5:29 am
Location: Adelaide, South Australia

Re: Get window handles of multiple instances

Post by timi2fly » Wed Jan 27, 2016 11:15 am

My current working script works by:

- image recognition to detect and count each instance of the window
- using this image recognition, it brings each successive window into focus and retrieves the handle using WIN_USEHANDLE=1 and GetActiveWindow.
- each successive handle is then stored in an array

I'm sure that there is a more elegant way to achieve this, however it's working nicely so far.

User avatar
JRL
Automation Wizard
Posts: 3529
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Get window handles of multiple instances

Post by JRL » Wed Jan 27, 2016 1:55 pm

If you run this script while your windows are open does each window name display in the message?

Code: Select all

GetWindowList>vList
MDL>vList

timi2fly
Newbie
Posts: 15
Joined: Sun Mar 23, 2014 5:29 am
Location: Adelaide, South Australia

Re: Get window handles of multiple instances

Post by timi2fly » Thu Jan 28, 2016 1:02 am

JRL wrote:does each window name display in the message?
Unfortunately all instances of the software/window of interest are displayed the same

timi2fly
Newbie
Posts: 15
Joined: Sun Mar 23, 2014 5:29 am
Location: Adelaide, South Australia

Re: Get window handles of multiple instances

Post by timi2fly » Thu Jan 28, 2016 2:37 am

I have it sorted now without using image recognition.

Code: Select all

Let>WIN_USEHANDLE=1
GetWindowList>allWindows
Separate>allWindows,CRLF,winH
let>Win_USEHANDLE=0
If>winH_count>0
Let>k=0
Let>m=0
Repeat>k
  Let>k=k+1
  Let>this_windowH=winH_%k%
  GetWindowNames>this_windowH,strTitle,strClass

Let>substring=*WINDOWNAMEHERE*
Pos>%substring%,%strTitle%,1,posText
if>posText>0
    let>m=m+1
    Let>winN_%m%=%strTitle%
    Let>winHandle_%m%=winH_%k%
  else
endif

Until>k=winH_count
I end up with two arrays - one with the window names (for confirmation and debugging) and one with the handles.

Any suggestions for optimising this code or cleaning it up are always appreciated.

Tim

User avatar
JRL
Automation Wizard
Posts: 3529
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Get window handles of multiple instances

Post by JRL » Thu Jan 28, 2016 2:45 am

I see you just posted. Good job!

I had pretty much the same thing in mind. Only one list though.

Code: Select all

Let>WindowSought=The Window Name I Seek
Let>WindowHandleList=

Let>WIN_USEHANDLE=1
GetWindowList>vList

Separate>vList,crlf,vWinHndl

Let>kk=0
Repeat>kk
  Add>kk,1
  Let>value=vWinHndl_%kk%
  GetWindowNames>value,vTitle,vClass
  If>%vTitle%=%WindowSought%
    Concat>WindowHandleList,%value%%crlf%
  EndIf
Until>kk=vWinHndl_Count

MDL>WindowHandleList

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts