~ 6 min read

Automating (Boring) Tasks with AutoHotkey

"I'm bringing macros back." — Justin Timberlake, probably.

While I don’t land a sweet developer job, I earn money by doing work in the field I previously worked, Education. I teach English as a Second Language privately but I also work as a tutor for a private college which offers mainly distance education degrees. Working as a tutor in said place is quite boring: My working consist of basically correcting the same assignment over and over, and the platform we use is prehistoric. It is a flash website packaged in Electron, and you can imagine how painful it to use an interface that has been the same for at least fifteen years. I try not to think about too hard so I won’t get angry when I’m working, and that usually works.

Unfortunately, today wasn’t one of those days. I got called at a meeting because there was a problem with the grading for all the subjects I was working on. The maximum scores weren’t properly set up so the actual maximum scores were threefold what we were told to work with and the solution was to regrade everything. I got very angry for a minute, because that would mean regrading around 1100 assignments between the subjects I’m responsible for. I thought about complaining for a second, but I put my problem-solving skills to work. Since the regrading process encompassed only increasing the grade three-fold and I couldn’t inject any code into the system because it is flash-based, I decided upon building a macro to solve my problem.

I always thought of macros as a technology of bygone times when you had to build an excel spreadsheet for everything and that they where used nowadays only as auto-clickers for games, but how mistaken I feel after today.

The problem I had to solve was quite simple in theory, I needed the macro to:

  • Click on an assignment and open it;
  • Copy the grade contained within it;
  • Multiply the grade by three;
  • Paste it back into the grade;
  • Save it;
  • Repeat the process until it reached the end of the list;

But there were some caveats I had to deal with:

  • My screen only holds 17 assignments at time, so I had to make it scroll to the end of the list;
  • The list moves when you press down on the second-last item, not the last one;
  • The list resets its scrolling position after I submit an activity;
  • the scroll wheel jumps 3 rows at a time, but the down key only jumps one.
  • There are no filters I could use to separate wrongly graded items from the new ones;
  • Assignment fetching and post is slow, so I had to calculate wait times.

The main UI looks roughly like this: main UI sketch

And the assignment UI looks roughly like this:

assignment UI sketch

For the tool to solve the issue, I’ve decided to go with AutoHotkey because it is open source and it uses a very simple scripting language, which I took me less than an hour to learn. To create the macro I wrote the following algorithm and variables:

Variables

  • count = 0 : resets the counter;
  • limit: how much assignments are in each given subject code;
  • delta0: how many times I initially need to press the down key to get the assignment that needs regrading on the first row. It is useful when I can’t do the whole list in one go.
  • delta: same as delta0, but for the current value. Its initial value is delta0.
  • mouseDelta: how much I have to vertically move the mouse to go to the next row.
  • mousepos: The vertical coordinate for a assignment row.

Algorithm

While count < (272 - 16 - delta0) -> R.H.S. is the last scrollable row;
-> Click on the second to last row on the table
-> Press the Down key delta times;
-> Click on the first line;
-> Wait 0.2 seconds;
-> Click on the 'Grade Assignment' button;
-> Wait 7 seconds;
-> Select the grade;
-> Wait 0.5 seconds;
-> Copy the grade to the clipboard;
-> Wait 0.1 seconds;
-> Multiply grade by 3;
-> Wait 0.1 seconds;
-> Delete old grade;
-> Wait 0.1 seconds;
-> Type new grade;
-> Save the assigment;
-> Wait 5 seconds;
-> increase count by one;
-> increase delta by one;

While count >= (272 - 16 - delta0) && count < limit
-> Click on the second to last row on the table
-> Press the Down key delta times;
-> Wait .5 seconds;
-> Double-click on the "line-th" line;
-> Wait 0.2 seconds; 
-> Click on the grade button;
-> Wait 7 seconds;
-> Select the grade;
-> Wait 0.5 seconds;
-> Copy the grade to the clipboard;
-> Wait 0.1 seconds;
-> Multiply grade by 3;
-> Wait 0.1 seconds;
-> Delete old grade;
-> Wait 0.1 seconds;
-> Type new grade;
-> Save the assigment;
-> Wait 5 seconds;
-> increase count by one;
-> increase line by one;
-> increase mousePos by mouseDelta;  

I did a sketch of how the algorithm works, too:

Algorithm sketch

You can observe that the there is a lot of waiting around in this algorithm. The first iteration I did had way less waiting, but not waiting enormous amounts of times broke it because the system is too slow to process everything instantly. I even have to wait for it to render the grading window and then some, otherwise it doesn’t draw the window correctly and the macro misclicks it.

And that solved it beautifully. The only downside is that I can’t use the computer because the macro needs to move the mouse, otherwise, this would be the perfect solution, but I have to admit that it is pretty satisfying to watch the computer work while I can just watch and sip my coffee.

It was a pretty funny exercise to automate boring parts of my work and to put some developer-thinking into an otherwise massively boring task. I now can see that I can build some other macros to speed up my work even more, and I’m eager to try it.

That’s all for today folks, have a good one!