Jump to content

Random(), WinGetHandle(), Variables, & Arrays


Recommended Posts

Goal:

Have a script that will start an application like calc.exe X number of times and capture the handle to a variable that will be used later in the script. The trouble I'm having is knowing the best way to have the script assign each handle to a variable that will later be called upon based upon the Random number later called by the script. If that makes sense.

For example if I start 10 calc.exe programs each will have their own unique handle. I want the script to assign each handle to a variable that I can then use in conjunction with Random(1,9,1) and each value 0-9 will WinActivate the corresponding calc.exe. I also want the script to keep track which variables have not yet been called so that each of the 10 calc.exe will only be called upon once.

I'd like the script to be dynamic in that the X number of calc.exe can be set through user input. For this reason I'm not sure if there will need to be 1 variable or 100 variables created that will hold the handles of the calc.exe. I'm not sure if the best method is to capture the handle into an array using _ArrayAdd($avArray,WinGetHandle("classname=SciCalc","")) or another method. Once the handles are in the array, how can I then have the script read the array and assign each element to a variable for later use in the script?

This is what I have working so far, but really don't know what direction to venture into to accomplish the end goal. Thanks in advance

Opt("WinTitleMatchMode",4)

Dim $myCal
$count = 0

While $count <= 9
    Run("calc.exe")
    WinWaitActive("Classname=SciCalc","")
    WinGetHandle("classname=SciCalc", "")
    MsgBox(0,"status", "Count = "&$count &" and handle = "& WinGetHandle("classname=SciCalc",""))
    $count = $count + 1
WEnd

$handle_Cal1 = WinGetHandle("classname=SciCalc", "")  ;; .....
$handle_Cal2 = WinGetHandle("classname=SciCalc", "")  ;;     .......
$handle_Cal3 = WinGetHandle("classname=SciCalc", "")  ;;     .............  variables to be automatically set if possible
$handle_Cal4 = WinGetHandle("classname=SciCalc", "")  ;; .....

While 1
    Sleep(500)
    $myCal = Random(0, 9, 1)
    
    Switch $myCal
        Case 1
            WinActivate($handle_Cal1)
        Case 2
            WinActivate($handle_Cal2)
        Case 3
            WinActivate($handle_Cal3)
        Case 4
            WinActivate($handle_Cal4)
    EndSwitch
WEnd
Link to comment
Share on other sites

You want the handles in an array, and WinList() will do that for you:

#include <array.au3>
Opt("WinTitleMatchMode", 4)

HotKeySet("{ESC}", "_Quit")

For $n = 1 To 9
    Run("calc.exe")
    Sleep(1000)
Next
Global $myCal = WinList("[CLASS:SciCalc]")

While 1
    $n = Random(1, $myCal[0][0], 1)
    WinActivate($myCal[$n][1])
    WinWaitActive($myCal[$n][1])
    Sleep(2000)
WEnd

Func _Quit()
    For $n = 1 To $myCal[0][0]
        WinClose($myCal[$n][1])
        WinWaitClose($myCal[$n][1])
    Next
    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
Link to comment
Share on other sites

You want the handles in an array, and WinList() will do that for you: ....

Thanks PsaltyDS, you you're good you! Like some many of the vets here you're dead on. I noticed the help on WinList says WinGetHandle is related, but the help on WinGetHandle doesn't point back to WinList. Maybe in the next release that could be added?

Did you have any suggestions on how I could make the script only WinActivate each handle once before the script ends? I tried adding _ArrayDelete to the loop, but that doesn't seem to work without killing the entire array then crashing the script.

While 1
    $n = Random(1, $myCal[0][0], 1)
    WinActivate($myCal[$n][1])
    WinWaitActive($myCal[$n][1])
    _ArrayDelete( $myCal,$myCal[$n][1])
    _ArrayDisplay( $myCal, "Remaining Array" )
    Sleep(2000)
WEnd

My apologies if I'm not describing this very well. Maybe this will help? After WinList places 9 handles into the $myCal[2][10] array, I would like the script to:

a. WinActivate a random element from $myCal --- this occurs now

b. remove the element just activated so that Random now has 8 instead of 9 elements to choose from

c. the While ... WEnd will exit once each of the 9 elements have been randomly activated.

Hopefully this paints a better picture of what I'm trying to accomplish. And again a sincere thank you PsaltyDS for helping me to see the light.

--ss3

...

Link to comment
Share on other sites

This way creates a seperate array just for the WinActivate's, and deletes each one as it's done. The original list in $avWinList is preserved for closing the windows when you hit ESC:

#include <array.au3>
Opt("WinTitleMatchMode", 4)

HotKeySet("{ESC}", "_Quit")

For $n = 1 To 9
    Run("calc.exe")
    Sleep(1000)
Next

Global $avWinList = WinList("[CLASS:SciCalc]")

Global $myCal[UBound($avWinList)]
For $n = 1 To $avWinList[0][0]
    $myCal[$n] = $avWinList[$n][1]
Next

Do
    $n = Random(1, UBound($myCal) - 1, 1)
    WinActivate($myCal[$n])
    WinWaitActive($myCal[$n])
    _ArrayDelete($myCal, $n)
    Sleep(2000)
Until UBound($myCal) = 1

While 1
    Sleep(20)
WEnd

Func _Quit()
    For $n = 1 To $avWinList[0][0]
        WinClose($avWinList[$n][1])
        WinWaitClose($avWinList[$n][1])
    Next
    Exit
EndFunc   ;==>_Quit

What was the point of this exercise?

:)

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
Link to comment
Share on other sites

This way creates a seperate array just for the WinActivate's, and deletes each one as it's done. The original list in $avWinList is preserved for closing the windows when you hit ESC:

What was the point of this exercise?

:)

Thanks again PsaltyDS, now its doing what I need. The point of the exercise? There's a computer test that flashes a series of letters in random order for a specifiy duration (randomly set) then pauses for a set of time (also randomly set) before moving to the next set of letters (randomly set). Since I don't have a background in programming, I thought I'd try to emulate what the computer test does while learning more about autoit and programming in general.

In summary the test does the following:

- Takes the 26 letters (A - Z) and randomly picks X number for the first set; say X = 10

- Randomly pick 10 letters from A - Z and assigns them to set1; set1 = A, J, H, M, L, Z, P, S, A, B

- creates set2 based off a random number Y and set3 off of number Z; Y = 12 and Z = 8; set2 = X, U, T, E, W, P, V, X, O, N, Z, A

- now that set1 has 10 random letters, set2 has 12 random letters, and set3 has 8 random letters the program

will display each set at a random interval.

- set1 will be displayed one letter at a time for A random duration (300ms - 5000ms) until entire set has been displayed; A = 800ms

- a random interval pause will then be executed anywere from 500ms to 5000ms

- set2 will be displayed one letter at a time for B random duration (300ms - 5000ms) .... ; B = 1200ms

blah, blah, blah

Hopefully this makes sense and answers your question. You see, it wasn't merely a pointless exercise as one might have originally thought. Then again everyone sees "pointless" differently. Anyone who's looking for a different project is more than welcome to give this a stab. I'm sure I'll be back asking the forum for help on this.

Thanks again PsaltyDS, I do appreciate your help and direction!

--ss3

...

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...