Way to make my script shorter?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Siff2Gi
Posts: 2
Joined: 03 Oct 2022, 04:50

Way to make my script shorter?

Post by Siff2Gi » 03 Oct 2022, 05:00

Hello guys,
i have a very simple to send keystrokes into chrome but i wanted to ask for help to make it shorter but i am an absolute noob to figure out, how..
Basically its repeating

S, G, L and backspace 10 times
Y single time
A, G, L and backspace 10 times
X single time

And another issue i got is, that my script stops at the final end but it should infinite loop until i quit manually.

Any help is appreciated and thanks in advance.
BR

Code: Select all

^q::
#Persistent
SetTimer, noafk, 1000
return

noafk:
	WinGet, winList, List, ahk_class Chrome_WidgetWin_1
   	Loop % winList
   	{   
			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , s,% "ahk_id " winList%a_index%
			ControlSend, , g,% "ahk_id " winList%a_index%
			Sleep, 1200
			ControlSend, , l,% "ahk_id " winList%a_index%
			Sleep, 500
			ControlSend, , {Backspace},% "ahk_id " winList%a_index%
			Sleep, 600


			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , s,% "ahk_id " winList%a_index%
			ControlSend, , g,% "ahk_id " winList%a_index%
			Sleep, 1200
			ControlSend, , l,% "ahk_id " winList%a_index%
			Sleep, 500
			ControlSend, , {Backspace},% "ahk_id " winList%a_index%
			Sleep, 600

			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , s,% "ahk_id " winList%a_index%
			ControlSend, , g,% "ahk_id " winList%a_index%
			Sleep, 1200
			ControlSend, , l,% "ahk_id " winList%a_index%
			Sleep, 500
			ControlSend, , {Backspace},% "ahk_id " winList%a_index%
			Sleep, 600


			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , s,% "ahk_id " winList%a_index%
			ControlSend, , g,% "ahk_id " winList%a_index%
			Sleep, 1200
			ControlSend, , l,% "ahk_id " winList%a_index%
			Sleep, 500
			ControlSend, , {Backspace},% "ahk_id " winList%a_index%
			Sleep, 600


			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , s,% "ahk_id " winList%a_index%
			ControlSend, , g,% "ahk_id " winList%a_index%
			Sleep, 1200
			ControlSend, , l,% "ahk_id " winList%a_index%
			Sleep, 500
			ControlSend, , {Backspace},% "ahk_id " winList%a_index%
			Sleep, 600


			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , s,% "ahk_id " winList%a_index%
			ControlSend, , g,% "ahk_id " winList%a_index%
			Sleep, 1200
			ControlSend, , l,% "ahk_id " winList%a_index%
			Sleep, 500
			ControlSend, , {Backspace},% "ahk_id " winList%a_index%
			Sleep, 600

			
			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , s,% "ahk_id " winList%a_index%
			ControlSend, , g,% "ahk_id " winList%a_index%
			Sleep, 1200
			ControlSend, , l,% "ahk_id " winList%a_index%
			Sleep, 500
			ControlSend, , {Backspace},% "ahk_id " winList%a_index%
			Sleep, 600


			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , s,% "ahk_id " winList%a_index%
			ControlSend, , g,% "ahk_id " winList%a_index%
			Sleep, 1200
			ControlSend, , l,% "ahk_id " winList%a_index%
			Sleep, 500
			ControlSend, , {Backspace},% "ahk_id " winList%a_index%
			Sleep, 600

			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , s,% "ahk_id " winList%a_index%
			ControlSend, , g,% "ahk_id " winList%a_index%
			Sleep, 1200
			ControlSend, , l,% "ahk_id " winList%a_index%
			Sleep, 500
			ControlSend, , {Backspace},% "ahk_id " winList%a_index%
			Sleep, 600


			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , s,% "ahk_id " winList%a_index%
			ControlSend, , g,% "ahk_id " winList%a_index%
			Sleep, 1200
			ControlSend, , l,% "ahk_id " winList%a_index%
			Sleep, 500
			ControlSend, , {Backspace},% "ahk_id " winList%a_index%
			Sleep, 600

			




			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , y,% "ahk_id " winList%a_index%
			Sleep, 450





			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , a,% "ahk_id " winList%a_index%
			ControlSend, , g,% "ahk_id " winList%a_index%
			Sleep, 1200
			ControlSend, , l,% "ahk_id " winList%a_index%
			Sleep, 500
			ControlSend, , {Backspace},% "ahk_id " winList%a_index%
			Sleep, 600


			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , a,% "ahk_id " winList%a_index%
			ControlSend, , g,% "ahk_id " winList%a_index%
			Sleep, 1200
			ControlSend, , l,% "ahk_id " winList%a_index%
			Sleep, 500
			ControlSend, , {Backspace},% "ahk_id " winList%a_index%
			Sleep, 600


			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , a,% "ahk_id " winList%a_index%
			ControlSend, , g,% "ahk_id " winList%a_index%
			Sleep, 1200
			ControlSend, , l,% "ahk_id " winList%a_index%
			Sleep, 500
			ControlSend, , {Backspace},% "ahk_id " winList%a_index%
			Sleep, 600



			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , a,% "ahk_id " winList%a_index%
			ControlSend, , g,% "ahk_id " winList%a_index%
			Sleep, 1200
			ControlSend, , l,% "ahk_id " winList%a_index%
			Sleep, 500
			ControlSend, , {Backspace},% "ahk_id " winList%a_index%
			Sleep, 600



			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , a,% "ahk_id " winList%a_index%
			ControlSend, , g,% "ahk_id " winList%a_index%
			Sleep, 1200
			ControlSend, , l,% "ahk_id " winList%a_index%
			Sleep, 500
			ControlSend, , {Backspace},% "ahk_id " winList%a_index%
			Sleep, 600



			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , a,% "ahk_id " winList%a_index%
			ControlSend, , g,% "ahk_id " winList%a_index%
			Sleep, 1200
			ControlSend, , l,% "ahk_id " winList%a_index%
			Sleep, 500
			ControlSend, , {Backspace},% "ahk_id " winList%a_index%
			Sleep, 600


			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , a,% "ahk_id " winList%a_index%
			ControlSend, , g,% "ahk_id " winList%a_index%
			Sleep, 1200
			ControlSend, , l,% "ahk_id " winList%a_index%
			Sleep, 500
			ControlSend, , {Backspace},% "ahk_id " winList%a_index%
			Sleep, 600



			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , a,% "ahk_id " winList%a_index%
			ControlSend, , g,% "ahk_id " winList%a_index%
			Sleep, 1200
			ControlSend, , l,% "ahk_id " winList%a_index%
			Sleep, 500
			ControlSend, , {Backspace},% "ahk_id " winList%a_index%
			Sleep, 600



			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , a,% "ahk_id " winList%a_index%
			ControlSend, , g,% "ahk_id " winList%a_index%
			Sleep, 1200
			ControlSend, , l,% "ahk_id " winList%a_index%
			Sleep, 500
			ControlSend, , {Backspace},% "ahk_id " winList%a_index%
			Sleep, 600


			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , a,% "ahk_id " winList%a_index%
			ControlSend, , g,% "ahk_id " winList%a_index%
			Sleep, 1200
			ControlSend, , l,% "ahk_id " winList%a_index%
			Sleep, 500
			ControlSend, , {Backspace},% "ahk_id " winList%a_index%
			Sleep, 600



			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , x,% "ahk_id " winList%a_index%
			Sleep, 450		

		
   	}
return
Escape::
ExitApp
Return

User avatar
boiler
Posts: 16771
Joined: 21 Dec 2014, 02:44

Re: Way to make my script shorter?

Post by boiler » 03 Oct 2022, 05:32

The clear way to make your script shorter is to use loops instead of repeating the same code 10 times followed by another piece of code, then repeating all of that again. You apparently know what a loop is. Why not employ it for this?

Code: Select all

^q::
#Persistent
SetTimer, noafk, 1000
return

noafk:
	WinGet, winList, List, ahk_class Chrome_WidgetWin_1
   	Loop % winList
   	{ 
		loop, 2
		{
			loop, 10
			{
				ControlFocus, , % "ahk_id " winList%a_index%	
				Sleep, 450
				ControlSend, , s,% "ahk_id " winList%a_index%
				ControlSend, , g,% "ahk_id " winList%a_index%
				Sleep, 1200
				ControlSend, , l,% "ahk_id " winList%a_index%
				Sleep, 500
				ControlSend, , {Backspace},% "ahk_id " winList%a_index%
				Sleep, 600
			}

			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , y,% "ahk_id " winList%a_index%
			Sleep, 450
		}
   	}
return
Escape::
ExitApp
Return

Since you have set it to repeat using SetTimer, I don’t see why it would not continue executing.

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Way to make my script shorter?

Post by teadrinker » 03 Oct 2022, 05:55

@boiler
I think your A_Index is not the same A_Index. :)

Siff2Gi
Posts: 2
Joined: 03 Oct 2022, 04:50

Re: Way to make my script shorter?

Post by Siff2Gi » 03 Oct 2022, 06:43

boiler wrote:
03 Oct 2022, 05:32
The clear way to make your script shorter is to use loops instead of repeating the same code 10 times followed by another piece of code, then repeating all of that again. You apparently know what a loop is. Why not employ it for this?

Code: Select all

^q::
#Persistent
SetTimer, noafk, 1000
return

noafk:
	WinGet, winList, List, ahk_class Chrome_WidgetWin_1
   	Loop % winList
   	{ 
		loop, 2
		{
			loop, 10
			{
				ControlFocus, , % "ahk_id " winList%a_index%	
				Sleep, 450
				ControlSend, , s,% "ahk_id " winList%a_index%
				ControlSend, , g,% "ahk_id " winList%a_index%
				Sleep, 1200
				ControlSend, , l,% "ahk_id " winList%a_index%
				Sleep, 500
				ControlSend, , {Backspace},% "ahk_id " winList%a_index%
				Sleep, 600
			}

			ControlFocus, , % "ahk_id " winList%a_index%	
			Sleep, 450
			ControlSend, , y,% "ahk_id " winList%a_index%
			Sleep, 450
		}
   	}
return
Escape::
ExitApp
Return

Since you have set it to repeat using SetTimer, I don’t see why it would not continue executing.
Thanks for your response!
But it executes the script 1 single time and thats it.. it goes 1 time s,g,l,back and y and then nothing happens :/

User avatar
boiler
Posts: 16771
Joined: 21 Dec 2014, 02:44

Re: Way to make my script shorter?

Post by boiler » 03 Oct 2022, 06:59

teadrinker wrote:
03 Oct 2022, 05:55
@boiler
I think your A_Index is not the same A_Index. :)
Good point. Easily correctable (as you know) by assigning it to another variable outside the inner loops and replace with that variable.

Post Reply

Return to “Ask for Help (v1)”