If Website Contains A Text Then Press a Key

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Autom8IT
Posts: 1
Joined: 22 Sep 2018, 03:08

If Website Contains A Text Then Press a Key

22 Sep 2018, 03:10

Hi,

Basically, I have this script:

Code: Select all

string1 = {Numpad2}{Return} 

string2 = {Numpad1}{Numpad4}{Return}

string3 = {Numpad1}{Numpad0}{Return}

Loop
{
Random, Var,1,3
Send, % string%Var%
Sleep,20000
}

Loop,
{
#Persistent
SetTimer, PressTheKey, 20000
Return

PressTheKey:
Send, {{Numpad2}{Return}}
Sleep, 20000

Send, {{Numpad1}{Numpad4}{Return}}
Sleep, 20000

Send, {{Numpad1}{Numpad0}{Return}}
Sleep, 20000
Return
}
The script above is intended to be used in Google Chrome on Windows 10.

What this script does is, that every 20 seconds, it randomly types either 2 followed by Enter keys or 14 followed by Enter keys or 10 followed by Enter keys.

So far so good.

I have the following questions:

1) At its' current state, the script I have runs indefinitely in a loop. Only way for me to stop it is either manually or by using Task Scheduler to shut down the PC after specific amount of time. I want the above loop to end after 1 hour or 2 or 3. Meaning, I want to be able to control how long the script runs.

2) At its' current state, when I run the script to test it, it works fine. However, with only Google Chrome being as the open and active window where the script is intended to run, sometime the Google Chrome becomes inactive on it's own. Meaning, that the script is just running on Desktop, where it has no affect or purpose. I'm not sure if this problem is caused by the script, but I want to be able to lock the script to only run on the active fully maximized active Google Chrome window.

3) I want each of the 3 strings above to be dependent on what is shown on the app in use.

For instance, using Chrome on my Windows 10 machine I want the script to do the following:

a) If a Web-page contains the word "summer" then execute "string1".

b) If a Web-page contains the word "winter" then execute "string2".

c) If a Web-page contains the word "autumn" then execute "string3".

4) Finally, the execution of the strings should, however, be dependent on two or more variables. For instance, if a Web-page contains either the word "autumn" or "spring" or "snowstorm" then execute "string3".

I hope you guys can help :)

Thank you!

Please note, I'm really new in to this, so please, do not assume I would understand everything, unless you explain :dance:
buttshark
Posts: 62
Joined: 22 Apr 2017, 20:57

Re: If Website Contains A Text Then Press a Key

22 Sep 2018, 03:38

1. It looks like your loop makes 1 iteration every 20 seconds. 1 hour = 60 min = 180 20 second chunks. Therefore, you can loop 180 times, and it should finish in about an hour.

Code: Select all

Loop 180
{
...
}
2. you can fix this by making sure every time you run through the loop, you are on Chrome. If you aren't, exit the script.

Code: Select all

Loop 180
{
	WinGetActiveTitle, title
	if(!InStr(title, "Google Chrome"))
		ExitApp
	...
3. Unfortunately, this is kind of difficult. It is possible to download the html file and parse it, but if the text you are looking for isn't in the raw html, I am unaware of how to do that, so this might work, but no promises.

Code: Select all

URLDownloadToFile, yourUrl, tmp.html ;replace yourUrl with the url you are on
FileRead, html, tmp.html
FileDelete, tmp.html
if(InStr(html, "summer"))
	send, % string1
else if(InStr(html, "winter"))
	send, % string2
else if(InStr(html, "autumn"))
	send, % string2
4.

Code: Select all

if(InStr(html, "autumn") OR InStr(html, "spring") OR InStr(html, "snowstorm"))
	send, % string3
P.S. I'm just assuming you mis-copy/pasted, as

Code: Select all

Loop,
{
#Persistent
SetTimer, PressTheKey, 20000
Return

PressTheKey:
Send, {{Numpad2}{Return}}
Sleep, 20000

Send, {{Numpad1}{Numpad4}{Return}}
Sleep, 20000

Send, {{Numpad1}{Numpad0}{Return}}
Sleep, 20000
Return
}
never gets executed, because you never leave the first loop.

Good luck!
I have no idea what I'm doing.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, mikeyww and 318 guests