Sign in to follow this  
Followers 0
CantCode

AutoIT equivalent of VBScript?

13 posts in this topic

Hey,

I was just wondering if there was an AutoIT equivalent of the VBScript function "Sub".

Thanks.

Share this post


Link to post
Share on other sites



Hey,

I was just wondering if there was an AutoIT equivalent of the VBScript function "Sub".

Thanks.

Look in the help for Func.

ie.

Func MyFunction()

msgbox (0,"title","Doing stuff")

EndFunc

Share this post


Link to post
Share on other sites

Func is as close as it gets to a vbs SUB(routine). and before you ask there is no GoSub. Just call the function.

Func MyFunction($someParam, $anotherparam = 0)

;; In here we write a bunch of code to do something important

Return TRUE

EndFunc

If MyFunction("text") Then ;;do something


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Thanks!

Also, I have a question about While loops, or more specifically, a loop within a loop (Cue Inception music).

So I don't really have questions as to how to do it, but it seems that when I have two whiles and I close with a WEnd, it seems both while loops take the same WEnd, even if I have a second one later on in the code, like:

while $example1
....
while $example2
....
WEnd
....
WEnd

The WEnd at the end (seems to be) ignored.

Thanks.

Share this post


Link to post
Share on other sites

It won't be ignored it's very probable that the condition has not been met.

Also take a look at ExitLoop() in the help file.

If I rewrite your code just a bit you can see what is happening

While 1

While 2

WEnd

WEnd

As long as the first condition is 1 it will stay in the outerloop until another condition 2 is met. It will then stay in the innerloop as long as 2 is true. Then it will exit that inner loop but if the first condition is still true it will stay in that loop. Using ExitLoop(2) in your inner loop would force it to exit both loops as soon as 2 was not true. Nested loops can be a bit tricky sometimes but you will get the hang of them if you look at enough examples in the help file.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Thanks!

Another question, if you don't mind.

Is there a way to have a loop's condition be to whether or not a key is being pressed, and have the loop be to simulate the key press?

For example, say I'm holding down the "R" key, and have it instead of using the keyboard's default key-press signal thing, simulate it repetitively sending a the keypress every 100 ms or so?

I was thinking it might look something like

While ;This is where I'm confused
Send("AutoIT rules")
WEnd

Also, I'm experiencing a strange problem.

After compiling a little test script and trying to run it, it gives me the error "..."If" statements must have a "Then" statement..."

However, the lines it reference are simply variable value declarations (i.e. $sample1 = $sampleValue), and all of my If statements are immediately followed by Then (after the condition, of course).

Also, back on functions. In order to call it to do something, instead of to check what the function returned, how would one do so?

Would one just simply have like:

genRand
MsgBox(0,"The result is...",$randNum)


Func genRand()
    $rnd = Random(0,500,1)
    $randNum = ($rnd + 500)  ;@@@@@@
    return $randNum
EndFunc
Edited by CantCode

Share this post


Link to post
Share on other sites

MsgBox(0,"The result is...",genRand())


Func genRand()
    $rnd = Random(0,500,1)
    $randNum = ($rnd + 500)  ;@@@@@@
    return $randNum
EndFunc

or

$RandNum = genRand()
MsgBox(0,"The result is...",$randNum)


Func genRand()
    $rnd = Random(0,500,1)
    $randNum = ($rnd + 500)  ;@@@@@@
    return $randNum
EndFunc

I don't understand the error about if though since there is no if statement there at all. But that message means you wrote something like

If $Condition genRand()
or

If $Condition
    genRand()
EndIf

The keyword "Then" is missing in both those examples.

For your other question see _IsPressed() function in the help file

User Defined Functions >> Misc Management

#Include<misc.au3>
While _IsPressed(52);; R key
    ;; Do something here
WEnd

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Thank you for the detailed reply once again.

Couldn't believe the If Then was such a simple solution heh.

I'm experiencing an issue however, where when I hit "Run" in the editor (only way I know how to debug :)) I get "==> Missing separator character after keyword.:"

The line it is giving the error about is scanning for color, i.e.

$col = PixelSearch(100,100,100,100,FFFFFF,0) ;Scanning the pixel at 100,100 for pure white

Share this post


Link to post
Share on other sites

$col = PixelSearch(100,100,100,100,0xFFFFFF,0) ;Scanning the pixel at 100,100 for pure white


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Thanks! I thought it automatically used Hex :).

Also, to idle a program so that it waits for, say, a key to be pressed, would one just use this?

While 1=1
Sleep(100000) ;Or some other large number to save memory
WEnd

Share this post


Link to post
Share on other sites

Sleep(1000) is usually more than adequate.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Thanks for all the answers, :).

One last question: Is there a way to have a hotkey turn a function on and off?

Share this post


Link to post
Share on other sites

as in call a function?

HotKeySet("Key", "Function")

Also it's best to unset the HotKey when you enter the function and then reset it if you have to.

HotKeySet("R", "_MyFunction")

Func _MyFunction()
    HotKeySet("R")
    While _IsPressed(52) ;; Now we can use our "R" key again
        Send("The R key is being pressed")
    WEnd
    HotKeySet("R", "_MyFunction");  Use the "R" as a hotkey again
EndFunc

All of this is in the help file and perhaps it's time for you to spend a bit of time looking through that. Documentation is a major plus for AutoIt over other languages.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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
Sign in to follow this  
Followers 0