Jump to content
vanillaslice

Having trouble making an Error Checking Function

Recommended Posts

Hey all!

I've been trying to make script that pulls hashtags from posts on Instagram so I can make some analytics. However I'm very new to AutoIt, and coding in general, so I thought I'd just familiarise myself with the Syntax before getting ahead of myself.

After a few days of straining to understand the help file and other posts I can't seem to fully comprehend Functions and Loops. I've been trying to make a check function that makes sure that the post preview section is on screen, and that a post is loaded. I have the variables which imply those states figured out, and what I think is a functional check/loop. However when it runs the LoadChk() function, the script seems to continue running without waiting for it to finish.

I feel like this is some really basic Syntax error, but I've tried my best to understand it and have come up short. Any help would be appreciated!

Global $iCounter = 0
Global $iOperations = 100

While $iCounter < $iOperations
   LoadChk()        ;Checks if post preview window is open, and the next post has loaded.

   Sleep (500)
   Send("^f")
   Send({ESC})      ;Here I'll put my search/record function. For now just closes the find tool and continues.
   Send("{RIGHT}")
   
   $iCounter = $iCounter + 1
WEnd

Func LoadChk()
   Local $iBGcolour = PixelGetColor(1355, 577)
   Local $iPOSTcolour = PixelGetColor(2081, 619)
   Local $iWaitCount = 0

      If $iBGcolour <> 526601 Then      ;If it is not this colour it means the post preview window is closed.
         MsgBox(0,"Error", "No Post Selected.")
         Exit
      ElseIf $iPOSTcolour = 526601 Then ;If it is this colour then it means post has not loaded.
         While $iPOSTcolour = 526601    ;Check again two more times, otherwise end the script.
            Sleep(1000)
            If $iWaitCount > 2 Then
               MsgBox(0, "Error", "Post not loading.")
               Exit
            EndIf
            iWaitCount = $iWaitCount + 1
            $iPOSTcol = PixelGetColor(2081, 619)
         WEnd
      EndIf
EndFunc

Func Terminate()
   Exit
EndFunc

 

Share this post


Link to post
Share on other sites
1 hour ago, Nine said:

Take a serious look at IE UDF.  Trying to automate a web site with colors and stuff will get you nowhere.

I see, this all looks very useful. Thanks!

While I appreciate your help, the reason I post is to understand why the script seems to continue without waiting for the LoadChk() to finish. I'd like to be sure I fully understand how to set up Error Checking functions, regardless of the environment they're in.

I've confirmed that it's pulling the correct colours, and that it detects it needs to wait for two more rechecks. Although when the LoadChk() runs it seems to continue with the script before it's had a chance to finish the function. Resulting in a bunch of keys being sent and making a mess before the error MsgBox appears and Exits the script.

 

Again, I really appreciate the time.

Share this post


Link to post
Share on other sites
2 hours ago, vanillaslice said:

$iPOSTcol = PixelGetColor(2081, 619)

missing a our there !

Like I said so many times, programmers of autoIt should always use :

Opt ("MustDeclareVars", 1)

At the beginning of their scripts.  It saves a lot of time debugging...

Edited by Nine

Share this post


Link to post
Share on other sites

 

4 hours ago, Nine said:

missing a our there !

Like I said so many times, programmers of autoIt should always use :

Opt ("MustDeclareVars", 1)

At the beginning of their scripts.  It saves a lot of time debugging... 

This is always a good advice :thumbsup:.

If anyone like to have it really fierce, she/he can also use the following compiler directive :

#AutoIt3Wrapper_AU3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7

https://www.autoitscript.com/autoit3/docs/intro/au3check.htm

-q : quiet (only error/warn output)
-d : as Opt("MustDeclareVars", 1)
-w 1 : already included file (on)
-w 2 : missing #comments-end (on)
-w 3 : already declared var (off)
-w 4 : local var used in global scope (off)
-w 5 : local var declared but not used (off)
-w 6 : warn when using Dim (off)
-w 7 : warn when passing Const or expression on ByRef param(s) (on)

Opt('MustDeclareVars', 1) can then be omitted because the flag -d sets the "MustDeclareVars" option to True.

Note :
I do not use the parameter -w 5 during development. Especially for larger projects it is not uncommon to declare local variables without using them immediately (only warnings - no errors - are issued, but this can be nerve-racking).
At the end of a project you can 'activate'  -w 5 to find unused local variables.

At the beginning, this directive can be very annoying because it reveals many weaknesses, but on the long run, it makes scripts neater.


Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...