Skip to content
Related Articles

Related Articles

How to use Javascript and Robotjs package to auto set up your desktop ?
  • Last Updated : 14 Apr, 2021

This article shows the method to set up your desktop using automation with the help of Robotjs package and JavaScript.The below steps have to be followed.

Step 1: Install the latest Node.js runtime from here .

Step 2: Install the robotjs package. We are going to nstall this package locally i.e. it will be accessed only in the working folder. This can be done by opening terminal/command prompt in the same directory where the node is installed and running the below command.

npm install robotjs

Step 3: Create a JavaScript file in the same directory as the credentials file. This file would contain the code for controlling the operating system and automate the desired tasks. The steps below are followed to do the same. This is written in this main JavaScript file.

  1. Get to the search bar.
  2. Type “openboard” and press enter to open it, then minimize it.
  3. Get to the search bar again.
  4. Type “sublime text” and press enter to open it, then minimize it.
  5. Get to the search bar again.
  6. Type “chrome” and press enter to open it. Open “whatsapp web” and “gfg pratice” tabs in it, then minimize it.
  7. Get to the search bar again.
  8. Type “one note” and press enter to open it, then minimize it.
  9. Get to the search bar again.
  10. Type “notepad” and press enter to open it, then write a “done” message

Step 4: Start the JavaScript file containing the script using the below command.



node automate.js

Complete Code:

Javascript




// Include the robotjs package 
var robot = require("robotjs"); 
// Timeout to wait if system is slow 
setTimeout(startOpenBoard, 1000);
  
//Opening the openboard
//Can learn more about these
//properties from the robotjs site
  
function startOpenBoard(){
    robot.moveMouseSmooth(98,844);
    robot.mouseClick();
    robot.typeString(" openboard ");
    robot.keyTap("enter");
      
    //Minimize openboard
    robot.moveMouseSmooth(1433,28);
    robot.mouseClick();
      
    //Start sublime text after 1s
    setTimeout(startSublimeText, 1000);
}
  
function startSublimeText(){
    robot.moveMouseSmooth(98,844);
    robot.mouseClick();
    robot.typeString(" sublime text ");
    robot.keyTap("enter");
     
   //Minimize sublime
    robot.moveMouseSmooth(1418,8);
    robot.mouseClick();
      
    //Start chrome after 1s
    setTimeout(startChrome, 1000);
}
  
function startChrome(){
    robot.moveMouseSmooth(98,844);
    robot.mouseClick();
    robot.typeString(" chrome ");
    robot.keyTap("enter"); 
      
    //Open whatsapp web
    robot.moveMouseSmooth(506,516);
    robot.mouseClick();
    robot.typeString("whatsapp web");
    robot.keyTap("enter"); 
  
    robot.moveMouseSmooth(349,389);
    robot.mouseClick();
      
    //Open a new tab
    robot.keyToggle("control","down");
    robot.keyTap("t");
    robot.keyToggle("control","up");
      
    //Open gfg practice
    robot.moveMouseSmooth(506,516);
    robot.mouseClick();
    robot.typeString("gfg practice");
    robot.keyTap("enter"); 
  
    robot.moveMouseSmooth(362,788);
    robot.mouseClick();
  
    //Open a new tab
    robot.keyToggle("control","down");
    robot.keyTap("t");
    robot.keyToggle("control","up");
  
    //Minimize chrome
    robot.moveMouseSmooth(1398,23);
    robot.mouseClick();
      
    //Start one note after 1s
    setTimeout(startOneNote, 1000); 
  
function startOneNote(){
    robot.moveMouseSmooth(98,844);
    robot.mouseClick();
    robot.typeString(" oneNote ");
    robot.keyTap("enter"); 
      
    //Minimize onr note
    robot.moveMouseSmooth(1443,10);
    robot.mouseClick();
      
    //Start notepad after 1s
    setTimeout(startNotePad, 1000);
}
  
function startNotePad(){
    robot.moveMouseSmooth(98,844);
    robot.mouseClick();
    robot.typeString(" notepad ");
    robot.keyTap("enter");
    robot.moveMouseSmooth(600,500);
    robot.mouseClick(); 
    //Type a "Set up done" message
    robot.typeString(" Your System is ready to use, Sir.");
}

Output:



NOTE: Here, I have used the coordinates according to my screen size. One can find their screen coordinates by running the following code and pointing their mouse to the location for which to find coordinates.

Run below command:

node screenPosition.js

Code:

Javascript




//Include robotjs package
var robot = require("robotjs");
  
//Show mouse location wherever it is pointing  
var id = setInterval(showMouseLocation,1000);
  
//function that
function showMouseLocation(){
var mousePosition = robot.getMousePos();
console.log(mousePosition);
//Terminate the program 
//whenever mouse points
//at top left corner
//or press ctrl+c to terminate
if(mousePosition.x == 0 && mousePosition.y == 0){
    clearInterval(id);  
}
}
My Personal Notes arrow_drop_up
Recommended Articles
Page :