Sign in to follow this  
Followers 0
geekycoder

Is there a main function for AutoIt script

4 posts in this topic

Is there a main function or equivalent for AutoIt just like Python and Java to make script testing convenient ?

def main():

# the main code goes here

if __name__ == "__main__":

main()

I will like the script to run the main func when the script is executed, and ignore the main when run as library. Currently the "main" function has to be commented and uncommented to be tested.

thx

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Not formally, no. The script itself is the scope of execution, e.g. AutoIt will execute the script from top to bottom. You encapsulate functions using Func / EndFunc, and you can create a "main" loop by using the control keywords like While/Wend or For/Next.

Generally speaking, most people put their code between While/Wend, like this:

$someVariable = "blah blah"
Global $SomeGlobalVariable = "blah"
$main = 10
$i = 0

While $main > 0
$i = $i + 1
MsgBox(0, "This is your main loop", "We will execute " & 10-$i & " more times before exiting the main loop")
$main -= 1
Wend

Func SomeFunc($parameterYay)
;commented line
;this function never gets called, but it's here to demonstrate scope
MsgBox(0, "This is Somefunc Speaking", "This is outside the main loop")
EndFunc

That's a pretty basic outline for how a lot of scripts are written. Some people are more formal, others are much less, and the different flow control keywords have different advantages depending on what you're doing. Check out the help file and the stickies on this forum, it will help you get a good grasp on the language much faster.

Edited by JRowe

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

You can use the @ScriptName macro to see which script is currently being executed.

Edit: Clarification. Your library would look like this:

If @ScriptName = "ThisLibName.au3" Then _LibTestMain()

Func _LibFunc1()
EndFunc

Func _LibFunc2()
EndFunc

Func _LibTestMain()
    ; Test this library
    _LibFunc1()
    _LibFunc2()
EndFunc

Main script:

#include "ThisLibName.au3"

; Dowhatever
Edited by Manadar

Share this post


Link to post
Share on other sites

Thank for the suggestions.

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