Is there a way to randomize sequence of WinGet, List? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
kyceblevito
Posts: 3
Joined: 31 Aug 2020, 21:02

Is there a way to randomize sequence of WinGet, List?

Post by kyceblevito » 02 Sep 2020, 01:32

I have a set of code that works perfectly, however I would like to randomize certain things.

I have a WinGet that groups Chrome windows, in which I use to sequence them in my code below.
Ex: ahk_id %cw1%, ahk_id %cw2%, ahk_id %cw3%, ahk_id %cw4%.

Is there a way to randomize which ahk_id %% comes first? Right now I have written it in a way where it goes from 1,2,3 and 4. I would like this sequence to change everytime. I have tried making an array and looping the array with random, but not sure how to applied it within the context of WinActivate, and ControlSend to run the script.

Code: Select all

#MaxThreadsPerHotkey 2
toggle = 0
Random, ranSleep, 100, 400
CoordMode, Pixel , Screen
return


F1::
toggle := !toggle
WinGet, cw, List, example - Google Chrome

if(toggle)
{
	SoundBeep, 350, 200
    Loop
    {
        ImageSearch, x, y, 0, 0, 1020, 940, ex1.png
		if (!toggle)
		{
			SoundBeep, 850, 200
			break
		}
		
        if (ErrorLevel = 0)
        {
            Sleep, %ranSleep%
			WinActivate,  ahk_id %cw1%
            ControlSend,, ex1{Enter}, ahk_id %cw1%
			
			Sleep, %ranSleep%
			WinActivate,  ahk_id %cw2%
			ControlSend,, ex1{Enter}, ahk_id %cw2%
			
			Sleep, %ranSleep%
			WinActivate,  ahk_id %cw3%
			ControlSend,, ex1{Enter}, ahk_id %cw3%
			
			Sleep, %ranSleep%
			WinActivate,  ahk_id %cw4%
			ControlSend,, ex1{Enter}, ahk_id %cw4%
			
            SoundBeep, 750, 50
            Sleep, 5000
        }
		
        ImageSearch, x, y, 0, 0, 1020, 940, ex2.png
        if (ErrorLevel = 0)
        {
            Sleep, %ranSleep%
			WinActivate,  ahk_id %cw1%
            ControlSend,, ex2{Enter}, ahk_id %cw1%
			
			Sleep, %ranSleep%
			WinActivate,  ahk_id %cw2%
			ControlSend,, ex2{Enter},ahk_id %cw2%
			
			Sleep, %ranSleep%
			WinActivate,  ahk_id %cw3%
			ControlSend,, ex2{Enter},ahk_id %cw3%
			
			Sleep, %ranSleep%
			WinActivate,  ahk_id %cw4%
			ControlSend,, ex2{Enter},ahk_id %cw4%

            SoundBeep, 750, 50
            Sleep, 5000
        }
		
        ImageSearch, x, y, 0, 0, 1020, 940, ex3.png
        if (ErrorLevel = 0)
        {
			Sleep, %ranSleep%
			WinActivate,  ahk_id %cw1%
            ControlSend,, ex3{Enter},ahk_id %cw1%
			
			Sleep, %ranSleep%
			WinActivate,  ahk_id %cw2%
			ControlSend,, ex3{Enter},ahk_id %cw2%
			
			Sleep, %ranSleep%
			WinActivate,  ahk_id %cw3%
			ControlSend,, ex3{Enter},ahk_id %cw3%
			
			Sleep, %ranSleep%
			WinActivate,  ahk_id %cw4%
			ControlSend,, ex3{Enter},ahk_id %cw4%
			
			SoundBeep, 750, 50
            Sleep, 5000
        }
    }
}
else
return


User avatar
Xtra
Posts: 1919
Joined: 02 Oct 2015, 12:15

Re: Is there a way to randomize sequence of WinGet, List?  Topic is solved

Post by Xtra » 02 Sep 2020, 02:00

Example:

Code: Select all

#MaxThreadsPerHotkey 2
toggle := 0
Random, ranSleep, 100, 400
CoordMode, Pixel , Screen
return

F1::
toggle := !toggle

winIDs := RandomIDs("example - Google Chrome")

if(toggle)
{
	SoundBeep, 350, 200
    Loop
    {
		if (!toggle) {
			SoundBeep, 850, 200
			break
		} 
        
        ImageSearch, x, y, 0, 0, 1020, 940, ex1.png
        if (ErrorLevel = 0) {
            ControlSendToIDs(winIDs, "ex1{Enter}", ranSleep)
            SoundBeep, 750, 50
            Sleep, 5000
        }
		
        ImageSearch, x, y, 0, 0, 1020, 940, ex2.png
        if (ErrorLevel = 0) {
            ControlSendToIDs(winIDs, "ex2{Enter}", ranSleep)
            SoundBeep, 750, 50
            Sleep, 5000
        }
		
        ImageSearch, x, y, 0, 0, 1020, 940, ex3.png
        if (ErrorLevel = 0) {
            ControlSendToIDs(winIDs, "ex3{Enter}", ranSleep)
			SoundBeep, 750, 50
            Sleep, 5000
        }
    }
}
return

RandomIDs(wintitle) {
    WinGet, cw, List, % wintitle
    Loop % cw
        winIDs .= cw%A_Index% . "|"
    winIDs := RTrim(winIDs, "|")
    Sort, winIDs, Random D|
    return winIDs
}

ControlSendToIDs(winIDs, Keys, ranSleep) {
    Loop, Parse, winIDs, |
    {
        Sleep, ranSleep
        WinActivate,  ahk_id %A_LoopField%
        ControlSend,, %Keys%, ahk_id %A_LoopField%
    }
}
I created some reusable functions to simplify the code.
If you want random ordered IDs every loop iteration putwinIDs := RandomIDs("example - Google Chrome") at the top of the loop.
(same goes for ransleep)

untested HTH

BoBo
Posts: 3764
Joined: 13 May 2014, 17:15

Re: Is there a way to randomize sequence of WinGet, List?

Post by BoBo » 02 Sep 2020, 04:11

Code: Select all

F12::
WinGet, cw, List, example - Google Chrome
Random, no, 1, cw
WinActivate,% "ahk_id " cw%no%
Return
Test Concept. Untested.

kyceblevito
Posts: 3
Joined: 31 Aug 2020, 21:02

Re: Is there a way to randomize sequence of WinGet, List?

Post by kyceblevito » 05 Sep 2020, 09:46

Thank you so much for the replies, I will try them and test it out. :D

kyceblevito
Posts: 3
Joined: 31 Aug 2020, 21:02

Re: Is there a way to randomize sequence of WinGet, List?

Post by kyceblevito » 05 Sep 2020, 10:26

Xtra wrote:
02 Sep 2020, 02:00

Code: Select all

RandomIDs(wintitle) {
    WinGet, cw, List, % wintitle
    Loop % cw
        winIDs .= cw%A_Index% . "|"
    winIDs := RTrim(winIDs, "|")
    Sort, winIDs, Random D|
    return winIDs
}

ControlSendToIDs(winIDs, Keys, ranSleep) {
    Loop, Parse, winIDs, |
    {
        Sleep, ranSleep
        WinActivate,  ahk_id %A_LoopField%
        ControlSend,, %Keys%, ahk_id %A_LoopField%
    }
}
I can quite grasp what is happening here, however I'm a little confused about why parse "|" is used. I'm guessing when ControlSendToIDs is called within if statement, it adds "|" to winIDs and then loop iteration restarts where RandomIDs randomizes the parse statement.

Not exactly sure what is happening in RandomIDs, but I can see that it adds "|" into every %A_Index% and then right after every index is parsed, it removes the "|"? How does these two correlate between the two function and why?

Appologies for the questions, just want to understand :think: :think:

Post Reply

Return to “Ask For Help”