NodeJS Desktop Automation with RobotJS, (but with a program that could get you h̶i̶r̶e̶d̶ fired😄)

NodeJS Desktop Automation with RobotJS, (but with a program that could get you h̶i̶r̶e̶d̶ fired😄)

Some while ago, I saw a meme video of "a day in the life of a software engineer" where the engineer wrote a script to make his computer switch on automatically, open Slack, and move the mouse at regular intervals while he is asleep to make it appear he is online and working at the same time.

We will be writing a similar program with NodeJS using the RobotJS module. RobotJS is a cross-platform desktop automation library.

This is for EDUCATIONAL purposes only. 😊

https://media.giphy.com/media/AEA4MdAfprCn1zevGl/giphy.gif

Steps

  • Run npm install yargs robotjs to install required dependencies.
  • create an app.js file and paste the code below. (I will explain the code):
// app.js
const yargs = require("yargs");
const { hideBin } = require("yargs/helpers");

const arg = yargs(hideBin(process.argv))
  .command("$0 [interval]", true, (yargs) => {
    yargs
      .positional("interval", {
        type: "number",
        describe: "the interval in second",
      })
      .default("interval", 60); // 60 seconds default
  })
  .usage("runs a desktop automator to run key your  mmouse move at interval")
  .example(
    "$0 -mk 3",
    "moves the mouse and press the keyboard after three seconds"
  )
  .option("m", {
    description: "enable the mouse",
    type: "boolean",
  })
  .option("k", {
    description: "enable the keyboard",
    type: "boolean",
  })
  .default("m", true)
  .help("h").argv;

The code above configures the argument options our application needs and also defines a CLI interface to describe the application when you run node app.js -h. We will have options to run only keyboard press (-k), mouse move (-m) or both (-mk) and define the time intervals of the events in seconds. I wrote an article on parsing NodeJS CLI arguments here.

  • We will define boolean variables to ascertain what operations to perform:
let is_both;
let is_mouse;
let is_keyboard;
  • Next, we will define functions to move the mouse and press the keyboard
function moveMouseBackAndForth() {
    robot.moveMouseSmooth(200, 200);
  robot.moveMouseSmooth(400, 400);
}

function pressKeyBoard() {
  robot.keyTap("shift");
}
  • Then we will call the functions depending on the arguments passed. The whole code will look like this:
const yargs = require("yargs");
const robot = require("robotjs");
const { hideBin } = require("yargs/helpers");

let is_both;
let is_mouse;
let is_keyboard;

const arg = yargs(hideBin(process.argv))
  .command("$0 [interval]", true, (yargs) => {
    yargs
      .positional("interval", {
        type: "number",
        describe: "the interval in second",
      })
      .default("interval", 60); // 60 seconds default
  })
  .usage("runs a desktop automator to run key your  mmouse move at interval")
  .example(
    "$0 -mk 3",
    "moves the mouse and press the keyboard after three seconds"
  )
  .option("m", {
    description: "enable the mouse",
    type: "boolean",
  })
  .option("k", {
    description: "enable the keyboard",
    type: "boolean",
  })
  .default("m", true)
  .help("h").argv;

let { m, k, interval } = arg;
// multiply seconds by 1000 to get milliseconds
interval = interval * 1000;

if (m && k) is_both = true;
else {
  if (m) is_mouse = true;
  else if (k) is_keyboard = true;
}

function moveMouseBackAndForth() {
  robot.moveMouseSmooth(200, 200);
  robot.moveMouseSmooth(400, 400);
}

function pressKeyBoard() {
  robot.keyTap("shift");
}

if (is_both) {
  setInterval(() => {
    moveMouseBackAndForth();
    pressKeyBoard();
  }, interval);
} else if (is_keyboard) setInterval(pressKeyBoard, interval);
else {
  setInterval(moveMouseBackAndForth, interval);
}
  • Run node app.js -m 3 to move our mouse only at an interval of 3 seconds.

Thanks for reading through. Would you rather have the program do something else than press the keyboard?

You can get the code from this Github gist

I will appreciate your feedback and questions.