Need Assistance with Loop and FindClick Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
-rozinante-
Posts: 3
Joined: 15 Jan 2022, 13:11

Need Assistance with Loop and FindClick

Post by -rozinante- » 15 Jan 2022, 13:23

Hello everyone. Here's the problem:
I'm attempting to print to pdf an online service manual for my truck. Due to javascript limitations, I am only able to access these pages via the browser. I need to recursively detect an icon in the browser window, click the icon, and then click a button which enables me to print the page to pdf.

I can successfully print the parsed loop to a msgbox, one coordinate pair at a time. However, I need to pass each coordinate, separated into x and y, into the function which allows me to mouseclick the print button. I assume this is done via StrSplit but can't seem to get it to work?

Code: Select all

#Include findclick.ahk

f12::
results := FindClick("C:\users\jl\desktop\icon.png", "e n")
	;msgbox,,, %results% ;msgbox of complete list of coordinate pairs

Loop, parse, results, `n 
	;msgbox,,, %A_Loopfield% ; msgbox of each coordinate pair row

SplitArray := ""
SplitArray := StrSplit(results, "`n")
	msgbox,,, %SplitArray%
	
	
	print2PDF(xin, yin)
	{
		mouseclick, l, xin, yin ;click on tree item
		Sleep 2000
		MouseClick, l, 2052, 135 ;click on print button
		Sleep 500
		Send {enter}
		WinWait, Save As
		Sleep 1500
		Send {enter}
		Sleep 2000
	}
return

esc::Pause

Thank you for any help or ideas you might have.

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

Re: Need Assistance with Loop and FindClick  Topic is solved

Post by boiler » 15 Jan 2022, 15:09

You have several issues. One is that you didn't define a code block for your parsing loop, so it's only looping on the following line, which is SplitArray := "". Then, the split would be on a comma, not a newline character. The results are a pair of coordinates on each line, and within each line, x and y are separated by a comma. And the variable you are splitting is A_LoopField because that's what the parsing loop assigns it to for each iteration of the loop. Then, you can't just put an array variable name in a MsgBox and display it. You have to identify the elements you want to display (which in this case would be both of them).

Code: Select all

#Include findclick.ahk

f12::
	results := FindClick("C:\users\jl\desktop\icon.png", "e n")
		;msgbox,,, %results% ;msgbox of complete list of coordinate pairs

	Loop, parse, results, `n
	{
		SplitArray := StrSplit(A_LoopField, ",")
		msgbox, % SplitArray.1 "," SplitArray.2
	}
return
	
print2PDF(xin, yin)
{
	mouseclick, l, xin, yin ;click on tree item
	Sleep 2000
	MouseClick, l, 2052, 135 ;click on print button
	Sleep 500
	Send {enter}
	WinWait, Save As
	Sleep 1500
	Send {enter}
	Sleep 2000
}

esc::Pause

Demo of splitting the results only:

Code: Select all

results =
(
12,25
22,48
2,80
16,102
)

Loop, parse, results, `n
{
	SplitArray := StrSplit(A_LoopField, ",")
	msgbox, % SplitArray.1 "," SplitArray.2
}
return

-rozinante-
Posts: 3
Joined: 15 Jan 2022, 13:11

Re: Need Assistance with Loop and FindClick

Post by -rozinante- » 15 Jan 2022, 15:41

Thank you for all of your help, the splitting is working beautifully.

I think I may be struggling with the use and syntax of arrays...this is my first time using them in a script. Am I able to save the splits (eg %SplitArray.1%) to a variable in some way, in order to pass this data into my "print2PDF" function? When I try, it says I have an illegal character, which I'm sure is the period.

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

Re: Need Assistance with Loop and FindClick

Post by boiler » 15 Jan 2022, 18:26

-rozinante- wrote: Am I able to save the splits (eg %SplitArray.1%) to a variable in some way, in order to pass this data into my "print2PDF" function? When I try, it says I have an illegal character, which I'm sure is the period.
Yes, you can save them or use their values however you want, but you don’t put % signs around them. That is how variables are used in legacy syntax, but only simple variables can be used in legacy syntax. You have to use expressions when you use arrays, and expressions have a different syntax. For assigning their values to other variables, you would use the expression assignment operator := like this:

Code: Select all

FoundX := SplitArray.1
FoundY := SplitArray.2
However, it won’t make much sense to save them to simple variables inside that loop because you’ll just be overwriting them each time through the loop. I suppose you could send them to your PDF from inside the loop, but then you wouldn’t even have to re-assign them to other variables. You could just use SplitArray.1 and SplitArray.2 directly when calling your PDF function.

If you do want to save them for use after you exit the loop, then you need to assign them to an array that would end up containing all the results, which I can show you how to do if you want.

-rozinante-
Posts: 3
Joined: 15 Jan 2022, 13:11

Re: Need Assistance with Loop and FindClick

Post by -rozinante- » 16 Jan 2022, 09:44

Just wanted to take a moment to come back and thank you for your generosity and expertise, boiler. With your help, I was able to get my script up and running late last night. Your hint with the arrays also helped me to find some resources that made sense, so I won't monopolize your time. Thank you again for all your help.

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

Re: Need Assistance with Loop and FindClick

Post by boiler » 16 Jan 2022, 10:22

:thumbup: Glad to help and to see that you are learning to use AHK even more effectively.

Post Reply

Return to “Ask for Help (v1)”