Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 24, 2021 06:14 am GMT

NodeJS Desktop Automation with RobotJS, (but with a program that could get you hired 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.jsconst 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 millisecondsinterval = 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.


Original Link: https://dev.to/zt4ff_1/nodejs-desktop-automation-with-robotjs-but-with-a-program-that-could-get-you-hired-fired-fj

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To