Jump to content

Fuction always resets itself?


twend
 Share

Recommended Posts

What's wrong with this code?

It keep spamming the If ($counter = 1), it doesn't move on elseif counter = 2,3 or 4.

Even the it's Global $counter = 0

Seems like the function always forgets the counting number, it starts always all over again from 0.

Also how I can use EndIf/Until code?

Like end the function after it has done all the if's/else if's.

EndIf ($counter = < 4/5)?

 

 
Global $counter = 0
 
Func pasteToProgram($data)
   WinExists( "Untitled - Notepad" )
   $counter = + 1
 
 
If ($counter = 1) Then
MouseClick("left", 131, 277, 2, 10) ; 
Send("{CTRLDOWN}v{CTRLUP}") ; paste the name
Sleep(200)
 
ElseIf ($counter = 2) Then
MouseClick("left" , 120, 292, 2, 10)
Send("{CTRLDOWN}v{CTRLUP}") ; paste the name
sleep(200)
 
 
ElseIf ($counter = 3) Then
  MouseClick("left", 132, 309 , 2, 10)
  Send("{CTRLDOWN}v{CTRLUP}") ; paste the name
  sleep(200)
 
 
ElseIf ($counter = 4) Then
 
  MouseClick( "left", 131, 325, 2, 10)
  Send("{CTRLDOWN}v{CTRLUP}") ; paste the name
  sleep(200)
 
EndIf
EndFunc

 

 

Edited by twend
Link to comment
Share on other sites

Your variable assignment is off I believe.

Try

$Count += 1

As far as the "ending" part. What you have there isn't in a loop, so why would you need to end it?

If you want it to reset as if it hasn't ran before then so a last EndIf

ElseIf $Counter = 5 Then
$Counter = 0
EndIf

Or just add a $Counter = 0 after the $Counter = 4 ElseIf

Edited by Damein

MCR.jpg?t=1286371579

Most recent sig. I made

Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic

Link to comment
Share on other sites

Not trying to be rude or anything, but the short answer is: a lot :)

 

First: if you post your code in an autoit code block here on the forum, it's much more readable. Indented, syntax-highlighted in pretty colors, etc..

 

Second: WinExists() does nothing on its own. You need to do something with its return value, which is, as the help file states, either 0 ( => window was not found) or 1 ( => window was found).

If WinExists("AutoIt Help") Then
    ConsoleWrite("It's aliiiiive")
Else
    ConsoleWrite("Oh noez, it's dead!")
EndIf

; or, longer but perhaps easier to understand:

$returnValue = WinExists("AutoIt Help")

If $returnValue == 1 Then
    ConsoleWrite("It's aliiiiive")
ElseIf $returnValue == 0 Then
    ConsoleWrite("Oh noez, it's dead!")
Else
    ; Assuming that the function WinExists doesn't have a bug, this block will never be reached.
    ConsoleWrite("Spacetime continuum collapse imminent; nearing black hole event horizon.")
EndIf

 

 

Third: $counter = + 1 will just set the variable $counter to +1 ( which is just 1). You are looking for the += operator, like Damein said.

 

Fourth: simulating CTRL+v can be done more weller betterly. For instance, this code will duplicate itself if you run it from within SciTE with your cursor all the way down under the script (and is also an example for that counter thing):

$counter = 1
$counter += 1
$counter += 1
ConsoleWrite($counter)

send("^a")
send("^c")
send("^v")
send("^v")

There are modifiers for ALT, SHIFT and WINDOWSKEY as well (resp. !, + and #), see the Send() help page.

Fifth: EndIf and Until are quite different. EndIf denotes the end of an If, ElseIf or Else block, and Until denotes the end of a Do block. The Until also requires a truth expression to make a decision on whether to continue the loop, i.e. something that is either true or false (a.k.a. boolean). Example:
 

$counter = 0
Do
    $counter += 1
    ConsoleWrite("Are we there yet?" & @CRLF)
    sleep(500)
Until $counter == 10

ConsoleWrite("No. It's much, MUCH further. We haven't even left the city yet. I'll probably look like ZZ Top when we arrive at Grandma's." & @CRLF)

There is another loop that is somewhat similar, but not exactly:
 

$age = 10
While $age < 18
    ConsoleWrite("No, you cannot get your drivers license yet." & @CRLF)
    sleep(500)
    $age += 1
WEnd

ConsoleWrite("Congratulations, you can now apply for a license." & @CRLF)

Assignment (if you feel like learning something): try to figure out the fundamental difference between Do/Until and While/WEnd. It's small but very important. Find a good example for both styles of looping. Many experienced programmers still have trouble with this.

 

Sixth: Instead of If/ElseIf/EndIf, you may want to look into Switch/Case/EndSwitch or Select/Case/EndSelect. Optional, but it can make your code much easier to read if it grows longer.

 

Hope you learned something :)

Good luck.

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Your variable assignment is off I believe.

Try

$Count += 1

As far as the "ending" part. What you have there isn't in a loop, so why would you need to end it?

If you want it to reset as if it hasn't ran before then so a last EndIf

ElseIf $Counter = 5 Then
$Counter = 0
EndIf

Or just add a $Counter = 0 after the $Counter = 4 ElseIf

 

I did what you told and now it's saying ""If" statement has no matching "EndIf" statement"

And thanks for the answers ;)

Edited by twend
Link to comment
Share on other sites

I did what you told and now it's saying ""If" statement has no matching "EndIf" statement"

 

I wrote you an entire lecture on basic programming concepts earlier today (edit: and not just to preach either, I merely went through your code and answered your questions), containing two working examples of if blocks. There are also hundreds of AutoIt help files with functioning examples of it. Come on now Twend...

/edit: Even if you just look at the English of it...

IF (it rains outside) THEN

(we take an umbrella)

ELSE 

(we take our sunglasses)

ENDIF

It's not that hard :) Start with the IF and give it something to evaluate. Then give it something to do if the evaluation is true, and something *else* to do if the evaluation is false. Then tell it to continue unconditionally again by saying "endif", i.e. "this is the end of the if thingy".

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

I don't just understand, I just started the coding with autoit.

I'm not very good with it yet.

The code has something to evaluate it's the $counter

There's 

Global $counter = 0

Func pasteToProgram($data)
   WinExists( "Untitled - Notepad" )
   $counter +=  1 // It should add +1 to the Global counter at this part.

 

If ($counter = 1) Then // if the counter is 1, it does the this.

Clicks 136, 233

ElseIf ($counter = 2) Then //if the counter is 2, it does this part.

Clicks 123,123

EndIf

EndFunc

This is the error message I get still.

 

Variable used without being declared.:

$counter += 1
^ ERROR
Edited by twend
Link to comment
Share on other sites

Post the full script or at least one that shows the problem. I'm going out on a limb here and guessing that you're calling the function before declaring the variable, probably declared in another function instead of at the top of the script. Or, your function is physically located in the script prior to where you have the declaration line, and the error is coming from Au3Check and not when you run the script.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Post the full script or at least one that shows the problem. I'm going out on a limb here and guessing that you're calling the function before declaring the variable, probably declared in another function instead of at the top of the script. Or, your function is physically located in the script prior to where you have the declaration line, and the error is coming from Au3Check and not when you run the script.

 

Omg thank you! It was the problem! The global $counter = 0 was in a wrong place!

It was supposed to be in top of the script!

thanks a lot <3

How I can make the function loop, like it does only the function once atm.

But it works!

If $line_data[0] = 3 Then 
 Call("pasteToProgram", $line_data[1])
   EndIf
 
It calls the function only once at the moment, how I make loop for the function it loops the function until there are no anything left to copy/paste?
Edited by twend
Link to comment
Share on other sites

Here you go, a function called 10 times from a loop:

For $i = 1 To 10
    testfunction($i)
Next

Exit

Func testfunction($number)
    ConsoleWrite($number & @CRLF)
EndFunc   ;==>testfunction

Also, please post your code in an autoit code box (the AutoIt logo button in the full editor).

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Something like this could work

Global $counter = 0
While Something()
WEnd
Func Something()
    $counter+=1
    if $counter = 1 Then
        ConsoleWrite("1" & @CRLF)
    ElseIf $counter = 2 Then
        ConsoleWrite("2" & @CRLF)
    ElseIf $counter = 3 Then
        ConsoleWrite("3" & @CRLF)
    ElseIf $counter = 4 Then
        ConsoleWrite("4" & @CRLF)
        $counter = 0
        Return False
    EndIf
    Return True
EndFunc

Or, if you are ok with terminating the full execution, add an Exit where the 'return false' is.

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...