Sign in to follow this  
Followers 0
rayzer

Winhandle(s)/Help With Script

25 posts in this topic

#1 ·  Posted (edited)

Good afternoon all.

I'm new to the world of scripting and have been frequenting the site for the last three weeks in order to educate myself whilst doing a project. I have searched the forum and sampled many things in the help file but cannot find an answer to a nagging problem. So here I am, asking the experts. :)

The script I created works flawlessly if I use notepad as the example, but once put into practice with the actual program, the windows that I assign to open in various places go out of sync. By out of sync I mean - The first window opens in the correct position and navigates to the correct menu. When the second window is set to open, the first jumps across the screen and throws the whole script out. As I said, no problems when sampling with notepad.

Does Wingethandle assign a specific window, to a specific destination, if so, please help me understand and use it correctly. The window titles used in the following script are the exact match and the application I'm trying to run.

$g_szVersion = "DeanGr Startup" ;checks to see if a single instance of the script is running

If WinExists($g_szVersion) Then Exit

AutoItWinSetTitle($g_szVersion)

Run ( "C:\Program Files\Tun\Emul\emul32.exe CS3-v3.cfg" );path/application to run

Opt("WinTitleMatchMode", 3)

WinWaitActive ( "CS3-v3 - Tun EMUL - [CS3v3]", "" );title of open window

WinMove ( "CS3-v3 - Tun EMUL - [CS3v3]", "", 0, 0, 652, 483 );windows position/size

Sleep ( 2000 )

Send ( "xxx" );username

Sleep ( 1000 )

Send ( "{enter}" )

Sleep ( 1000 )

Send ( "xxx" );password

Send ( "{enter}" )

Sleep ( 1000 )

Send ( "{up}" );keypress to 'Ran Enquiry'

Send ( "{right}" );keypress to 'Ran Enquiry'

Send ( "{down}" );keypress to 'Ran Enquiry'

Send ( "{right}" );keypress to 'Ran Enquiry'

Sleep ( 2000 )

Run ( "C:\Program Files\Tun\Emul\emul32.exe CS3-v3.cfg" );path/application to run

Opt("WinTitleMatchMode", 3)

WinWaitActive ( "CS3-v3 - Tun EMUL - [CS3v3]", "" );title of open window

WinMove ( "CS3-v3 - Tun EMUL - [CS3v3]", "", 460, 0, 652, 483 );windows position/size

Sleep ( 2000 )

Send ( "xxx" );username

Sleep ( 1000 )

Send ( "{enter}" )

Sleep ( 1000 )

Send ( "xxx" );password

Send ( "{enter}" )

Sleep ( 1000 )

Send ( "{down}" );keypress to 'Receipts'

Send ( "{right}" );keypress to 'Receipts'

Send ( "{down 2}" );keypress to 'Receipts'

Send ( "{right 2}" );keypress to 'Receipts'

Send ( "{up}" );keypress to 'Receipts'

Send ( "{right}" );keypress to 'Receipts'

Sleep ( 1000 )

Run ( "C:\Program Files\Tun\Emul\emul32.exe CS3-v3.cfg" );path/application to run

Opt("WinTitleMatchMode", 3)

WinWaitActive ( "CS3-v3 - Tun EMUL - [CS3v3]", "" );title of open window

WinMove ( "CS3-v3 - Tun EMUL - [CS3v3]", "", 460, 300, 652, 483 );windows position/size

Sleep ( 2000 )

Send ( "xxx" );username

Sleep ( 1000 )

Send ( "{enter}" )

Sleep ( 1000 )

Send ( "xxx" );password

Send ( "{enter}" )

Sleep ( 1000 )

Send ( "{down}" );keypress to 'Transfers'

Send ( "{right}" );keypress to 'Transfers'

Send ( "{down}" );keypress to 'Transfers'

Send ( "{right 3}" );keypress to 'Transfers'

Sleep ( 1000 )

Run ( "C:\Program Files\Tun\Emul\emul32.exe CS3-v3.cfg" );path/application to run

Opt("WinTitleMatchMode", 3)

WinWaitActive ( "CS3-v3 - Tun EMUL - [CS3v3]", "" );title of open window

WinMove ( "CS3-v3 - Tun EMUL - [CS3v3]", "" , 460, 300, 652, 483 );windows position/size

Sleep ( 2000 )

Send ( "xxx" );username

Sleep ( 1000 )

Send ( "{enter}" )

Sleep ( 1000 )

Send ( "xxx" );password

Send ( "{enter}" )

Sleep ( 1000 )

Send ( "{down}" );keypress to 'Stock Enquiry'

Send ( "{right}" );keypress to 'Stock Enquiry'

Send ( "{down}" );keypress to 'Stock Enquiry'

Send ( "{right}" );keypress to 'Stock Enquiry'

Send ( "{down}" );keypress to 'Stock Enquiry'

Send ( "{right}" );keypress to 'Stock Enquiry'

Send ( "{down}" );keypress to 'Stock Enquiry'

Send ( "{right}" );keypress to 'Stock Enquiry'

Send ( "{f9}" );keypress to 'Stock Enquiry'

Sleep ( 1000 )

Run ( "C:\Program Files\Tun\Emul\emul32.exe CS3-v3.cfg" );path/application to run

Opt("WinTitleMatchMode", 3)

WinWaitActive ( "CS3-v3 - Tun EMUL - [CS3v3]", "" );title of open window

WinMove ( "CS3-v3 - Tun EMUL - [CS3v3]", "", 460, 300, 652, 483 );windows position/size

Sleep ( 2000 )

Send ( "xxx" );username

Sleep ( 1000 )

Send ( "{enter}" )

Sleep ( 1000 )

Send ( "xxx" );password

Send ( "{enter}" )

Sleep ( 1000 )

Send ( "{down}" );keypress to 'Copy Stock To Warehouse'

Send ( "{right}" );keypress to 'Copy Stock To Warehouse'

Send ( "{down}" );keypress to 'Copy Stock To Warehouse'

Send ( "{right}" );keypress to 'Copy Stock To Warehouse'

Send ( "{down 2 }" );keypress to 'Copy Stock To Warehouse'

Send ( "{right 2}" );keypress to 'Copy Stock To Warehouse'

Edited by rayzer

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Basically your problem is you have 2 windows with the exact same name (unless I misunderstood).. thus you need to differentiate them to get the right handle. You might wanna take a look at this thread.

Edited by MikeP

Share this post


Link to post
Share on other sites

Welcome to the forums.

Two things:

1. You only need to declare the Opt once at the top of your script, it will persist throughout unless you change it with another.

2. Since your Window Titles all appear to be the same, the script is probably getting confused. There's probably a lot of different ways to handle this, maybe something like this:

Run(Whatever..)
Sleep(1000);  Sleep for a second or two, however long it takes to ensure that the program you just ran is now the active window
$hWnd = WinGetHandle("")  ; Get a handle to the active window.  This will be unique, no matter what the Title is.  This way you ensure your script won't become confused by the same Window Title.
WinMove($hWnd,"",x,y)

Run(Whatever..)
Sleep(1000)
$hWnd2 = WinGethandle("")  ; You may not need to make a second variable, but just incase you want to do more stuff with this particular window later on..
WinMove($hWnd2,"",x,y)

Share this post


Link to post
Share on other sites

Hi.

I was just wondering if there is a way to find the window handle for a given PID. That would make it very easy, as

PID1=Run("program param")

will make the program's PID available. That would be basically the opposite function of

WinGetProcess ( "title" [, "text"] ) :)

Beside this, in the <misc.au3> you'll find the fuction _singleton() :(

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

Yes thanks, Mike.

I have five windows with the exact names. I'll look at the thread you linked me to. :)

_____

Covaks.

Without sounding like a complete idiot, no wait... I am. :( . Do I need to open 5 instances of the program in question to get their unique handle(s) and incorporate them into the script. Why I ask is, once the window closes and a new one opens, doesn't the handle change?

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Without sounding like a complete idiot, no wait... I am. :) . Do I need to open 5 instances of the program in question to get their unique handle(s) and incorporate them into the script. Why I ask is, once the window closes and a new one opens, doesn't the handle change?

Yup.. the handle will change. But as Covaks said.. if you run your window with your script then you can get the handle right after you've done it.

It gets only more tricky if you launch your applications without your script and THEN your script gotta guess which is what.. :(

Edited by MikeP

Share this post


Link to post
Share on other sites

Here's an example of how to get a window handle from a pid

opt("WinTitleMatchMode",4)

$PID = Run("Notepad")
Sleep(2000)

$hWnd = _GetHandleFromPid($PID)

WinMove($hWnd,"",1,1)

Func _GetHandleFromPid($PID)
    Local $aWinList
    $aWinList = WinList()
    
    For $x = 1 to $aWinList[0][0]
        If WinGetProcess($aWinList[$x][0]) = $PID Then Return $aWinList[$x][1]
    Next
        
    Return -1
EndFunc

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Thanks guys, great help. :)

Script will be tested tomorrow when I get to work. I've used the example posted by Covaks. This is how it looks x5 windows (different co-ords):

Run ( "C:\Program Files\Tun\Emul\emul32.exe CS3-v3.cfg" )
Sleep( 1000 )
$hWnd = WinGetHandle( "0x00760238" );example handle for window
WinMove($hWnd, "0x00760238", 0, 0, 652, 483 );example handle/windows position co-ords

I hope that's right. Do I still need to use one line in the script for the Opt for title matching or is it not needed?

Edited by rayzer

Share this post


Link to post
Share on other sites

If you don't put the Opt("WinTitleMatchMode",x) line then the default title recognition is mode 1.. only the starting characters of your title are needed to find the window.. personnally i'm always using mode 2 which allows to use any substring of a title to find a window. Just put the Opt line at the start of the script once and then you can forget it.. no need to put it 5 times..

Share this post


Link to post
Share on other sites

#10 ·  Posted

Thanks guys, great help. :)

Script will be tested tomorrow when I get to work. I've used the example posted by Covaks. This is how it looks x5 windows (different co-ords):

Run ( "C:\Program Files\Tun\Emul\emul32.exe CS3-v3.cfg" )
Sleep( 1000 )
$hWnd = WinGetHandle( "0x00760238" );example handle for window
WinMove($hWnd, "0x00760238", 0, 0, 652, 483 );example handle/windows position co-ords

I hope that's right. Do I still need to use one line in the script for the Opt for title matching or is it not needed?

No, handles are not specified by strings like "0x00760238". You specify the window by title/text (or Class/etc. if using advanced mode). The handle is a 32bit number returned by WinGetHandle() and saved to a variable. If you are working with multiple windows, just save the various handles to separate variables, i.e. $hWin_1 and $hWin_2.

You could also just launch as many instances as you need and let WinList() collect the handles to all the windows into an array and operate on them as required:

Global $sRunCmd = "C:\Program Files\Tun\Emul\emul32.exe CS3-v3.cfg"
Global $sWinTitle = "CS3-v3 - Tun EMUL - [CS3v3]"
Global $avWinList = WinList($sWinTitle)
Global $iWinCount = $avWinList[0][0]; Current number of emul32.exe windows

; Open 5 instances
While $iWinCount < 5
    Run($sRunCmd)
    Do
        Sleep(100)
        $avWinList = WinList($sWinTitle)
    Until $avWinList[0][0] = $iWinCount + 1
    $iWinCount = $avWinList[0][0]
WEnd
Sleep(1000)

; Now there are 5 instances running, and their HWNDs are in $avWinList[1][1] thru [5][1]

; Move each window to space nicely on screen
For $n = 1 To 5
    WinMove($avWinList[$n][1], "", $n * 100, $n * 100, 652, 483)
Next
Sleep(1000)

; Login and automate each window
For $n = 1 To 5
    _WinLogin($avWinList[$n][1])
    Sleep(1000)
    
    Switch $n
        Case 1; Window 1
            ControlSend($avWinList[$n][1], "", "", "{up}"); keypress to 'Ran Enquiry'
            ControlSend($avWinList[$n][1], "", "", "{right}");keypress to 'Ran Enquiry'
            ControlSend($avWinList[$n][1], "", "", "{down}");keypress to 'Ran Enquiry'
            ControlSend($avWinList[$n][1], "", "", "{right}");keypress to 'Ran Enquiry'
        Case 2; Window 2
            ControlSend($avWinList[$n][1], "", "", "{down}");keypress to 'Receipts'
            ControlSend($avWinList[$n][1], "", "", "{right}");keypress to 'Receipts'
            ControlSend($avWinList[$n][1], "", "", "{down 2}");keypress to 'Receipts'
            ControlSend($avWinList[$n][1], "", "", "{right 2}");keypress to 'Receipts'
            ControlSend($avWinList[$n][1], "", "", "{up}");keypress to 'Receipts'
            ControlSend($avWinList[$n][1], "", "", "{right}");keypress to 'Receipts'
        Case 3; Window 3
        ; etc.
        Case 4; Window 4
        ; etc.
        Case 5; Window 5
    EndSwitch
    Sleep(1000)
Next

Func _WinLogin($hWin)
    WinActivate($hWin)
    WinWaitActive($hWin)
    ControlSend($hWin, "", "", "xxx"); username
    Sleep(1000)
    ControlSend($hWin, "", "", "{Enter}")
    Sleep(1000)
    ControlSend($hWin, "", "", "xxx");password
    Sleep(1000)
    ControlSend($hWin, "", "", "{Enter}")
EndFunc  ;==>_WinLogin

:(


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#11 ·  Posted

Hi.

Here's an example of how to get a window handle from a pid

opt("WinTitleMatchMode",4)

$PID = Run("Notepad")
Sleep(2000)

$hWnd = _GetHandleFromPid($PID)

WinMove($hWnd,"",1,1)

Func _GetHandleFromPid($PID)
    Local $aWinList
    $aWinList = WinList()
    
    For $x = 1 to $aWinList[0][0]
        If WinGetProcess($aWinList[$x][0]) = $PID Then Return $aWinList[$x][1]
    Next
        
    Return -1
EndFunc
Nice Snippet, thanks! :(

I'm just wondering, if *ONE* PID might have two GUI windows? :) I don't even know howto search for that, as my knowledge on how processes and GUI windows act is, well, pretty poor ..

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

#12 ·  Posted

I'm just wondering, if *ONE* PID might have two GUI windows?

Yes, one process can have multiple GUIs, and each GUI will have its own unique handle. As soon as you run an AutoIt script with InputBox() or MsgBox(), there are at least two GUIs for that process.

Demo:

#include <Guiconstants.au3>
#include <Array.au3>

Opt("GuiOnEventMode", 1)

Global $avGUIs[5]
For $n = 0 To 4
    $avGUIs[$n] = GUICreate("GUI " & $n, 300, 300, 100 + (100 * $n), 100 + (50 * $n))
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit", $avGUIs[$n])
    GUISetState(@SW_SHOW, $avGUIs[$n])
Next

Global $avWinList = WinList("GUI")
ReDim $avWinList[UBound($avWinList)][3]
For $n = 1 to $avWinList[0][0]
    $avWinList[$n][2] = WinGetProcess($avWinList[$n][1]); Get PID by window handle
Next
_ArrayDisplay($avWinList, "$avWinList")

While 1
    Sleep(20)
WEnd

Func _Quit()
    Exit
EndFunc  ;==>_Quit

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

No, handles are not specified by strings like "0x00760238". You specify the window by title/text (or Class/etc. if using advanced mode). The handle is a 32bit number returned by WinGetHandle() and saved to a variable. If you are working with multiple windows, just save the various handles to separate variables, i.e. $hWin_1 and $hWin_2.

You could also just launch as many instances as you need and let WinList() collect the handles to all the windows into an array and operate on them as required:

Global $sRunCmd = "C:\Program Files\Tun\Emul\emul32.exe CS3-v3.cfg"
Global $sWinTitle = "CS3-v3 - Tun EMUL - [CS3v3]"
Global $avWinList = WinList($sWinTitle)
Global $iWinCount = $avWinList[0][0]; Current number of emul32.exe windows

; Open 5 instances
While $iWinCount < 5
    Run($sRunCmd)
    Do
        Sleep(100)
        $avWinList = WinList($sWinTitle)
    Until $avWinList[0][0] = $iWinCount + 1
    $iWinCount = $avWinList[0][0]
WEnd
Sleep(1000)

; Now there are 5 instances running, and their HWNDs are in $avWinList[1][1] thru [5][1]

; Move each window to space nicely on screen
For $n = 1 To 5
    WinMove($avWinList[$n][1], "", $n * 100, $n * 100, 652, 483)
Next
Sleep(1000)

; Login and automate each window
For $n = 1 To 5
    _WinLogin($avWinList[$n][1])
    Sleep(1000)
    
    Switch $n
        Case 1; Window 1
            ControlSend($avWinList[$n][1], "", "", "{up}"); keypress to 'Ran Enquiry'
            ControlSend($avWinList[$n][1], "", "", "{right}");keypress to 'Ran Enquiry'
            ControlSend($avWinList[$n][1], "", "", "{down}");keypress to 'Ran Enquiry'
            ControlSend($avWinList[$n][1], "", "", "{right}");keypress to 'Ran Enquiry'
        Case 2; Window 2
            ControlSend($avWinList[$n][1], "", "", "{down}");keypress to 'Receipts'
            ControlSend($avWinList[$n][1], "", "", "{right}");keypress to 'Receipts'
            ControlSend($avWinList[$n][1], "", "", "{down 2}");keypress to 'Receipts'
            ControlSend($avWinList[$n][1], "", "", "{right 2}");keypress to 'Receipts'
            ControlSend($avWinList[$n][1], "", "", "{up}");keypress to 'Receipts'
            ControlSend($avWinList[$n][1], "", "", "{right}");keypress to 'Receipts'
        Case 3; Window 3
; etc.
        Case 4; Window 4
; etc.
        Case 5; Window 5
    EndSwitch
    Sleep(1000)
Next

Func _WinLogin($hWin)
    WinActivate($hWin)
    WinWaitActive($hWin)
    ControlSend($hWin, "", "", "xxx"); username
    Sleep(1000)
    ControlSend($hWin, "", "", "{Enter}")
    Sleep(1000)
    ControlSend($hWin, "", "", "xxx");password
    Sleep(1000)
    ControlSend($hWin, "", "", "{Enter}")
EndFunc;==>_WinLogin

:(

Thanks, I now have the script working courtesy of Covaks post. :)

I discovered the use of using the title rather than the actual handle when I got to work this morning. It did take me two attempts before realising. :D

The script you have written looks well over my head for now, but of course I'll be using it and educating myself further over the following weeks to help me understand more efficiently. Thanks for taking the time to read my problem and post a great solution, its greatly appreciated.

Thanks to all who have contributed.

Edited by rayzer

Share this post


Link to post
Share on other sites

@ PsaltyDS.

May I have some advice on the example you made about arrays please?

I've tried various things for a few hours but have no idea why the first two windows open fine in the correct positions but the last three don't? Is it because three of them are identical so therefore it only executes the first?

Global $sRunCmd = "notepad.exe"
Global $sWinTitle = "Untitled - Notepad"
Global $avWinList = WinList($sWinTitle)
Global $iWinCount = $avWinList[0][0]



While $iWinCount < 5
    Run($sRunCmd)
    Do
        Sleep(100)
        $avWinList = WinList($sWinTitle)
    Until $avWinList[0][0] = $iWinCount + 1
    $iWinCount = $avWinList[0][0]
WEnd
Sleep(1000)


For $n = 1 To 5 
    
    
    Switch $n
        Case 1
            WinMove($avWinList[$n][1], "", $n * 0, $n * 0, 652, 483)
        Case 2
            WinMove($avWinList[$n][1], "", $n * 186, $n * 0, 652, 483)
        Case 3
            WinMove($avWinList[$n][1], "", $n * 186, $n * 88, 652, 483)
        Case 4  
            WinMove($avWinList[$n][1], "", $n * 186, $n * 88, 652, 483)
        Case 5  
            WinMove($avWinList[$n][1], "", $n * 186, $n * 88, 652, 483)
    EndSwitch
    Sleep(1000)  

Next

Help greatly appreciated.

:)

Share this post


Link to post
Share on other sites

@ PsaltyDS.

May I have some advice on the example you made about arrays please?

I've tried various things for a few hours but have no idea why the first two windows open fine in the correct positions but the last three don't? Is it because three of them are identical so therefore it only executes the first?

Global $sRunCmd = "notepad.exe"
Global $sWinTitle = "Untitled - Notepad"
Global $avWinList = WinList($sWinTitle)
Global $iWinCount = $avWinList[0][0]

While $iWinCount < 5
    Run($sRunCmd)
    Do
        Sleep(100)
        $avWinList = WinList($sWinTitle)
    Until $avWinList[0][0] = $iWinCount + 1
    $iWinCount = $avWinList[0][0]
WEnd
Sleep(1000)

For $n = 1 To 5 
    Switch $n
        Case 1
            WinMove($avWinList[$n][1], "", $n * 0, $n * 0, 652, 483)
        Case 2
            WinMove($avWinList[$n][1], "", $n * 186, $n * 0, 652, 483)
        Case 3
            WinMove($avWinList[$n][1], "", $n * 186, $n * 88, 652, 483)
        Case 4  
            WinMove($avWinList[$n][1], "", $n * 186, $n * 88, 652, 483)
        Case 5  
            WinMove($avWinList[$n][1], "", $n * 186, $n * 88, 652, 483)
    EndSwitch
    Sleep(1000)  
Next

Help greatly appreciated.

:)

Your code is a little bit awkward. Just use the array reference to $avWinList[0][0] for the count; no need to read it out to another variable. You are already in a For/Next loop using $n, so the Switch/Case is not needed.

I still get at least 5 instances of notepad, and sometimes more, all at different locations, so it almost works. The problem seems to be that Sleep(100) is not long enough to wait for the new process to create its GUI. Changing that to Sleep(250) got me exactly 5 instances every time.

You might try this version, cleaned up a little bit:

Global $sRunCmd = "notepad.exe"
Global $sWinTitle = "Untitled - Notepad"
Global $avWinList = WinList($sWinTitle)

; Create 5 instances of notepad
While $avWinList[0][0] < 5
    Run($sRunCmd)
    Sleep(250); Wait for new process to create its GUI
    $avWinList = WinList($sWinTitle)
WEnd

; Move and ID each instance
For $n = 1 To $avWinList[0][0]
    WinActivate($avWinList[$n][1])
    WinMove($avWinList[$n][1], "", ($n - 1) * 100, ($n - 1) * 100, 640, 480)
    ControlSetText($avWinList[$n][1], "", "Edit1", "This is notepad.exe instance no." & $n)
    Sleep(1000)  
Next
Sleep(1000)

; Close each instance
For $n = 1 To $avWinList[0][0]
    WinActivate($avWinList[$n][1])
    WinClose($avWinList[$n][1])
    Sleep(1000)
Next

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

Thanks PsaltyDS.

This is working much better than my original but I'm still a little confused where you state 'move and ID each instance.

The script opens 5 windows but not at the co-ords I would like to use:

Global $sRunCmd = "notepad.exe"
Global $sWinTitle = "Untitled - Notepad"
Global $avWinList = WinList($sWinTitle)

; Create 5 instances of notepad
While $avWinList[0][0] < 5
    Run($sRunCmd)
    Sleep(250); Wait for new process to create its GUI
    $avWinList = WinList($sWinTitle)
WEnd

; Move and ID each instance
For $n = 1 To $avWinList[0][0]
    WinActivate($avWinList[$n][1])
    WinMove($avWinList[$n][1], "", ($n - 1) * 100, ($n - 1) * 100, 640, 480)
    ControlSetText($avWinList[$n][1], "", "Edit1", "This is notepad.exe instance no." & $n)
    Sleep(1000)  
Next
Sleep(1000)

co-ords required (for example) :

WinMove($avWinList[$n][1], "", $n * 0, $n * 0, 652, 483)

WinMove($avWinList[$n][1], "", $n * 186, $n * 0, 652, 483)
  
WinMove($avWinList[$n][1], "", $n * 186, $n * 88, 652, 483)
  
WinMove($avWinList[$n][1], "", $n * 186, $n * 88, 652, 483)
  
WinMove($avWinList[$n][1], "", $n * 186, $n * 88, 652, 483)

I would like to move each instance at a position I choose.

Thanks.

I hope you had a really good weekend too. :)

Edited by rayzer

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

The script opens 5 windows but not at the co-ords I would like to use:

I would like to move each instance at a position I choose.

I don't see the problem.

Just change the math:

; Move and ID each instance
For $n = 1 To $avWinList[0][0]
    WinActivate($avWinList[$n][1])
    WinMove($avWinList[$n][1], "", ($n - 1) * 186, ($n - 1) * 88, 652, 483)
    ControlSetText($avWinList[$n][1], "", "Edit1", "This is notepad.exe instance no." & $n)
    Sleep(1000)  
Next

Or, if there is no convenient math for setting x and y, store the values in another array and read them out of there:

; Move and ID each instance
Global $avWinPos[UBound($avWinList)][2] = [[$avWinList[0][0], ""],[0,0],[186,0]]
For $n = 3 To $avWinList[0][0]
    $avWinPos[$n][0] = $n * 186
    $avWinPos[$n][1] = $n * 88
Next

For $n = 1 To $avWinList[0][0]
    WinActivate($avWinList[$n][1])
    WinMove($avWinList[$n][1], "", $avWinPos[$n][0], $avWinPos[$n][1], 652, 483)
    ControlSetText($avWinList[$n][1], "", "Edit1", "This is notepad.exe instance no." & $n)
    Sleep(1000)  
Next

:)

Edit: Tweaked values in $avWinPos

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Sorry, I changed the math but only the first window would move to the correct co-ords, the next four would open equally spaced ignoring the next co-ord given. I am a total noob with autoit and I'm very thankful for your help.

I will try the above shortly. :)

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

Thanks, PsaltyDS. Full credit for the help you have given me, if there was a way in terms of karma, I would. :)

It took me until now to work out the math for my windows positioning but as I said, I'm a complete novice trying to understand the advanced workings of autoit. The below is correct now, but I only stumbled across the math by accident (trial and error). Have I made it hard for myself or was that the correct way (is there a set formula).

Global $sRunCmd = "notepad.exe"
Global $sWinTitle = "Untitled - Notepad"
Global $avWinList = WinList($sWinTitle)

; Create 5 instances of notepad
While $avWinList[0][0] < 5
    Run($sRunCmd)
    Sleep(250); Wait for new process to create its GUI
    $avWinList = WinList($sWinTitle)
WEnd

; Move and ID each instance

For $n = 1 To $avWinList[0][0]
    WinActivate($avWinList[$n][1])
    WinMove($avWinList[$n][1], "", ($n - 1) * 0, ($n - 1) * 0, 652, 483)
    ControlSetText($avWinList[$n][1], "", "Edit1", "This is notepad.exe instance no." & $n)
Next

For $n = 2 To $avWinList[0][0]
    WinActivate($avWinList[$n][1])
    WinMove($avWinList[$n][1], "", ($n - 1) * 372, ($n - 1) * 166, 652, 483)
    ControlSetText($avWinList[$n][1], "", "Edit1", "This is notepad.exe instance no." & $n)
Next

For $n = 3 To $avWinList[0][0]
    WinActivate($avWinList[$n][1])
    WinMove($avWinList[$n][1], "", ($n - 1) * 186, ($n - 1) * 0, 652, 483)
    ControlSetText($avWinList[$n][1], "", "Edit1", "This is notepad.exe instance no." & $n)
Next

For $n = 4 To $avWinList[0][0]
    WinActivate($avWinList[$n][1])
    WinMove($avWinList[$n][1], "", ($n - 1) * 124, ($n - 1) * 55.3, 652, 483)
    ControlSetText($avWinList[$n][1], "", "Edit1", "This is notepad.exe instance no." & $n)
Next

For $n = 5 To $avWinList[0][0]
    WinActivate($avWinList[$n][1])
    WinMove($avWinList[$n][1], "", ($n - 1) * 93, ($n - 1) * 41.5, 652, 483)
    ControlSetText($avWinList[$n][1], "", "Edit1", "This is notepad.exe instance no." & $n)
Next
Edited by rayzer

Share this post


Link to post
Share on other sites

Thanks, PsaltyDS. Full credit for the help you have given me, if there was a way in terms of karma, I would. :)

It took me until now to work out the math for my windows positioning but as I said, I'm a complete novice trying to understand the advanced workings of autoit. The below is correct now, but I only stumbled across the math by accident (trial and error). Have I made it hard for myself or was that the correct way (is there a set formula).

Global $sRunCmd = "notepad.exe"
Global $sWinTitle = "Untitled - Notepad"
Global $avWinList = WinList($sWinTitle)

; Create 5 instances of notepad
While $avWinList[0][0] < 5
    Run($sRunCmd)
    Sleep(250); Wait for new process to create its GUI
    $avWinList = WinList($sWinTitle)
WEnd

; Move and ID each instance

For $n = 1 To $avWinList[0][0]
    WinActivate($avWinList[$n][1])
    WinMove($avWinList[$n][1], "", ($n - 1) * 0, ($n - 1) * 0, 652, 483)
    ControlSetText($avWinList[$n][1], "", "Edit1", "This is notepad.exe instance no." & $n)
Next

For $n = 2 To $avWinList[0][0]
    WinActivate($avWinList[$n][1])
    WinMove($avWinList[$n][1], "", ($n - 1) * 372, ($n - 1) * 166, 652, 483)
    ControlSetText($avWinList[$n][1], "", "Edit1", "This is notepad.exe instance no." & $n)
Next

For $n = 3 To $avWinList[0][0]
    WinActivate($avWinList[$n][1])
    WinMove($avWinList[$n][1], "", ($n - 1) * 186, ($n - 1) * 0, 652, 483)
    ControlSetText($avWinList[$n][1], "", "Edit1", "This is notepad.exe instance no." & $n)
Next

For $n = 4 To $avWinList[0][0]
    WinActivate($avWinList[$n][1])
    WinMove($avWinList[$n][1], "", ($n - 1) * 124, ($n - 1) * 55.3, 652, 483)
    ControlSetText($avWinList[$n][1], "", "Edit1", "This is notepad.exe instance no." & $n)
Next

For $n = 5 To $avWinList[0][0]
    WinActivate($avWinList[$n][1])
    WinMove($avWinList[$n][1], "", ($n - 1) * 93, ($n - 1) * 41.5, 652, 483)
    ControlSetText($avWinList[$n][1], "", "Edit1", "This is notepad.exe instance no." & $n)
Next
You are re-moving the windows over and over again. I don't know if that's what you mean to do or not. Doesn't seem necessary. Also, fractional pixel coordinates just get rounded off, so 55.3 and 41.5 may not be that helpful.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
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
Sign in to follow this  
Followers 0