Loop through YouTube windows

Get help with using AutoHotkey and its commands and hotkeys
scriptor2016
Posts: 655
Joined: 21 Dec 2015, 02:34

Loop through YouTube windows

08 May 2020, 23:50

Hey all :)

I really can't figure out how to do this:

Let's say I have 15 instances of Google Chrome open at once (not good, I know).

And that 5 of them are on YouTube, each playing any random video.

When I press the hotkey, I want it to activate the first instance of a YouTube window it finds, so that the window comes to the front.

And when I press the hotkey again, it will activate the next instance of YouTube it finds and then bring that one to the front.

And so on. Once it hits the 5th instance, pressing the hotkey will loop back to the first instance again.

I think I have this right but how do I pause the loop once the first instance is found, and then wait until the key is pressed again to resume the loop?


Code: Select all

SetTitleMatchMode, 2
z::
  
WinGet, windowtile, List, YouTube

Loop, %windowtile% 
{ 
WinGetTitle, Title, % "ahk_id " windowtile%A_Index%
winactivate, %title%
; I need it to stop here, and wait for the next time I press the hotkey in order to search for the next instance
}
Return
GEV
Posts: 844
Joined: 25 Feb 2014, 00:50

Re: Loop through YouTube windows

09 May 2020, 00:19

GroupActivate is what you need in this case.
scriptor2016
Posts: 655
Joined: 21 Dec 2015, 02:34

Re: Loop through YouTube windows

09 May 2020, 00:35

Thanks GEV

okay so I adjusted the code to the following, and it looks like this works - but with the same problem: it activates all YouTube windows, one after the other until the loop is complete.

I'm hoping to have it activate just the first one with when the hotkey is pressed, then wait for the next press of the hotkey to activate the second instance, etc

Code: Select all

SetTitleMatchMode, 2
z::
  
WinGet, windowtile, List, YouTube

Loop, %windowtile% 
{ 
GroupAdd, Group, YouTube
WinGetTitle, Title, % "ahk_id " windowtile%A_Index% ;this is probably redundant now
GroupActivate, %Group%
 ;how do we get the loop to stop here and wait for the next hotkey press in order to resume the loop
}
Return
GEV
Posts: 844
Joined: 25 Feb 2014, 00:50

Re: Loop through YouTube windows

09 May 2020, 00:54

Code: Select all

SetTitleMatchMode, 2
GroupAdd, YouTube_Chrome, YouTube ahk_exe chrome.exe

		RETURN	 ; === end of auto-execute section ===

$z:: GroupActivate, YouTube_Chrome, R
scriptor2016
Posts: 655
Joined: 21 Dec 2015, 02:34

Re: Loop through YouTube windows

09 May 2020, 01:08

Trying a different approach here. This now works a little better, but it's getting stuck in between just two instances, and activates between them back and forth each time the hotkey is pressed, even if there's 3 or 4 other YouTube windows:

Code: Select all

flag=
SetTitleMatchMode, 2
z::
  
WinGet, windowtile, List, YouTube

Loop, %windowtile% 
{ 
WinGetTitle, Title, % "ahk_id " windowtile%A_Index%
       
         if (flag = Title)
         {
         continue
         }
         else
         {
         winactivate, %title%
         flag := title
         break
         }
}

Return

I can't figure out why it's getting stuck in between two of the instances upon each hotkey press (it's like an endless toggle between them both). It won't progress onto the remaining instances...
scriptor2016
Posts: 655
Joined: 21 Dec 2015, 02:34

Re: Loop through YouTube windows

09 May 2020, 01:11

wow- thanks GEV.. this can be done without a loop ?

Guess I'll have to read up on this, as I want to do this with other WinTitles as well...
scriptor2016
Posts: 655
Joined: 21 Dec 2015, 02:34

Re: Loop through YouTube windows

09 May 2020, 01:40

can I ask one more question here:

when using this code in a GUI with a button, why is it getting stuck on the first YouTube instance and not continuing to cycle through the rest of the YouTube windows? The hotkey method cycles through all instances, but this only works with the first press of the GUI button and then becomes unrespsonsive:

Code: Select all

SetTitleMatchMode, 2
GroupAdd, YouTube_Chrome, YouTube ahk_exe chrome.exe

Gui, Add , Button, x13  y13 w80 h30 vButton gClick, BUTTON
Gui, Show, x500 y500 w110 h50, TEST
Return

Click:
GroupActivate, YouTube_Chrome, R
Return

GuiEscape:
ExitApp
Return
scriptor2016
Posts: 655
Joined: 21 Dec 2015, 02:34

Re: Loop through YouTube windows

09 May 2020, 01:41

whoops never mind... I got it... just remove the ,R

thanks again GEV this works nicely now :)

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], masato, moowee, songdg and 50 guests