Else statement for If(InStr(................) is Failing

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sjdheunaskdf9384348
Posts: 14
Joined: 29 Dec 2015, 16:52

Else statement for If(InStr(................) is Failing

30 Dec 2015, 16:08

Hello everyone! I have a script I've been working on for a few days (my first script ever!), and it is almost working perfectly. However, I tried adding a new feature but it isn't working as intended.

Basically, the script copies the whole page and then searches through the text for certain strings. It then completes actions depending on the string(s) found. That part is working fine. From time to time, a different page (or a popup) is clicked, which doesn't have any of the strings I am looking for. I've been working on getting the script to check for that and close it to go back to the main page again.

So far I've tested it by opening a page I know doesn't have any of the strings I am looking for. It successfully closes the page, and restarts the main loop. However, once it is on the page that has the strings I am looking for (I verified by checking the contents of the clipboard, and the info was definitely there) it closes that page as well.

I have an array that contains the strings and it is looped through. Here is the part of the code (the else statement) that is giving me problems:

Code: Select all

If(InStr(Clipboard,storeValue) > 0)  ; storeValue is the current value from the array 
		{
			SendInput {CTRL down}
			sleep, miniRoll
			SendInput {f}{CTRL up}
			sleep, smallRoll
			SendInput %storeValue%
			sleep, smallRoll
			SendInput {ESCAPE}
			sleep, miniRoll
			SendInput {ENTER}
			sleep smallRoll
			found := AIndex     ; AIndex is a variable I made so it can be reset to 0, which A_Index can't
			
			If(found = 1)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, hourRoll
				closeRefresh()
				continue wow 
			}
			
			else if(found = 2)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, shortRoll
				closeRefresh()
				continue wow 
			}
			
			else if (found = 3)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, shortRoll
				closeRefresh()
				continue wow 
			}
			
			else if (found = 4)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, bigRoll
				closeRefresh()
				continue wow 
			}
			
			else if (found = 5)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, bigRoll
				closeRefresh()
				continue wow
			}
			
			else if (found = 6)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, bigRoll
				closeRefresh()
				continue wow 
			}

			else if (found = 7)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, bigRoll
				closeRefresh()
				continue wow  
			}
		}

		else 
		{
			closeRefresh()
			continue wow
		}	
Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

Re: Else statement for If(InStr(................) is Failing

30 Dec 2015, 17:02

I do not see any issues with the code portion you provided. However, you did not provide the full script, so I am unable to try and replicate the issue. My suspicion is that the error lies in the code that loads the page contents into the clipboard. Most likely, it is either not refreshing the clipboard at all when going back to the original page or it is not waiting for the clipboard update to complete before moving on to the If InStr().
sjdheunaskdf9384348
Posts: 14
Joined: 29 Dec 2015, 16:52

Re: Else statement for If(InStr(................) is Failing

30 Dec 2015, 17:26

Thank you for your response! I'm hoping it is something simple. I know my code is sloppy (first time making a script!) but I want to focus on getting this new feature working before I optimize. Right now, it does everything I want (except close a tab if it's contents do not match any of the strings). Basically how the website works is you can find/use one string, and when you refresh the page it won't be available again until you find/use another string. My first three strings are priority (in an ideal situation, I'd loop between the three of them, but they aren't always available). That is why I call the main loop each time something is successful, since it restarts and searches from the beginning. Some of the strings require different actions, which is why there are so many "random" rolls needed.

--Edit-- I have tested it after the page has refreshed and recopied, and the clipboard contents have string(s) that I am looking for. If I remove the "else" statement, the code runs how I want it (besides closing tabs that don't have any matching strings obviously). Something about that else statement is throwing things off.

Here is my code:

Code: Select all

#z::
SendMode Input
SetWorkingDir %A_ScriptDir% 
SetBatchLines -1
#SingleInstance Force
#Persistent
#NoEnv
#Warn

;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/
;Preparation and Startup
;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/

wow:
Loop 
{
	textArray := {}
	textArray.aText := "asdfasdfasd"
	textArray.bText := "Thasdffhhafh"
	textArray.cText := "asdfhfhafhafdhafdh"
	textArray.dText := "sadfaszzzzzz"
	textArray.eText := "asfgasdgdsga"
	textArray.fText := "asdgasdgsagag"
	textArray.gText := "ahafafhafhadshfasd"

	global AIndex = 0
	global found := AIndex
	global miniRoll
	global smallRoll
	global mediumRoll
	global bigRoll
	global hourRoll
	global shortRoll
	global copyX
	global copyY
	global xClick
	global yClick
	
	Random, miniRoll, 50, 60
	Random, smallRoll, 2000, 3500
	Random, mediumRoll, 5000, 6500
	Random, bigRoll, 1110000, 1140000
	Random, hourRoll, 1000000, 1100000
	Random, shortRoll, 500000, 550000
	Random, copyX, 570, 1000
	Random, copyY, 190, 270

	copyText()
	{
		SendInput {HOME}
		sleep, smallRoll
		SendInput {CTRL down}
		sleep, miniRoll
		Click %copyX%, %copyY%
		SendInput {CTRL up}
		sleep, smallRoll
		SendInput {CTRL down}
		sleep, miniRoll
		SendInput {a}{CTRL up}
		sleep, smallRoll
		SendInput {CTRL down}
		sleep, miniRoll
		SendInput {c}{CTRL up}
		sleep, smallRoll
	}

	copyText()
	
;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/
;Functions
;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/
	outer:
	For storeKey, storeValue in textArray
	{
		AIndex++	
		
		searchForImage()
		{
			ImageSearch, discoveryX, discoveryY, 2, 97, 310, 326, C:\Users\User\Desktop\Script Folder\test_image.png
			discoveryRightSideX := discoveryX + 50
			discoveryRightSideY := discoveryY + 20
			Random, xClick, discoveryX, discoveryRightSideX
			Random, yClick, discoveryY, discoveryRightSideY
		}

		closeRefresh()
		{
			SendInput {CTRL down}
			sleep, miniRoll
			SendInput {w}{CTRL up}
			sleep, mediumRoll
			SendInput {CTRL down}
			sleep, mediumRoll
			SendInput {r}{CTRL up}
			sleep, smallRoll
		}	
			
;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/
;First Search Branch
;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/
		
		If(InStr(Clipboard,storeValue) > 0)
		{
			SendInput {CTRL down}
			sleep, miniRoll
			SendInput {f}{CTRL up}
			sleep, smallRoll
			SendInput %storeValue%
			sleep, smallRoll
			SendInput {ESCAPE}
			sleep, miniRoll
			SendInput {ENTER}
			sleep smallRoll
			found := AIndex
			
			If(found = 1)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, hourRoll
				closeRefresh()
				continue wow 
			}
			
			else if(found = 2)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, shortRoll
				closeRefresh()
				continue wow 
			}
			
			else if (found = 3)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, shortRoll
				closeRefresh()
				continue wow 
			}
			
			else if (found = 4)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, bigRoll
				closeRefresh()
				continue wow 
			}
			
			else if (found = 5)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, bigRoll
				closeRefresh()
				continue wow
			}
			
			else if (found = 6)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, bigRoll
				closeRefresh()
				continue wow 
			}

			else if (found = 7)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, bigRoll
				closeRefresh()
				continue wow  
			}
		}

		else 
		{
			closeRefresh()
			continue wow
		}	
	}
}
Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

Re: Else statement for If(InStr(................) is Failing

30 Dec 2015, 18:07

OK. I see no obvious issues. However, I have made a couple of tweaks that might help, plus a few others for general good practice. I am unable to test it as I do not know a suitable web site/application to try it on. With the changes I made, use Win+Z to start the script, and Win+Z again to stop it (I am firmly opposed to endless loops :lol:).

Code: Select all

SendMode Input
SetWorkingDir %A_ScriptDir% 
SetBatchLines -1
#SingleInstance Force
#Persistent
#NoEnv
#Warn
Return
 
;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/
;Preparation and Startup
;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/
 
#z::
RunMyScript := !RunMyScript
If RunMyScript
	SetTimer, wow, -1
Return

wow:
Loop 
{
	If !RunMyScript
		Break
	textArray := {}
	textArray.aText := "asdfasdfasd"
	textArray.bText := "Thasdffhhafh"
	textArray.cText := "asdfhfhafhafdhafdh"
	textArray.dText := "sadfaszzzzzz"
	textArray.eText := "asfgasdgdsga"
	textArray.fText := "asdgasdgsagag"
	textArray.gText := "ahafafhafhadshfasd"
 
	global AIndex = 0
	global found := AIndex
	global miniRoll
	global smallRoll
	global mediumRoll
	global bigRoll
	global hourRoll
	global shortRoll
	global copyX
	global copyY
	global xClick
	global yClick
 
	Random, miniRoll, 50, 60
	Random, smallRoll, 2000, 3500
	Random, mediumRoll, 5000, 6500
	Random, bigRoll, 1110000, 1140000
	Random, hourRoll, 1000000, 1100000
	Random, shortRoll, 500000, 550000
	Random, copyX, 570, 1000
	Random, copyY, 190, 270
 
	copyText()
 
;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/
; Search Loop
;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/
	outer:
	For storeKey, storeValue in textArray
	{
		If !RunMyScript
			Break
		AIndex++	
 
;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/
;First Search Branch
;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/
 
		If InStr(Clipboard,storeValue)
		{
			SendInput {CTRL down}
			sleep, miniRoll
			SendInput {f}{CTRL up}
			sleep, smallRoll
			SendInput %storeValue%
			sleep, smallRoll
			SendInput {ESCAPE}
			sleep, miniRoll
			SendInput {ENTER}
			sleep smallRoll
			found := AIndex
 
			If(found = 1)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, hourRoll
				closeRefresh()
				continue wow 
			}
 
			else if(found = 2)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, shortRoll
				closeRefresh()
				continue wow 
			}
 
			else if (found = 3)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, shortRoll
				closeRefresh()
				continue wow 
			}
 
			else if (found = 4)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, bigRoll
				closeRefresh()
				continue wow 
			}
 
			else if (found = 5)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, bigRoll
				closeRefresh()
				continue wow
			}
 
			else if (found = 6)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, bigRoll
				closeRefresh()
				continue wow 
			}
 
			else if (found = 7)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, bigRoll
				closeRefresh()
				continue wow  
			}
		}
 
		else 
		{
			closeRefresh()
			continue wow
		}	
	}
}
Return

;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/
; Functions
;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/
copyText()
{
	Clipboard :=
	SendInput {HOME}
	sleep, smallRoll
	SendInput {CTRL down}
	sleep, miniRoll
	Click %copyX%, %copyY%
	SendInput {CTRL up}
	sleep, smallRoll
	SendInput {CTRL down}
	sleep, miniRoll
	SendInput {a}{CTRL up}
	sleep, smallRoll
	SendInput {CTRL down}
	sleep, miniRoll
	SendInput {c}{CTRL up}
	ClipWait
	sleep, smallRoll
	Return
}

searchForImage()
{
	ImageSearch, discoveryX, discoveryY, 2, 97, 310, 326, C:\Users\User\Desktop\Script Folder\test_image.png
	discoveryRightSideX := discoveryX + 50
	discoveryRightSideY := discoveryY + 20
	Random, xClick, discoveryX, discoveryRightSideX
	Random, yClick, discoveryY, discoveryRightSideY
	Return
}

closeRefresh()
{
	SendInput {CTRL down}
	sleep, miniRoll
	SendInput {w}{CTRL up}
	sleep, mediumRoll
	SendInput {CTRL down}
	sleep, mediumRoll
	SendInput {r}{CTRL up}
	sleep, smallRoll
	Return
}
sjdheunaskdf9384348
Posts: 14
Joined: 29 Dec 2015, 16:52

Re: Else statement for If(InStr(................) is Failing

30 Dec 2015, 18:34

Thank you for getting back to me so quickly!

Unfortunately, it is still getting the same error. I loaded it on a page that has at least a few of the strings I am searching for. The page and its contents were successfully copied to the clipboard; but it still closed that page instead of completing the required actions.

Like I said, if I remove the else statement, the script works perfectly (besides what I want the else code to do). But when I put the else code in, it pretty much stops the whole script. All it does is copy the page and then close it.


---EDIT--- I moved the else statement inside "If InStr(Clipboard,storeValue)" just to see what would happen. My code runs fine when there is a match. I went to a tab that I know doesn't have any matching string, and it repeated the copyText function over and over. It seems like if I don't find a match, it keeps copying the page until it finds something that matches. It doesn't run the else statement if its in the larger if statement.

---EDIT #2--- I have placed "Listvars" in a couple of spots to try and figure things out. With the code before, it seems like found wasn't updating with AIndex (which is weird, since it still searches for things if the string is there). So I updated it a little bit so they stay the same. It seems like it cycles through all of the possible choices (so AIndex and Found end up being 7) and then just restarts wow (I think?) or at the very least calls the copyText function again. I tried putting if/else/else if statements within the loops stating if (found > 7) ..... and having it run the closing function. That hasn't worked either. I am continuing to test different things and gather information, this is tough.

I think that shows that the else statement is conflicting with the "If InStr(Clipboard,storeValue)" line for sure, because when the else statement is paired with it, its closes the tabs.
sjdheunaskdf9384348
Posts: 14
Joined: 29 Dec 2015, 16:52

Re: Else statement for If(InStr(................) is Failing

30 Dec 2015, 19:37

I figured it out! Thank you so much for your help, I really appreciate it.

After tons of testing and slamming my head against the keyboard, here is the code that works (note, for the close tab code, part of it is an extension for Chrome allowing a hotkey to close all tabs to the right):

Code: Select all

SendMode Input
SetWorkingDir %A_ScriptDir% 
SetBatchLines -1
#SingleInstance Force
#Persistent
#NoEnv
#Warn
Return

;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/
;Preparation and Startup
;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/

#z::

wow:
Loop 
{
	textArray := {}
	textArray.aText := "asdfasdfasd"
	textArray.bText := "Thasdffhhafh"
	textArray.cText := "asdfhfhafhafdhafdh"
	textArray.dText := "sadfaszzzzzz"
	textArray.eText := "asfgasdgdsga"
	textArray.fText := "asdgasdgsagag"
	textArray.gText := "ahafafhafhadshfasd"
	textArray.hText := "4635724975932759527395379357935793759352343243546356356356"
	
	global AIndex = 0
	global miniRoll
	global smallRoll
	global mediumRoll
	global bigRoll
	global hourRoll
	global shortRoll
	global copyX
	global copyY
	global xClick
	global yClick
	
	Random, miniRoll, 50, 60
	Random, smallRoll, 2000, 3500
	Random, mediumRoll, 5000, 6500
	Random, bigRoll, 1110000, 1140000
	Random, hourRoll, 1000000, 1100000
	Random, shortRoll, 500000, 550000
	Random, copyX, 570, 1000
	Random, copyY, 190, 270
	
	copyText()
	
;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/
; Search Loop
;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/
	outer:
	For storeKey, storeValue in textArray
	{
		AIndex++	
		global found := AIndex
;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/
;First Search Branch
;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/
		
		If InStr(Clipboard,storeValue)
		{
			SendInput {CTRL down}
			sleep, miniRoll
			SendInput {f}{CTRL up}
			sleep, smallRoll
			SendInput %storeValue%
			sleep, smallRoll
			SendInput {ESCAPE}
			sleep, miniRoll
			SendInput {ENTER}
			sleep smallRoll
			found := AIndex
			
			
			If(found = 1)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, hourRoll
				closeRefresh()
				continue wow 
			}
			
			else if(found = 2)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, shortRoll
				closeRefresh()
				continue wow 
			}
			
			else if (found = 3)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, shortRoll
				closeRefresh()
				continue wow 
			}
			
			else if (found = 4)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, bigRoll
				closeRefresh()
				continue wow 
			}
			
			else if (found = 5)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, bigRoll
				closeRefresh()
				continue wow
			}
			
			else if (found = 6)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, bigRoll
				closeRefresh()
				continue wow 
			}
			
			else if (found = 7)
			{
				sleep, mediumRoll
				searchForImage()
				Click %xClick%, %yClick%
				sleep, bigRoll
				closeRefresh()
				continue wow  
			}		
		}
		else if (found > 7)
		{
			closeRefresh()
			continue wow  
		}

	}
}
Return

;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/
; Functions
;/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/--\--/
copyText()
{
	Clipboard :=
	SendInput {HOME}
	sleep, smallRoll
	SendInput {CTRL down}
	sleep, miniRoll
	Click %copyX%, %copyY%
	SendInput {CTRL up}
	sleep, smallRoll
	SendInput {CTRL down}
	sleep, miniRoll
	SendInput {a}{CTRL up}
	sleep, smallRoll
	SendInput {CTRL down}
	sleep, miniRoll
	SendInput {c}{CTRL up}
	ClipWait
	sleep, smallRoll
	Return
}

searchForImage()
{
	ImageSearch, discoveryX, discoveryY, 2, 97, 310, 326, C:\Users\User\Desktop\Script Folder\test_image.png
	discoveryRightSideX := discoveryX + 50
	discoveryRightSideY := discoveryY + 20
	Random, xClick, discoveryX, discoveryRightSideX
	Random, yClick, discoveryY, discoveryRightSideY
	Return
}

closeRefresh()
{
	SendInput {SHIFT down}{ALT down}
	sleep, miniRoll
	SendInput {r}{SHIFT up}{ALT up}
	sleep, smallRoll
	SendInput {CTRL down}
	sleep, miniRoll
	SendInput {w}{CTRL up}
	sleep, smallRoll
	SendInput {CTRL down}
	sleep, miniRoll
	SendInput {r}{CTRL up}
	sleep, smallRoll
	Return
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], RandomBoy, scriptor2016 and 357 guests