Up And Running With AutoHotKey - Automate Software in 2022 | Faruq Fayaz | Skillshare

Playback Speed


  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Up And Running With AutoHotKey - Automate Software in 2022

teacher avatar Faruq Fayaz

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

16 Lessons (1h 37m)
    • 1. Introduction

      1:30
    • 2. What is AutoHotKey?

      2:00
    • 3. Your First AutoHotKey Script

      4:56
    • 4. AutoHotKey Resources

      1:39
    • 5. Window Spy

      2:57
    • 6. Introducing Visual Studio Code

      7:07
    • 7. Course Project Overview

      4:08
    • 8. Setting up the Google form for the course project

      3:29
    • 9. Filling the form once

      16:31
    • 10. Using functions to write less code

      4:47
    • 11. Loops

      3:14
    • 12. Variables

      13:10
    • 13. Arrays

      8:39
    • 14. Using arrays to fill a form multiple times

      4:14
    • 15. Bonus lesson - Formatting data

      6:28
    • 16. Bonus lesson - Page wait

      11:48
  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

60

Students

--

Projects

About This Class

Learn AutoHotKey the easy way. If you've ever wanted to automate a simple or complex task on a Windows computer in this course you'll learn how to do so.

If you've ever had to do the same thing repetitively on a computer whether it's filling a form, or using the same commands in your favorite software AutoHotKey can save your time and spare you from the boredom of the most tedious parts of a job.

In this lesson we'll use entirely free open source software tools to accomplish your goal of automating Windows programs. I'll take you through installation, common commands and then writing a simple but sophisticated program to give you the knowledge you need to write your own code.Ā 

To complete this course you won't need any programming experience orĀ purchase any software, as long as you have a Windows computer and enthusiasm you can be successful with AutoHotkey.

Skills you'll learn:

  • Automate Form Filling
  • Avoid tedious routine tasks
  • Make your own keyboard shortcuts
  • Work many times faster
  • Programming from the ground up

Meet Your Teacher

Teacher Profile Image

Faruq Fayaz

Teacher

Class Ratings

Expectations Met?
  • Exceeded!
    0%
  • Yes
    0%
  • Somewhat
    0%
  • Not really
    0%
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. Introduction: Hi, I'm fruit fly out and I'm going to get you up and running. If auto hockey, Why auto Hockey? Well, if you've ever done any repetitive task using a Windows computer, maybe it's copying and pasting from one program to another, perhaps keystrokes, you always have to press or routine commands you're running in your favorite application. Also, hotkeys are free and open source solution that could well and your misery. You might be familiar with some automation for recording a macro in specific software. But because alter hockey runs across different applications is far more useful. Also, it's a simple scripting language that you can get up and running in about an hour without any programming experience. It won't take you forever to learn and use. And after that, we'll be able to write useful code in minutes. Over language is a great place where you can write something that's useful, that's not going to take you hours or days to write. In fact, auto hockey is a great introduction to programming as a whole. You don't need any prerequisites in terms of programming skill or requires specialist knowledge, if you've got a Windows computer and diffuse out and you'll be able to use what they've hockey. In this course. I won't go through all the common commands that you could use to automate a whole series of tasks. So join me to get up and running with auto hockey. 2. What is AutoHotKey?: Okay, so what is auto hockey? Easiest way to find out is from their homepage. So if you Google HK, Well, for auto hockey, of course. And if you click on the top link, also hockey, it'll take you to the homepage you can download here with this link. Well, before we do that, the page down, it tells you on the homepage page exactly what it is. Water hockey is a free open source. So that's good scripting language for Windows that allows users to easily create small to complex scripts. All kinds of tasks, such as form fillers, auto clicking, macros, et cetera. Scroll down a little bit more dots, some code snippets. So as a little preview of the top of it, we will be writing, let me scared by all the enigmatic words here. Essentially, if you were to copy and paste this into notepad savings account with an extension dot HK wants auto hockey is installed. You could double-click that fall and he would play script. So it's, it's fairly simple rather. So we'll be doing that anyway shortly. So we can go ahead and download it by clicking the Download button. And they can't download core version. Once you say that to your hard drive, will go ahead and install it. To install auto. Okay. Wherever you saved it on your hard drive, double-click the water hockey shop file. For some of you, it will ask you for permissions. Just click yes. And then you click on Express Installation. And that's it. Installation can play here, installed. What a hockey. 3. Your First AutoHotKey Script: But to test our auto hockey installation, let's run a quick script. So to do that, and we'll use Notepad. Open up your notepad and write on commands thrown. Open up Notepad, and then we'll send some text and light paths extend. Hello, World. We should mark after save that file, save it anyway you like. And the file hallway world. And it must have the extinction though, a HK, HK, they've that. If you double-click it to open Notepad or it hasn't sent the text Hello World. And what's happened? The reason that's happened is often deliberately to show you something that happens quite regularly. And that's because the script has run faster than Windows can respond. So in this case, a Windows application to respond this notepad and it's run, the command will Notepad. And then it's immediately afterwards, send Hello World text to the application. Or there's been a delay to the fraction of a second when I open notepad. So it's run this script before notepad stipend. So it's an easy way to remedy that. And that's just the poor in the delay by using the command sleep, sleep. And 500 milliseconds, which is 2.5th. If we save that. Even at this old window, double-click Hello World in. And he opens up with helloworld back the exclamation marks missing. So Boise, usually we use curly braces to send special characters. Literally, an exclamation marks are special character. What do I mean by special characters? And special characters have reserved. And the distinct meaning in O2 hockey, not meaning we normally use in English language. So we get that and Brown that use curly braces. Phase that. Then when the script again. Now you've got the exclamation mark. So let's try. Another common and ubiquitous use case of auto hockey is the actual alt key, which also hockey is named after. So in order to do that, you will have a keyboard shortcut. In this case, we'll assign a carrot k, which is this color hat emojis. In English keyboards or UK King keyboards, it's above the six. And so that stands for control, a particular symbol, control key on your keyboard. And we'll use as its control. It will shrink them. And if you put double colon, and then it has to do something which we'll say is a proper message box windowed and send the text Hello world again. This DOM, we don't need the curly braces because it's hockey windows, so we don't need to lock a special character. If we save that and run it. If it happens until we press control G. And they've got Hello World. Now another thing, it's worth knowing when you've got whore to hotkey script cooling will appear in your system tray as a green icon. And you can right-click it and exited. That way if there's any thought confusion about what scripts you got running, and always start with a clean slate, I believe in the system tray. In this lesson, we've run our first HK script. We've made the script run Notepad and send some text to it. We fix a script that Rosalie fast by using the commonly used sleep command to make the script running speed we want. We've also used a hockey command that we can use to create keyboard shortcuts. And the moment we've done it in the simplest way possible by using Notepad as our code editor. This was deliberate just to show you how simple you can go to make things work. But I know lots of you will be thinking that isn't too slick. So later on we'll be using the free VS Code Editor to write scripts more quickly with the added bonus of looking far more attractive. 4. AutoHotKey Resources: Occasionally when you're using auto hockey is Tom's. You'll want to find out more about specific command or how to script something you've got in mind. Though in this quick tutorial, I'm gonna show you some of the best places to go. One of the first places you can go is the HK documentation. And you can search a specific commands that say message box and the documentation or give you a description of the command. And most importantly at the bottom, will give you some examples about how to use that. So that's really useful if you want to find out more about all the commands, it's gone all, okay. So more look intimidating, but you won't be using all the commands in a single project. Since you're working on specific problem area, you will most likely use a few commands several times and you'll become familiar with those quickly. So alongside the documentation on the auto hotkey website, you've got forums. So here you can search for answers for any questions you've got, but you can also post problems. So stack overflow is always a good resource to find answers to questions. So if you look at the alter your hockey forum, you'll see whole well-formatted solutions to problems. If you just take one example. So most parts of the question, and immediately you got, you can see a tick. We're tenant quotes indicating that that's quite a good solution to this problem. 5. Window Spy: In the first group rewrote in notepad is helloworld to HK file. We wrote a script which I pin Notepad. I'm sending the text Hello World to it. By double clicking it. It prints out the text Hello world. But in the actual foil, we open a file to meditate with Notepad. You can say that we've got the RUN command, and that's a fairly common commands. So you could easily discover that using the HK, HK documentation. Put notepad dot EXE is the application opens. How would you know what the key word is to open your perhaps obscure application that you're using in order to instruct that, to do certain commands if you would like to automate. So to phone those keywords to use in our script, we use a program called windows by tau h k. So if we open a file explorer and look in the folder which water hockey was actually installed in. And it's usually in C, program files. Auto hockey, you'll see a foil, call Windows, boy, do I hate k? If you double-click on that, will bring up this GUI graphical user interface that was written in auto hockey that shows you all sorts of useful information that we can use to write scripts. Now in this section, this information, let me know how to target a particular. And in this section, it tells you mass position, which is useful when you're writing a script. And you want the mouse to target a particular location on your screen and click on it. So mostly it's these two sections here that you'll be using. When you hover over or click on, sorry, a particular application, you'll see the information in Windows pi change. So it tells you here the mic. So I clicked on the file explorer that that process is called explorer dot EXE. And it's go over properties of the window. So window tile, class and process, which will be in mostly for our notepad. Now, you'll see that the process is called notepad dot EXE. And that corresponds to what we use in the script. If I dot EXE. 6. Introducing Visual Studio Code: So up until now we've been using Notepad to write your code. It works, but it doesn't help us too much. I mean, it doesn't Hall our auto hockey keywords. Make suggestions on how to use HK code. And that'll make our coding much quicker. So in this lesson, I'm going to introduce the Code Editor VS code. There's loads more code editors available of use sublime text in the past to rot a hate K script and that works well too. I believe historically saw T4 auto hockey has been popular in HK community, but feel free to go with whatever you're comfortable with. In these lessons, I'll be using VS Code pixels Frey and very quick to get up and running. It's also got good pedigree. If we jump over to Wikipedia, we see it's made by Microsoft and it's got a strong IP source dimension to it. It's very popular amongst developers with a recent survey ranking and most popular developer tool with about 50% of 90 thousand respondents claiming to use it. So if we Google Visual Studio code, usually the top link is the one we're after. So I click on that. Nothing. She said the homepage, that's code to Visual Studio.com. And from there you can click on download for Windows and say VT harddrive. Once it's downloaded, you can go ahead and install it. Accept the defaults here. Installing in the default location, desktop icon and lead they hovered Default Options. And click on install. Now you can launch VS Code. For European VS code, it can look quite intimidating. It's quite a departure from an iPad. I just remember all its menus and panels are there to help you. Unfortunately, it's quite easy to learn as you go along. So you could do pro in. And if you click Next and welcome screen and fighting close that and double-click anywhere in here. And we'll create a new file. So I'm going to write some h k carrier you're familiar with. I'm gonna zoom in a little bit so you can see it there. So incidentally that's control plus nauseam in and if you want to change back to the normal zeros. So always depressed control 0 on the known part. Like I'm upset, say that's always on ROI, some code you're familiar with. Message box there. Now. And then I went a fin Tyvek sign and we call psi. If I say, let's save that as VS code. Test. Dot and call it anything you want. Of course, we must have the a h k extension. So still not a lot happening. But if we go over to extensions, which is this icon over here, which we'll talk a lot kind of Tetris symbol and click on it. And then search a high h k. I'll bring up all auto hotkey related extensions. Now, what we want is this one boy equation. So if we click on that and install it, it says this is tension. This extension is neighbor globally, which is what we want. Close it. And now the keywords been wholehearted. Code highlighting, enabled LCD. Is there an hover over a keyword? It will give you a description of that command and some of the options available. And I for just comment that out doesn't know the thing, that courage is really good at it. You can press control for slush. And it will commented out. So what's, it's inserted the semicolon, which means that this line of code where I'm ruin. And if we were to plot the command again, IntelliSense is triggered. So that means it will give you a code suggestion depending on the first few layers that you've read. The first one with this ABC on it is actually the generic VS code code suggestion and not the one from the extension. We want to ignore that. So if you press the down arrow, press tab and I'll select the second one, which is one we won't message box. And then it will also give you the options for that command. And different color for each of the options. Most of them are in light blue. Bold text is in a darker blue, and darker blue indicates that it's an essential parameter. Options is a number and alpha, nucleus picks up look to the HK documentation, which we've looked at before. So that gives you a really straightforward description of how a US message box and all the parameters and what they do. So options is a number one to six, which dictates how many balloons the message box we'll have. So I'm just going to go for one. Then total is a name of the message box I'm going to go for. This is my message box. I notice here that that is not quite perfect. Recognize these as keyword words. Even though in this sense we're using a, just as a normal string, text is not actually uses a command. So this is Corey and A's, which are clearly keywords. But we can ignore that. It's still really useful. So if we press tab, it's gonna move to the next command. The next option, which is the critical option text. And we'll pull this is my message text. And time out is the amount of time the message box appears for on-off 17. Say. So if I press Save now. And we kept the wrongness script from by right-clicking and selecting Roy Rowan HK script. And now the message box appears here. And you can see that these all work so worn has now given this option, one is now given as more than just the okay, that's giving us a cancel. This is my message box is a toy model. This is my message text, which is here. And then after 17 seconds is just disappeared. 7. Course Project Overview: So now we've got everything in place to write useful auto hockey scripts. We've installed also hockey and configured VS character artist scripts sufficiently. In this section, we'll introduce a practical project that shows how effective auto hockey is. The scenario for the project is that we work for an organization that has training classes for its workforce. And the training attendance record for each class has ended up as a simple text file. Unfortunately, Theory and the cluster applies. Those running the classes didn't have the time to put the attendance data in the official online record. So now you as the employee, have ended up with the exciting job of entering hundreds of records into the system. To give you an idea of what we're looking to achieve, i'll show you the final auto hockey script running. So this is our final script. And as you can see, if I scroll down so any around 70 lines. And in fact, if I was to get rid of the actual training records, the actual code which does anything is around 50 loans. So it's not much code at that, and it's quite simple. So it's not difficult today. These are the training records that we've been given. Boy, our imaginary boss. This had been reformatted because the boss is given as in a simple text format like I said. And it's tab separated. It could be any source separation, could be Space separate. It just made up it reformatted for autarky slightly differently. Attorney tab separator is just for convenience. Now, this textFile works is each line represents a separate tank training recode. So in this first line, jason camp completed his IT skills on the 30th of November 2019 and I've also given a given him an employee number. And then the next line is a new record, a different person. So the script will go through each of these track record training records lumbar loin, and insert it into the form which is here. And this is produced in Google Sheets are can replicate it easily, but it could be anywhere software and it doesn't even have to be browser-based. It could be a desktop application, bottled in Google sheets. And manually. You choose the class, you choose a date. And he points most Name. Let's first 19, sorry. And employee number, random number there. And then we've submitted the response. So that form submission ends up in a Google spreadsheet that is linked to the form. And I'll show you how to cry. That is steady, easy. And of course it's not part of the auto hockey core skills. It's part of the Google Doc, sweet. And it's just for demonstration. So now we know how the script supposed to work. Let's try running it. So if I right-click and before right-click and start, it will do is it will just a free, free records because kind of boring watching this loop over all these records. So right-click and run the script. You should say carrying three, adding all the training records. And you go, it's complete, dies free. And if we look in the spreadsheet, you can say though is free records have been entered correctly. We turned milky In the last record, and the third record is Tennessee milky. 8. Setting up the Google form for the course project: So to create a form or use Google Drive is free to sign up for, and you can create a form via Google Drive, insights or belief, the Google Docs interface. But omega4 drives I or you do. You click on new and more creative forms and go to platform. Once you there, we're going to recreate the form that we saw in the training register. Next one is class attended. Copy and paste this just to make it less boring, it's very easy and intuitive. So I'll copy and paste this to make it quicker. And probably speeding up as well to make it less tedious for you. But I will show you any hard bits and the types of dropdowns. So this is a drop-down. Top of question is for safety awareness is first-aid in the workplace. Hobson freeze IT skills. And that's that London. Then we've got date. And that's fine as is. Firstname seems to automatically recognize a topic question. So you see what my create a Show Answer. Fail for that. So let's create the show answer. That's really clever. Google Drive. And then employee name, employee number, and show answer so that we can preview the form using this little icon here. And we get the form previewed, opened up in a new tab. And before we submit anything, we probably want to get back to the All Tab and save the response is somewhere. So if you click on these three dots, you can select a response destination and increases when previously, but we can put in a new place to store the records training accords to recreate. So now we've created our, our form and we've created the place where the form is going to be saved me, it's a Google sheets. If you haven't given your former name in click up here and give it a name. Once you've done that, you go to Google Drive. You'll say. But if the form and the training record store spreadsheet in your Google Drive. Now, we were actually in the offering environment for the form. What we want is the actual form to where we are h k script. If we go to the training wreck, coastal, or shape or form and go to law reform. You'll go to the form. Now. It's exactly the same as your form is just not got the offering tools. So V, using this, I'd actually put on as before, we will be referring back to it quite a lot. 9. Filling the form once: So we're back in the BS code and we're going to create a new file by double clicking here. Or we can go to File New. And we'll save this as a HK file, File Save As. And we'll call it data input. Call it anything a Loic. Remember the i h, k extension. It will my click create some nasty extension, creating this, giving you this code here, which is all good practice. And we're gonna wanna delete up. And for our own lock standard coding, what we're gonna do is paste in some code. And I'll explain this code. This is a best practice for this code you tend to use on pretty much most of your scripts. So this has shown stands for directive, and that means it's processed before the script is storage room affects the behavior of the script as a whole, then our directive will stop empty variables being searched. And it's something that's considered good practice and on pretty compatibility with future age cave releases basically is a performance thing. It'll just run faster. So this next one, single instance force prevention running multiple instances of the script. This next one warn will give you better debugging. So if your code goes wrong, it's easier to find out where silica said, don't worry about these lines of code is just good practice. And you could well use them all the time so you can copy and paste them in most of your scripts. I'll provide them alongside all the code for the course in the download section. So to target online form continues the command when activate. And it'll give us always optional parameters to help us identify Amman throne, which is in the browser. So how do we identify the browser, if you recall, we used when spy to help us identify different programs are running in Windows. So if we go into Windows Explorer and go to C drive, if you remember, when Spy comes bundled with the auto hockey installation and there is the longer entity to make it more convenient. Now suggest you do the same is making sure your code is going to say you can't create a short coherence. Guess this is where the programs are doing a shortcut to be placed on the desktop. Instead. Click Yes. And when we go to the desktop, all the way, when there is m is the shortcut for that. So if we double-click Windows boy and bring compile window spy, and this will tell us information about Chrome window. And most importantly, the whole identifying features which are here. Now, usually the mouse positions or moving. And in this case we are not concerned with that. If you wanted to suspend the updates so you can copy them, you can press control as it says down here. And it says benzene. But in our case we want the and identifying feature, which is we're going to use. The process HK on school xy crime. If you right-click that, copy it and paste it in our code. So now if we save the code or Control S and run the script, it brings up current. So if you go back to our code reviews, we inactivate because we've assumed that Chrome is open, which works well for this particular task, but you could run crime from scratch. Or here we're assuming that crime is already I punish just minimized and we're activating it. So now we're going to target the form itself. So moment do I know that everyone's got different screen sizes? I want to actually zoom in on this, are pressing Control plus. So recall a scroll bar. Let's pretend the form was Other Button bomb for some reason. And we wanna get back to the top. So to get back to the top of form, and we want to get back to the top form, just begin to fill the four main. We use the send import. So that will send this to the top to form ball. Let's give the browser a little bit of a chance to open up. So it will give it a sleep of say, 500 milliseconds saved up. If we push press room, goes back to the top of the form. So you may have noticed I'm using the send input command. And previously when we were testing out windows, sorry, our auto hockey installation, we just use the send command. And that does the same thing. Now the reason I'm using the send input is because it's best practice. It runs a little bit faster. So we install using sand and port now. So the next thing today is, if you go back to the form, is well, first of all, on this Mikey normal size or leads to, let's make MLS. Alright, so you can see it is to target. This dropped down to how do we do that? So we go to our friend. When they spoiled. We go bring desktop pool and double-click on Windows boy, that is on top. And if we hover over that, we can see the mouse position. And if you can see here is these different types. So this is position relative to different origins. So the screen is, the top left of the screen. Window is this particular window. So if I make that smaller, you'll notice that the window position on its corner is near exactly 0. Boy is nearly 0 in the window and the screen Coat tours. There is so much theory there on the screen. The one we're going to be using is the Screen1. Let's open up again. So the coordinates for the mouse position. If we, if we hover over the drop-down, if I'm gonna make this standard size. And the reason for that, if we have the same size window over time, this mouse position will change over time. So I'm going to keep it at the standard texts noise. And now hovering over that position, press control that will suspend the coordinates and we can copy the screen coordinates for the mouse position. So back in VS Code, we can use that coordinates. But first we're going to use coupled mode, which is a command HK No, what screen coordinates using explicitly? Say mouse. And we're gonna say screen. And then we'll use mouse click to let h k. Now, what are you going to mouse-click? Left mouse point. And in those coordinates. Let's see if that works. And it does clicking that. If you've got two screens, I have occasionally had a problem getting the correct mass coordinates from Windows. Boy, if you having this problem, the thing to do is run your target application in left-most screen when you're actually determining mouse position in the first place. And then afterwards when you're running the code. So in AXA is without firm running lean the left-most screen when we're actually using Windows boy, to retrieve disposition. And this usually resolves the problem. So sometimes you can get away with using this cover wide code node. And it will work accurately where it's a mouse click is good participate in. Also is good practice to pull in a casual comment. So that's a semi-colon space. And just a little description of what we're doing. Next. Next, if we go to the online form, you want to choose one of these. I forget about the code. We can sleep. For 500 would give you a chance to clicking that drop down. And then you can send in, pull IT, skills, save it, and run it. And it doesn't work. Is because we go back to the script. That's because we're using send input. And if we use just a simple send, that should now work. So I'm not exactly sure why that's the case. But it's probably connected to the way it's, those commands are sending the actual key strikes. In any case. We've got it working with the same command. So now we want to target this date field. So we want to sleep for an interval of 500 and then send tab. And then we'll send him for 10102020. And see how that works. So right click Run. And it works. So now just code to get down the form. Let's sleep again. And it's good, it's good idea to draw the carried out just to develop that muscle memory and send a name to the name field. It's in the tab K. Let's just test it's working. And it's not because I forgot to send the conclude that tab down there. We will now works for it. I have noticed I've not put asleep command between sending more firstName and sending a second name. That's because TAP works quite quickly. Shouldn't need it. If your computer is running a little bit slower, you need it. So just be aware that you might need a sleep ment. Also, whilst we're on the subject of individual computers, you'll obviously your mouse-click coordinates will be different. So you can get there is from winds boy, windows, Polly, sorry. Now going down to copy and paste it a little bit, enumerated voice of muscle memory and put in the employee number. And then once again, to get down to the submit, this time, click Enter. Save that. And let's see if we can actually run the whole script. So it runs reading TOEFL and it's amid. So that's worth that's great. So you successfully fill the form in and submit it. But let's get back to where we started, which was the fresh form. Just to finish it off, give us a complete cycle. So let's sleep. Before what occurred, let's look at the Submit and our response would be to get the coordinates of that mass coordinates. But another way is if you press Tab, you'll notice that the link is in focus. And if you press Enter, it'll take us it any form. So let's draw that in the code. So if you sleep, maybe seconds, that will give the form a chance to submit. Sending pull, tab, enter, and then slake another 2 thousand milliseconds just to get the just to give the form a chance to load. And the formula is. So now if we run the code, save it first. Chi works and the forms I did, that's great. Probably will have a lesson summary for this one. It's kind of a critical lesson. In summary, while filling some fields with data, we've done the essential task of targeting and application and interacting with it. In a way, it's a feasibility study for the entire idea or project you've got in mind. Essentially, if you can get auto hockey to do it once, you can probably automate the task entirely and repay any procedure many times from experience, if you're having difficulty filling Garfield, we've data is often because your scripts are running too fast. So try putting in sleep commands. We various did items around the sending or send commands. 10. Using functions to write less code: So far we've successfully go ask grip to fill the form in once. But of course, our problem is that we've got fill the form in dozens and dozens of times. Ib understands maybe even thousands of times. So we're going to introduce something called a function. And I go over to this test. Test may have carried. So if so, let's demonstrate how to use a function. So all the function is, is a block of code that you can call to ruin multiple times. So if I write my function, and that's the name of the block of code to run Paulson parentheses as some curly brackets. Unsigned message box there. So that's a function. Now if I run this, run it, nothing happens. Because even though we've got a function with this encoding, it is not being called upon. So in order to run the function, we just wrote, my function with some parentheses indicate that the function should be executed. So now if we run, it, says hello there. So this command here has called upon the function, the function declaration, which is kept here. And this is the actual code that runs inside the function. So now she wants to call, call this carried this block of code again. We could just write phi function. Again, save it. And now if we run it, says hello there. I click. Okay, says hello there again. Now with this example, there's only one line of code. That can be multiple lines of code. And that's what we've got in our case. So the ability to just write the code once and just call upon it. We will single line sizes, having to duplicate the code many, many times. So that's trying to implement a function in our existing code. So if we start the code and Freud single record field has the name of the function, right? For essays, I'll put the code inside the function. In these curly braces. End that block of code. And of a car, the price. Just to make it look neater, I'm going to tab all the code inside. So it just shows that everything inside that's indented inside that function. That's easy to say. Let's see if this function works by calling it. They call it, you just put the name of the function with parentheses around it. And let's try running it twice. Let's change the name. And let's say I change it to j. Logs. Save it actual earlier proposition. And let's run this code. So that's it. It's run the function toys. So hopefully, if we go to the Google Sheet which holds our submissions, will say it twice, and that's what's happened. Okay, we're well on our way to fitting the swarm out many, many times. So that's great. 11. Loops: So in the last lesson, we were introduced to the idea of functions, which is a fundamental programming concept where we can use that block of code and run it several times. So we don't have to write that block of code out dozens, dozens, and dozens of time. So we've incorporated the idea of a function in our filling of the form. So all the code that fills it out a single time, we've put in a function called single record field. So we can make our code more concise. Instead of duplicating this code within the function, we can just call single record fill several times. But we can go further. If you want us to fill the form in dozens and dozens of times, we could obviously duplicate this code. And at the moment we know is filling the forming with the same information, but we're going to come to that later. And we call it several times, but we can go further and we can use the loop command. And you use a command like this. So you wrote loot. And how many times you want to leap, let's say two. Okay? And you put the code that you want to leap round in curly braces. Indent, intend that, save it. So now let's use a different name. Let's call it fly. Side is that make sure we're in the rock paging. And we'll save that and run the script. And that's it's complete. If we look at the the, the form submissions, we say multimeric fly has been entered twice. So that's great. One further thing I wanna do with the code is allow us to exit their code. So this is an assault on the leaf. It's just too. If your code keeps on running and you want to quick way of getting out of it, I'm going to introduce a hockey. The allows us to escape. So I'm gonna use the caret character which is above the six, especially F6 and escape. And then double colon. And the command you want to run with that shortcut. That's the hockey to escape from our code if it running it and we want to stop it. And that's control. That carrot and escape say who's press Control and the Skype together, you'll exit the app. 12. Variables: So in the last lesson, we introduced the idea of a loop. And in this lesson we're going to bring in some variables which will allow us to fill the form in which different people. Before I begin, I just want to party the code or polytope. And this is personal preference reasonably Venus, because at the moment the script runs and immediately wounds is sleep and we're not controlling the execution, the leap, it just runs top to bottom. And the leaps executes a longer do. And then I put that loop in a function. So if you remember, we declared a function with an a and this name could be anything. We didn't call it multiple record flow. Because that's what this function does. We want to put this loop code in that on that. So the moment code doesn't do anything because we haven't cooled multiple record field. In order to close multiple record field, we just took the name of the function with the curly parenthesis. So just test this works. So EBIT and then run it. And it seems to work. Mosfet fly until now four times, three times we are additive in the previous lesson. So as it stands, the script's pretty good as submitting records via the online form. The problem is, of course, is sending the same information again and again. So what we need to do is change these fields, the class, the date, the firstname, surname, and employee number. So it can be different every time the script loops around. So in essence, we need to very diverse fields. And for that we need variables. So I'll jump over to our test HK script and I'll briefly explain variables. By is a common programming metaphor and that's one of the bucket. So a bouquet in real life performs the useful function of taking something from one location to another. But it doesn't have to be the same thing in that we can put whatever we want in the bucket. So the metaphor here is our variable name, is the bucket. If we brought a variable name, we'll call it. Okay. Can be called anything you want and give it a value of water. And then we can use a variable in message box to say, Okay. Is being carried into the garden and save it. Anyway. We've assigned for the string, the value string into the booking, which is the variable name. And what we're going to attempt to do is print out in a message box the water. So in a swap out, the book value is being carried into the garden. So let's try that. And what you get is the book is being carried into the garden. So it didn't exactly work. Environment is just a string. In order to refer to a variable in hockey, you need to use presented side around the variable. So now if we save, it, says the water is being carried into the garden. Thing we variables is we can pull whatever we want into them. So we can pull mode. So now if we run a script, there's a mode is being carried into the garden. Now, the variables are common to all programming languages, but the key things we hold a hockey is how you assign them. The syntax for assigning him, say use this assignment operator, which is a colon and an equal sign. And how this works is what's on the right of this assignment operator. Goes into the book, came to the valley water is assigned to advocate for being on the right hand side of this assignment operator. They will thing to remember is how to refer to those those variables. And that's what the percentage sign around the variable name to indicate that it's a string. Just be a text. We put quotation marks around water and what the string value. So let's see how we can use variables in our script. Now to begin with, I'm going to ignore all the variables are ignored. Date, surname, and employee number. Let's just focus on first_name. Just keep it easy to start with. So let say we create the variable firstname and we assign it the value of first name one. So that's the first record. In a second would record the value of first name, first name too, and that will be J. So that's how we assign a value to a variable name, can be called y V1. You can't use a number. Begin a variable name, but biologic and do not even call morphine-like. So I've arbitrarily cold and first_name, but if meaningful, because that's what it represents. So that's one variable, S t variables we've created. Let's first say commit. And sulfur will use light around. Let's create another variable and call it record number. And we'll keep that the value of record number. We want the record number to change every time it loops. The First Army carry deeps, he's got the value record number equals one. But when you've completed the labor, let's change the value. Or using an incremental operator plus equals and add one to it. So the Firstly will equal one when it reaches the end and we form would be added to next labour we free if they V4 recorded only 57 and so on. So I will create free variables, these first three variables and the moment that change value. This third variable record number that changes. So each time and loops each ITS value. So how do we refer to those? There's variables in our code site. So I focusing on first name. Let's pull in firstName here. First name on an app, we'll refer back to first-name, which is ferric. Or if you think about it, it's no good to us. Because the second time this loop is run, this block of carriers running using the loop, you'll still refer back to firstname one. What we really want is something like firstname one the first time he carries around, then firstName. So how would we create that? One way of doing that, we vote, we've got all the elements to create this. We can change this number one to using this record number. Variable changes with the loop number. What we want is something like first name joined to recommend that occurred. And the way you do that in the code is used this single percentage, so on. And what firstName and record number or that one it tells. It concatenates, which means join to give record number the variable which in the first loop is one. And a secondary piece to, to firstname, first_name just stays the same. Hasn't got a variable. It's just a string. So this percentage sign forces to an expression. That means is, is that I will consider this an expression in programming. And expression is something that evaluates. Value, and that value will be our variable name. So if we run this code now, we should get hurry can jive entered into the form. So let's try that. And we get an error saying this variable has not been assigned a value. Specifically record number a local variable. So if we pick control escape, which is our escape code, okay, back before that y. So what is essentially saying is that record number here is record night number variable is not available in this single record Phil function. And that's because of a thing called scope. So variables created in the scope of a function are only available in that function. So we have to pass it to an odd function. And this is a common technique which you can use, which is in a function declaration. Allow it to have a parameter which is record number. She's basically what this function is expecting in terms of a variable, IS expecting to be sent a variable which he previously didn't have. And we can send it record NBA. So for those of you bit more experienced programmers and probably thinking we could have set record numbers at relabel variable outside of it. But good functional programming practices is good. Not say we'll change the value of anything outside of a function. So we don't want to do that me and keep me good practice and pass it as an argument to the function. So essentially, just in summary, more just did that. This record number variable wasn't available in this function. So we have to pass it to it because a Skype of variables within a function are limited to that function unless you pass it explicitly, like. So now, we say that, thank showing the rotation, it's refresh this to get back to the store. And now if we run it, we should get two submissions. And it does. Let's have a look, seems to run free and entered into the full and ended up in the spreadsheet. So that's great. 13. Arrays: Now we could continue writing our script in the same fashion with the variables stuck to the top like this. But what we find is that it eventually looks something like this. I'm gonna copy and paste or you end up looking like, and that's why if two sets of data, it's not particularly concise. And all psi, if we remember the data are imaginary ball centers. The diode is in rows, and now we've got a single column. So it would probably require more formatting. And, and also with the data formatted law, this, it's, it's quite dissimilar to the original data and therefore less intuitive. So what I'm gonna do now is introduce you to a data structure called arrays. And using arrays have formatted data will be a lot more concise. So let's jump five. It's a, how a test I h k. Let's see how arrays work. Let's just create an array. An array looks much like the declaration of a variable. Well, we've got some square brackets here. And the array, we can have different elements and call them all first element. They want us to about this first element. Second element, third element array. And these are the elements in them, and that's how you declare it. This is the first element in hockey. The first element begins with number one, that's the first element, the second element, and that's the third on it. So this is the array data structure. So that's Troy retrieving elements. Per message box. The idea is there is diode retrieve the first element now can't just access it now that we have to use the percent stalling, the force and expression, save that, and let's try running it. And sure enough, it brings back my first element, the first element for a fluid element. So that's working great. So let's try this with something more similar small wave go. So if all I bring in two records, someday, a pre formatted in an eraser and erase the and I'll go through how to format the title of this using a spreadsheet. But for now, we've got exactly the same as my array, except the elements are different. In the first dominic IT skills second, and you go date. Third element, name, fulfillment and an employee number, each elements enclosed in double quotes. So let's try and retrieve element as little message box sinusoid. Record on one. I say, I comment this out. And it works for us ultimately will have hundreds, possibly thousands of records. And we want to dynamically create a reference to the record. Say for example, if we need this function. So I've created a function that just to just say we can explicitly kickoff are carried. So environment, if you run it up and telling you about and label variable. So the moment, because this is a function, this record one, array reference is not accessible from within the function. So what we're going to have to do that we can create Skype or in Global before a variable name. On a Friday night. We'll give it a Skype meeting that it's accessible anyway. Shouldn't error laced. It doesn't. Because we haven't asked the called function psi form IT skills. And that's what we can do right? Now inside this function. And that's my kit run twice, which corresponds to the number of records we're going this instance. Sorry, the slope of it. I want too many curly braces. And let's try running that. It skills once. It skills twice, so it's routing twice. But we don't want it to do that. We want it to loop and progress to the second record. It's how do we do that? What we can do is create a variable that increases every time we loop. Say, if we declare a variable here called record, start there. And then brought record number close equals one. That's the incremental upright, which will increase the value of record number boy won every time we go through that loop. So let's have a look at that. So if we, if we record message box, I should say the value of this variable increase is try that. First time it leaves, it goes to 1 second toil leaves to, to complete psi. Let's get rid of this now. And let's try using our variable. So refer to each record. What we try to do here is reference the variable record one, record two using record number seven, rot record, and then record number since on. And then one being the first element. So what's going on here? Let's zoom in a little bit more closely. Record here is cutting as a string and it refers to the record par, the variable name. Record number is a variable and this record number refers to the number one or number two. Number two in the second label. And number one, square brackets refers to the first element of the array. So moiety skills first and then the second leap, first aid. And that's how you bring together an array reference so you can retrieve the element. So let's say that marine. Interestingly, we get o IT skills. And then first item, the workplace. So that's pretty neat. 14. Using arrays to fill a form multiple times: So let's try using arrays in actual code used to fill the form. So let's get rid of this old style of accessing the data. And we'll copy and paste just for now. These arrays. Let's see what we've got. We've got record number from last time, so that's good. We can keep that. And we can, when we incremented single Rico Fill in the synchronous single record film is just accessing standard variable. We need to change that to record and then record number. And then so this is firstname, this field here. So that's 123, first element, second element. So we can reframe that unless we can copy that code for each one. So the first field is IT skills. Let's pull IT skills. Second field is a date and is accessing the second element. Guessing that's for employee number is five. And let's put in complete. And as put in the loop. So I want to end the loop. Message box I, success. That's forwarding that psi. Make sure we're on a fresh form. They're gonna loop two times. So you should enter these whole records looking for Jason camp. Meet the following details as well. So it doesn't quite work. So if we look at our code, we can see that we will call it calculator in just copying and pasting send input. So what the problem was, the dropdown isn't most Indian pole and if we send now should then work. So let's try it on a fresh form so it's ready to go and save. Run. Now say it, success. So have a look at a spreadsheet and it's painted. Absolutely fine. That's great. That's essentially it. We've managed to accredit carried with loops over all records and automates in the data in a form. In the next lesson, I'll show you how to format this carried using Google Spreadsheets. Congratulations on finishing the project. Have a play around with the code and see if you can automate your tasks. 15. Bonus lesson - Formatting data: So I want my formatting the data so we can have in arrays ready for HK is to use Google Spreadsheets. It's not the only way you can use Excel. And it's possibly giveaways among ways of for my newdata. But essentially, if you use the attached foil in the course resources, you will find all the data that we're going to use Google Sheets to convert into arrays. So it's ready for a HK to digest, select it and copy it. Control c. And then carried to Google Drive. New Sheet, blank spreadsheet and pasted strain pixels top formatted. It will split into columns rather. So that's quite convenient as a benefit, Tafel might put, you can configure it. So I'm sure you'll do it with any source separation. So now what we're going to do is create an array. Remember our array. Array looks like this. A, a global record one colon equal asset does a declaration of an array, count on equalss square bracket, and a great first element, second, third, fourth, fifth element. And we call these inverted commas in between a comma separating the elements. Thus, essentially what we're gonna do is get the spreadsheet to wrote the array for us. We're going to do it. We're going to use the concatenate commands. If you wrote concat. Night, it's the second one. That adds to give a t values is appends two strings to one new life we use that. We can say something like, I want the date. Copy's A1. First off, the first EJ. And HD elements must remember is put in inverted commas hero that you get. All spell Ross. My birthday is on the date of the first and June. So essentially we are using this formula to bringing different elements to give a delay that J equals a. So equally forms a spreadsheet that we are using the formula. So we're gonna use up valuable record. And we will record Islam an operand. Pull out brackets in everyone, a very common say component countering dollar and it, because it's Google Spreadsheet thinks that you're enclosing a string. But we got to use another technique. You can escape it. Oh, we'll use this technique which such a variety in the character code for e-commerce safely entered IOC what we've got so far, so you can say the column's gone in and replaced the charge 34. So we carry on right now. And you can say, I've missed comma. I'm not going to put the comma because when an aid to them, I'm going to just pull in, in a cell or that refer to allow that, that why have always rowing char for E2 hundreds of times. So I'm going to see what that looks like. That. And you can see here the date format. That's a date for 47 noise. And so what we need to do is convert that to a string, since recognise it as some sort of date to text. And I, this is a bit tedious for a steady for referring to raw bits. To 34. I should say, needs us. Bring that down each printer all the way down and then code for us. Right? So what you do then, you use a drag this all the way down to arrive along, you'll you'll record list is and press Copy and Paste it. Let's skip this for a second. So you imagine coded as a for all our arrays all nicely listed on that. So that's that. 16. Bonus lesson - Page wait: As it stands, the code works and the automation warns brilliantly, but we can always make our code or there'll be more slick. And the moment the form to be filled and we click submit. And we don't actually wait for this page to be noted. We wait a standard amount of time. If we look at our code is just two seconds. And we hope that these pages loaded. What we could do to improve our code is other page weight function that whites are more appropriate amount of time then continues to run the code. So what we could do to find out if this page is loaded is look for a specific bit texts which only appears on this page. And that is your responses being recorded. So you could use that. Now, we will write this bit of code and there's possibly offer ways of doing it. But I think it's a good way of introducing an if statement which I missed out on the main bulk, bulk of the course. And that's largely because we didn't need it. But since they're standard bear programming, it's probably good to include that. And also waiting for the page load is something that you probably need to do if you're going to automate any saw browser. So we'll need it twice. Well needed to get to this page. And we'll need it also the page wait to submit a normal response to wait for this page to occur. So I'll write some code to deal with this situation. So what we'll do, we'll begin with the code as we left it. So here we've got asleep command and we'll get rid of that. And we'll get rid of this. But that's commented out. And we'll put in our page weight code. What we'll do, we'll type page weight and run that for now. We haven't actually written this page weight. In fact. Let's just common now for now as well. And that's where our page weight function will go. What we can do is draw our page weight function here. So paige wait, put the block of code below which we want to run the weights of the page in here. So let's just fill the form in two weeks and covers the clique to save time. So in order to save this page has loaded test if this text here, you're response has been recorded, has actually loaded on onscreen. And we could keep on testing that. We have a loop until it does. If it does, we could run the continue running the code as if the new, new formers bloated. And if not, we just keep on testing for this text. So that's data. We want is a loop command. Let's loop it, say 50 times. I'm going to have. I sleep commands just for the sacred might not need this. This is just a way for any of the previous tasks to finish off. I'll put cold, cold mode in here just to let you know what type of screen coordinates we are using. And then we're going to is this type of action, which is a click into the screen, boy before you drag across to the end of the full stop and then copy. So we know what that text is. This is going to continuously do this if these pages and here, it will just pay with nothing or some other text. So let's do that. So mouse click and drag. Deploys mass born x position, y position, which says Stop starts of our drug. And x2 exploiter is the end of our drag is optional, which we're going to leave them in square brackets. We don't need them. And which favor that. So we can recall we want like in lowercase, Just pixel then. And we're going to pass. So the moment we haven't actually explicitly put in these coordinates, because we're going to give the coordinates, we're going to use this page weight among toys. So instead of writing the code out toys, we can run at once and pass in different coordinates each time. So the first time is when you submit the form. And then the second time is, do you want to fill in ANOVA formula and over time, which is a normal response to the first one was when we, so it, it second time is submit a novel response. So you want to page white commands. So that's what we're gonna do it in a function so we can pass in these coordinates voice. So yes, a milliseconds, you're going to send the copy command, control zeta is. And click wait. What this does. What this does is if you look at the IHI documentation, it just waits until something's actually entered the click the computer clipboard, a voice. The code can run faster than something's actually entered the memory of the clipboard. So it's just the way you could replace that are used to do this. We've asleep command. Just the way a few seconds, we'll insert this school sleep clip weight. Probably bad to use that. Then what we're gonna do, we're gonna cycle, we've copied, which is clipboard. That's the clipboard variable. And just clean it up a little bit. What happened? What kinda happened when I first started using copy, using h k, you can actually copy this text, but it won't appear. Appear like one line or not. It will appear enter and a non-return, a carriage return. And this for me because it looks exactly the same text, but you actually want to get rid of these new lines. So we're gonna get rid of that. He got tested. If you're testing your response has been recorded and that's against your response as being recorded with a new line. The not exactly going to equal each other. So what we've got to do is strip out new line with this code here. And if you leave out, the optional thing is going to replace it with a little, replace it with nothing. So just take a look in the string, which is what we've copied from the web page. And it'll strip out any character turns. Then we can click this clip format. Let's change that to formatted. That's what we want is an active format equals target text. We want to come out of the loop and carry on running the code. How do we do that? We just wrote break. So that command comes out of this loop, which will keep on running until it finds a text and say I can't be found the text and carry on running. I could have in air. So if or else commands just break out the code just in case it never finds it, we're going to leave that for now. Well, is there if you want to put in, we're gonna put mouse-click X11. And what that does is, if you look at this, so it's gonna start, begin not loop page right, click across the and it's already halted. Or that mouseClicked does is just reset it. Otherwise it's already highlighted. So if you have a lot of text and you click in our slick, reset it so it allows you to highlight again mouseClicked left. And we're going to sleep for 100 milliseconds just to allow our code to slow down a little bit too fast. And that's essentially now you might have noted. And we've got this page white command, which is going to be called My maybe a code page weight here, which will come in to bring that back in. But we haven't actually given it. So this is going to call page weight, but it hasn't actually put these parameters in. Let's put those in. We want x1, y1, so that's the beginning of the drag. And we want our target text. So what does that mean? So when we call the page weight function, we're gonna pass it to stop position of the drag those to the end position of the drug, which is those two. And the text we want to check it for, check against, which is tau you text. So let's try that. Now I've got these x and y coordinate saved. We will have to look them up on women's bodies. Obviously, I don't know what you guys are going to paste them. They're so easy target texts and is your x and y and go and this is the second page. Wait, let's try that. We'll start to begin in. Save access. Some look if that's worked. And it has to, that's great.