Cleaning up code.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
idawood
Posts: 4
Joined: 29 May 2020, 21:39

Cleaning up code.

17 May 2021, 06:04

Hello,

I want help cleaning up this code to make it both neater and shorter. This code is to test account creation in my personal website.

Code: Select all

; this script opens the bookmark, generates a random name, and inputs an age.
SetWorkingDir %A_ScriptDir% 
#SingleInstance, Force

name := ""
Loop 10 {
  random, c, 97, 97 + 26
  random, n, 0, 10
  name .= chr(c) . n
}

; this presses the bookmark
MouseMove, 40, 85
Click

; this make sures the page is fully loaded.
loop {
    CoordMode Pixel
    ImageSearch, x0, y0, 560, 565, 1200, 670, loaded1.png
    if(!ErrorLevel) {
        break
    }
    else if(ErrorLevel = 1 ) {
        CoordMode Pixel
        ImageSearch, x1, y1, 560, 565, 1200, 670, loaded2.png
    if(!ErrorLevel) {
        break
    }
    else if (ErrorLevel = 1 ) {
        CoordMode Pixel
        ImageSearch, x2, y2, 560, 565, 1200, 670, loaded3.png
    if(!ErrorLevel) {
        break
    }
    else if (ErrorLevel = 1 ) {
        continue
    
}
}
}
}


sleep, 2000

; this presses the name field in the page.
MouseMove, 955, 630
Click
sleep, 100

; this fills in the name field in the page.
Send, %name%
sleep, 100

; this presses the continue button in the page.
MouseMove, 830, 690
Click
sleep, 100

; this makes sure the age page is loaded.
loop {
    CoordMode Pixel
    ImageSearch, x3, y3, 745, 590, 1325, 700, loadedAge1.png
    if(!ErrorLevel) {
        break
    }
    else if (ErrorLevel = 1 ) {
        CoordMode Pixel
        ImageSearch, x4, y4, 745, 590, 1325, 700, loadedAge2.png
    if(!ErrorLevel) {
        break
    }
    else if (ErrorLevel = 1 ) {
        CoordMode Pixel
        ImageSearch, x5, y5, 745, 590, 1325, 700, loadedAge3.png
    if(!ErrorLevel) {
        break
    }
    else if (ErrorLevel = 1 ) {
        CoordMode Pixel
        ImageSearch, x6, y6, 745, 590, 1325, 700, loadedAge4.png
    if(!ErrorLevel) {
        break
    }
    else if (ErrorLevel = 1 ) {
        continue
}
}
}
}
}
sleep, 1000

; this fills in the birthday parameters randomly.
Send {tab}
Random, month, 1, 12
Send, %month%
sleep, 1000

Send {tab}
Random, day, 1, 28
Send, %day%
sleep, 1000

Send {tab}
Random, year, 1970, 2000
Send, %year%
sleep, 1000

Send {enter}
sleep, 1000
Send {enter}
sleep, 5000

; this exits out of the prompts.
MouseMove, 925, 975
loop, 10 {
    Click
}

return

Thank you in Advance.
Rohwedder
Posts: 7610
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Cleaning up code.

17 May 2021, 06:40

Hallo,
perhaps?:

Code: Select all

; this script opens the bookmark, generates a random name, and inputs an age.
SetWorkingDir %A_ScriptDir%
#SingleInstance, Force
CoordMode Pixel
name := ""
Loop 10
{
	random, c, 97, 97 + 26
	random, n, 0, 10
	name .= chr(c) . n
}
; this presses the bookmark
MouseMove, 40, 85
Click
; this make sures the page is fully loaded.
Loop
{
	Na := Mod(A_Index-1, 3) ; 0-2
	Nb := Na+1 ; 1-3
	ImageSearch, x%Na%, y%Na%, 560, 565, 1200, 670, loaded%Nb%.png
}
Until !ErrorLevel
sleep, 2000
; this presses the name field in the page.
MouseMove, 955, 630
Click
sleep, 100
; this fills in the name field in the page.
Send, %name%
sleep, 100
; this presses the continue button in the page.
MouseMove, 830, 690
Click
sleep, 100
; this makes sure the age page is loaded.
Loop
{
	Na := Mod(A_Index-1, 4)+3 ; 3-6
	Nb := Na-2 ;1-4
	ImageSearch, x%Na%, y%Na%, 745, 590, 1325, 700, loadedAge%Nb%.png
}
Until !ErrorLevel
sleep, 1000
; this fills in the birthday parameters randomly.
Send {tab}
Random, month, 1, 12
Send, %month%
sleep, 1000
Send {tab}
Random, day, 1, 28
Send, %day%
sleep, 1000
Send {tab}
Random, year, 1970, 2000
Send, %year%
sleep, 1000
Send {enter}
sleep, 1000
Send {enter}
sleep, 5000
; this exits out of the prompts.
MouseMove, 925, 975
Click, 10
return
User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: Cleaning up code.

17 May 2021, 08:33

The version below uses functions and some other approaches to neaten/shorten some more. Also, changed the end range of the random characters to 97 + 25, which is a to z, instead of 97 + 26, which is a to {.

Code: Select all

; this script opens the bookmark, generates a random name, and inputs an age.
SetWorkingDir %A_ScriptDir%
#SingleInstance, Force
CoordMode Pixel
name := ""
Loop 10
	name .= chr(Random(97, 97 + 25)) . Random(0, 10)
Click, 40, 85 ; this presses the bookmark
LoadWait(2000) ; this make sures the page is fully loaded.
SendSleep("{Click 955 630}", 100) ; this presses the name field in the page.
SendSleep(name, 100) ; this fills in the name field in the page.
SendSleep("{Click 830 690}", 100) ; this presses the continue button in the page.
LoadWait(1000) ; this makes sure the age page is loaded.
; this fills in the birthday parameters randomly.
SendSleep("{tab}" . Random(1, 12), 1000)
SendSleep("{tab}" . Random(1, 28), 1000)
SendSleep("{tab}" . Random(1970, 2000), 1000)
SendSleep("{enter}", 1000)
SendSleep("{enter}", 5000)
; this exits out of the prompts.
MouseMove, 925, 975
Click, 10
return

SendSleep(str, delay) {
	Send, % str
	Sleep, delay
}

Random(min, max) {
	Random, r, min, max
	return r
}

LoadWait(delay) {
	Loop {
		Na := Mod(A_Index-1, 4)+3 ; 3-6
		Nb := Na-2 ;1-4
		ImageSearch, x%Na%, y%Na%, 745, 590, 1325, 700, loadedAge%Nb%.png
	} Until !ErrorLevel
	Sleep, delay
}
idawood
Posts: 4
Joined: 29 May 2020, 21:39

Re: Cleaning up code.

17 May 2021, 09:04

Thanks a lot.
Rohwedder
Posts: 7610
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Cleaning up code.

17 May 2021, 09:45

Two different pages must be waited for:
first the page: ImageSearch, x%Na%, y%Na%, 560, 565, 1200, 670, loaded%Nb%.png and then
the age page: ImageSearch, x%Na%, y%Na%, 745, 590, 1325, 700, loadedAge%Nb%.png
User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: Cleaning up code.

17 May 2021, 09:51

Oops...missed that. Correction:

Code: Select all

; this script opens the bookmark, generates a random name, and inputs an age.
SetWorkingDir %A_ScriptDir%
#SingleInstance, Force
CoordMode Pixel
name := ""
Loop 10
	name .= chr(Random(97, 97 + 25)) . Random(0, 10)
Click, 40, 85 ; this presses the bookmark
LoadWait(560, 565, 1200, 670, "loaded", 2000) ; this make sures the page is fully loaded.
SendSleep("{Click 955 630}", 100) ; this presses the name field in the page.
SendSleep(name, 100) ; this fills in the name field in the page.
SendSleep("{Click 830 690}", 100) ; this presses the continue button in the page.
LoadWait(745, 590, 1325, 700, "loadedAge", 1000) ; this makes sure the age page is loaded.
; this fills in the birthday parameters randomly.
SendSleep("{tab}" . Random(1, 12), 1000)
SendSleep("{tab}" . Random(1, 28), 1000)
SendSleep("{tab}" . Random(1970, 2000), 1000)
SendSleep("{enter}", 1000)
SendSleep("{enter}", 5000)
; this exits out of the prompts.
MouseMove, 925, 975
Click, 10
return

SendSleep(str, delay) {
	Send, % str
	Sleep, delay
}

Random(min, max) {
	Random, r, min, max
	return r
}

LoadWait(x1, y1, x2, y2, name, delay) {
	Loop {
		Na := Mod(A_Index-1, 4)+3 ; 3-6
		Nb := Na-2 ;1-4
		ImageSearch, x%Na%, y%Na%, x1, y1, x2, y2, %name%%Nb%.png
	} Until !ErrorLevel
	Sleep, delay
}
Rohwedder
Posts: 7610
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Cleaning up code.

17 May 2021, 11:36

Sorry! I really don't want to be petty, but:
first the page:
Na := Mod(A_Index-1, 3) ; 0-2
Nb := Na+1 ; 1-3

and then the age page:
Na := Mod(A_Index-1, 4)+3 ; 3-6
Nb := Na-2 ;1-4

and maybe LoadWait() should be global?
User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: Cleaning up code.

17 May 2021, 11:52

You’re not being petty. Thanks for pointing that out. I clearly should have looked at the two pieces of code more closely. Given that there are so many differences between them, I’m now thinking they should probably just be left as they were rather than to make a function out of them and pass it a whole bunch of parameters. The rest of the functions and other changes do plenty to help streamline the code.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 263 guests