Beginner Lesson 1 - Creating Your First Script
In this lesson, we will be learning the syntax of AutoIt and creating our first script. AutoIt is actually very similar to VB.NET, but it doesn't use dot notations, i.e, 'file.exists' would be 'fileexists'. AutoIt is based around user-like actions, such as, but not limited to, mouse clicks, audio playing window manipulation and much more, it is actually very powerful.
To do this in other languages would require lots of code and would most likely over complicate things. AutoIt does a great job of simplifying things, so you can quickly make scripts instead of spending weeks on them.
The Syntax
In AutoIt, a lot of the time you will want your script to remain alive. You can do this like so:
To do this in other languages would require lots of code and would most likely over complicate things. AutoIt does a great job of simplifying things, so you can quickly make scripts instead of spending weeks on them.
The Syntax
In AutoIt, a lot of the time you will want your script to remain alive. You can do this like so:
What we are doing here is checking While True Is True, which it must be and then sleeping the script for 10ms. Then we close the While with 'WEnd'. Because the 'True' never changes, this will always run. You can also use the number 1 instead of True, as 0 = False and 1 = True in programming.
I use this in every script I have ever wrote, because I don't want it to close after doing the script. I want it to be remain active and 'listening' for my hotkey presses.
The second thing you would likely want to do, is create some 'HotKeySets', Which is when a key has been pressed, you can tell the script to run a custom function, (that you must also create).
In AutoIt CTRL, ALT and SHIFT aren't used in there 'name form'. but rather as their special characters. For example...
CTRL = ^
ALT = !
SHIFT = +
it you would like your script to be activated by just one key such as the letter a, you would do this:
I use this in every script I have ever wrote, because I don't want it to close after doing the script. I want it to be remain active and 'listening' for my hotkey presses.
The second thing you would likely want to do, is create some 'HotKeySets', Which is when a key has been pressed, you can tell the script to run a custom function, (that you must also create).
In AutoIt CTRL, ALT and SHIFT aren't used in there 'name form'. but rather as their special characters. For example...
CTRL = ^
ALT = !
SHIFT = +
it you would like your script to be activated by just one key such as the letter a, you would do this:
But if you would like to have it so you are press more than one, such as CTRL+F1 you would do this:
This applies to if you have more than even two keys as well. If you wanted to have another key like ALT you would type that as well like so:
In my scripts, I use a ini file and pass the user set key as the key instead. Rather than putting the actual key I want to test, I put the variable name that was read from the ini file.
In AutoIt, to declare a variable you should use the Global or Local before the variable name, but it still works without it. You must however use the dollar sign when declaring the variable and whenever you reference the variable, for example:
In AutoIt, to declare a variable you should use the Global or Local before the variable name, but it still works without it. You must however use the dollar sign when declaring the variable and whenever you reference the variable, for example:
You can see here that I used Global $VaribleName and initialised it's value of string "^!f1", then passed the variable name into the hot key set instead.
You can to use Global outside of the functions and when you think you will be using that variable in other functions. Local is used inside of functions, like a throwaway variable that is used to get something, but itself is not required for anything else or anything that's needed to be accessed by other functions.
He is a practical example of this by checking if a file exists:
You can to use Global outside of the functions and when you think you will be using that variable in other functions. Local is used inside of functions, like a throwaway variable that is used to get something, but itself is not required for anything else or anything that's needed to be accessed by other functions.
He is a practical example of this by checking if a file exists:
Now let's make sure we understand at least some of these operators:
https://www.autoitscript.com/autoit3/docs/intro/lang_operators.htm
https://www.autoitscript.com/autoit3/docs/intro/lang_operators.htm
If you have understood most of those operators, we can continue on to making a basic script that will do a small sequence of tasks.
In this demonstration I want to open Notepad and make it type something. There is far better and more reliable ways to do this, such as shellexecute and winactivate, but for demo purposes, I would like to show you through mouse moves.
We will want it so we have toggle key, so that we can stop it from interfering with our normal computing routine when we don't want to use the script, but also have it listening for when we do want to use it.
We can achieve this by making a Global variable and setting it to false (this will be our boolean):
In this demonstration I want to open Notepad and make it type something. There is far better and more reliable ways to do this, such as shellexecute and winactivate, but for demo purposes, I would like to show you through mouse moves.
We will want it so we have toggle key, so that we can stop it from interfering with our normal computing routine when we don't want to use the script, but also have it listening for when we do want to use it.
We can achieve this by making a Global variable and setting it to false (this will be our boolean):
Next, we will create our HotKeySets like so:
If you want to use actual buttons you must enclose them inside braces, if you don't, the script will think you mean the letter F and the number 1 instead. Remember, you must have the modifier, in my case the ^ outside of the braces, but inside the quotations.
Now we can make our functions. I will make the toggle function first.
Now we can make our functions. I will make the toggle function first.
In this function I set our Global boolean to NOT it's current value, which means I set it to the opposite of it current value. This way, every time we press that key it will set the value of the boolean the opposite of what it was. You cannot use $bOn = true as once it is true, it is true, it won't make it false when you press it again.
Then we do a check with an if statement whether $bOn is true, when checking if true, you just do "if $bOn", you don't need to specify if it is true as that is what it assumes you are checking for by default.
If checking if it is false, you would check like this: "if not $bOn".
So now you use a Message Box to tell you whether the boolean is true or false:
MsgBox(0, "MsgBox_Title", "MsgBox_Body_Text")
Then we use and 'Else' to Message Box that it is false if it isn't true. Close the if with EndIf and close the function with EndFunc.
Now we can create the actual function that will do the mouse moves and typing.
Then we do a check with an if statement whether $bOn is true, when checking if true, you just do "if $bOn", you don't need to specify if it is true as that is what it assumes you are checking for by default.
If checking if it is false, you would check like this: "if not $bOn".
So now you use a Message Box to tell you whether the boolean is true or false:
MsgBox(0, "MsgBox_Title", "MsgBox_Body_Text")
Then we use and 'Else' to Message Box that it is false if it isn't true. Close the if with EndIf and close the function with EndFunc.
Now we can create the actual function that will do the mouse moves and typing.
First we check if $bOn and if it is, we will do the following...
You can use the AU3Info on the Tools menu to get the mouse position you need. Head over to the 'Mouse' tab and use the finder tool by dragging it and dropping it on the element you want the mouse to click on to in our script. It will display to mouse position in the control below. Try to remember that number and type it into the 'MouseClick' Parameters. You can freeze it by going to Options > Freeze, if you aren't good at remembering numbers.
You can use the AU3Info on the Tools menu to get the mouse position you need. Head over to the 'Mouse' tab and use the finder tool by dragging it and dropping it on the element you want the mouse to click on to in our script. It will display to mouse position in the control below. Try to remember that number and type it into the 'MouseClick' Parameters. You can freeze it by going to Options > Freeze, if you aren't good at remembering numbers.
In my case I read my X position for my mouse to be 132 and my Y position to be 787, I would type in like so...
Everything I do something I choose to sleep the script for one second (1000ms) just incase things may not have loaded quick enough.
When I send the text I also concatenate (add on) this & @CRLF, which is basically and newline and means "Carriage Return Line Feed"
I do the same thing for all of the other actions and then close the if that checks the boolean state and then close the function.
I also made an option to delay the send keys by 50ms so it looks more like human typing, by default it is set to 5ms which is very quick typing. Most languages will send without any delay, which makes it look like copy and paste.
Here is a full list of Options AutoIt has to offer:
http://www.autoitscript.com/autoit3/docs/functions/AutoItSetOption.htm
To make the option I put this at the top of script so that it is processed when the script first starts:
When I send the text I also concatenate (add on) this & @CRLF, which is basically and newline and means "Carriage Return Line Feed"
I do the same thing for all of the other actions and then close the if that checks the boolean state and then close the function.
I also made an option to delay the send keys by 50ms so it looks more like human typing, by default it is set to 5ms which is very quick typing. Most languages will send without any delay, which makes it look like copy and paste.
Here is a full list of Options AutoIt has to offer:
http://www.autoitscript.com/autoit3/docs/functions/AutoItSetOption.htm
To make the option I put this at the top of script so that it is processed when the script first starts:
To return it to normal you would just do that again but make it 5ms instead of 50ms.
Now all we have to is add our bit of code that will keep the script alive.
Now all we have to is add our bit of code that will keep the script alive.
So everything is done, this is what it should look like
Now this is the fun part, let's check if it works!
To run the script go to Tools > Go (or press F5).
You can also add a function to exit the script. Just make a HotKeySet with your key choice, and give it the function to run, create that function and use the single word "exit" to kill the script. The function should look like this:
To run the script go to Tools > Go (or press F5).
You can also add a function to exit the script. Just make a HotKeySet with your key choice, and give it the function to run, create that function and use the single word "exit" to kill the script. The function should look like this:
Well done, you have completed your first AutoIt coding tutorial with me. If you would like to learn more, proceed to my next tutorial here *WRITING TUTORIAL NOW*
Download The Tutorial Script
Download The Tutorial Script
Author: Riccardo Geraci