Convert Figma logo to code with AI

octalmage logorobotjs

Node.js Desktop Automation.

12,304
950
12,304
192

Top Related Projects

113,668

:electron: Build cross-platform desktop apps with JavaScript, HTML, and CSS

2,188

Native UI testing / controlling with node

:zap: Delightful Node.js packages and resources

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

88,205

JavaScript API for Chrome and Firefox

Quick Overview

RobotJS is a Node.js desktop automation library that allows you to control the mouse, keyboard, and screen. It provides a simple and intuitive API for simulating user input and capturing screen content, making it useful for creating macros, automated testing, and other desktop automation tasks.

Pros

  • Cross-platform compatibility (Windows, macOS, Linux)
  • Easy-to-use API with straightforward methods
  • High-performance native implementation
  • Extensive documentation and community support

Cons

  • Limited support for complex GUI interactions
  • Potential security risks if used improperly
  • May require additional setup on some Linux distributions
  • Not suitable for web automation (use Puppeteer or Selenium instead)

Code Examples

  1. Moving the mouse and clicking:
const robot = require('robotjs');

// Move the mouse to x, y and click
robot.moveMouse(100, 100);
robot.mouseClick();
  1. Typing text:
const robot = require('robotjs');

// Type "Hello, World!" with a 500ms delay between keystrokes
robot.typeString("Hello, World!", 500);
  1. Capturing screen content:
const robot = require('robotjs');

// Get pixel color at x, y
const color = robot.getPixelColor(100, 100);
console.log(color);

// Capture screen region
const screenshot = robot.screen.capture(0, 0, 800, 600);
console.log(screenshot.width, screenshot.height, screenshot.image);

Getting Started

To use RobotJS in your Node.js project, follow these steps:

  1. Install RobotJS:

    npm install robotjs
    
  2. Create a new JavaScript file (e.g., automation.js) and add the following code:

    const robot = require('robotjs');
    
    // Move the mouse to the center of the screen
    const screenSize = robot.getScreenSize();
    const x = screenSize.width / 2;
    const y = screenSize.height / 2;
    robot.moveMouse(x, y);
    
    // Type "Hello, RobotJS!"
    robot.typeString("Hello, RobotJS!");
    
  3. Run the script:

    node automation.js
    

This will move the mouse to the center of the screen and type "Hello, RobotJS!".

Competitor Comparisons

113,668

:electron: Build cross-platform desktop apps with JavaScript, HTML, and CSS

Pros of Electron

  • Cross-platform desktop application development using web technologies
  • Large ecosystem and community support
  • Extensive documentation and resources for developers

Cons of Electron

  • Larger application size due to bundled Chromium and Node.js
  • Higher memory usage compared to native applications
  • Potential security concerns due to full system access

Code Comparison

Electron (main process):

const { app, BrowserWindow } = require('electron')

function createWindow () {
  const win = new BrowserWindow({ width: 800, height: 600 })
  win.loadFile('index.html')
}

app.whenReady().then(createWindow)

RobotJS:

var robot = require("robotjs");

// Move the mouse to 100, 100 on the screen.
robot.moveMouse(100, 100);

// Left click.
robot.mouseClick();

Key Differences

  • Electron focuses on building desktop applications with web technologies
  • RobotJS is primarily for automating system-level tasks and simulating user input
  • Electron provides a full application framework, while RobotJS offers specific system automation functions
  • RobotJS has a smaller footprint and is more suitable for lightweight automation scripts
  • Electron applications can have rich user interfaces, whereas RobotJS typically runs without a GUI
2,188

Native UI testing / controlling with node

Pros of nut.js

  • Cross-platform support for Windows, macOS, and Linux
  • Provides image search and OCR capabilities out of the box
  • Offers a more extensive API for complex automation tasks

Cons of nut.js

  • Slower execution compared to RobotJS due to its JavaScript nature
  • Requires more setup and dependencies, potentially increasing project complexity

Code Comparison

nut.js:

const { mouse, screen } = require("@nut-tree/nut-js");

(async () => {
  await mouse.move([100, 100]);
  await mouse.leftClick();
})();

RobotJS:

const robot = require("robotjs");

robot.moveMouse(100, 100);
robot.mouseClick();

Both libraries provide similar functionality for basic mouse operations, but nut.js uses an asynchronous approach with Promises, while RobotJS offers a more straightforward synchronous API. nut.js provides more advanced features like screen capture and image recognition, making it suitable for complex automation tasks. However, RobotJS is generally faster and lighter, making it ideal for simpler scripts or performance-critical applications.

:zap: Delightful Node.js packages and resources

Pros of awesome-nodejs

  • Comprehensive curated list of Node.js resources, libraries, and tools
  • Regularly updated with community contributions
  • Covers a wide range of Node.js-related topics and categories

Cons of awesome-nodejs

  • Not a functional library or tool itself, just a collection of resources
  • May include outdated or less maintained projects alongside popular ones
  • Requires additional effort to evaluate and choose appropriate tools for specific needs

Code comparison

Not applicable, as awesome-nodejs is a curated list and doesn't contain executable code. RobotJS, on the other hand, is a functional library for desktop automation. Here's a simple example of RobotJS usage:

const robot = require('robotjs');

// Move the mouse to x, y and click
robot.moveMouse(100, 100);
robot.mouseClick();

Summary

awesome-nodejs serves as an extensive resource for Node.js developers, providing a curated list of tools, libraries, and resources. It's beneficial for discovering new projects and staying updated with the Node.js ecosystem. However, it's not a functional tool itself and requires additional effort to evaluate and implement the listed resources.

RobotJS, in contrast, is a specific library for desktop automation, offering direct functionality for tasks like mouse and keyboard control. While it serves a particular purpose, it doesn't provide the broad overview of the Node.js ecosystem that awesome-nodejs does.

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

Pros of Playwright

  • Cross-browser support (Chromium, Firefox, WebKit)
  • Powerful API for web automation and testing
  • Active development and community support

Cons of Playwright

  • Focused on web automation, less suitable for general desktop automation
  • Steeper learning curve for non-web developers

Code Comparison

Playwright:

const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await browser.close();
})();

RobotJS:

const robot = require('robotjs');

robot.moveMouse(100, 100);
robot.mouseClick();
robot.typeString("Hello, World!");

Key Differences

  • Playwright is designed for web automation and testing, while RobotJS focuses on desktop automation
  • Playwright offers cross-browser support, whereas RobotJS is platform-specific
  • RobotJS provides low-level system interactions (mouse, keyboard), while Playwright operates at a higher level within web browsers

Use Cases

  • Playwright: Web scraping, automated testing, browser automation
  • RobotJS: Desktop automation, game bots, system-level interactions

Community and Support

Playwright has a larger and more active community, with frequent updates and extensive documentation. RobotJS, while useful, has a smaller community and less frequent updates.

88,205

JavaScript API for Chrome and Firefox

Pros of Puppeteer

  • Provides a high-level API for controlling Chrome/Chromium browsers
  • Supports headless mode for server-side rendering and testing
  • Offers extensive documentation and a large community

Cons of Puppeteer

  • Limited to web-based automation; cannot interact with native desktop applications
  • Requires a browser instance, which can be resource-intensive
  • May have slower execution compared to native automation tools

Code Comparison

RobotJS (Native desktop automation):

const robot = require('robotjs');

robot.moveMouse(100, 100);
robot.mouseClick();
robot.typeString("Hello, world!");

Puppeteer (Web-based automation):

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.click('#button');
  await page.type('#input', 'Hello, world!');
})();

Key Differences

  • RobotJS focuses on native desktop automation, while Puppeteer specializes in web automation
  • RobotJS provides lower-level control over mouse and keyboard inputs
  • Puppeteer offers more advanced web-specific features like page navigation and element selection
  • RobotJS is generally faster for simple tasks, while Puppeteer excels in complex web scenarios

Both tools have their strengths and are suited for different use cases, depending on whether you need desktop or web automation.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Node.js Desktop Automation. Control the mouse, keyboard, and read the screen.

RobotJS supports Mac, Windows, and Linux.

This is a work in progress so the exported functions could change at any time before the first stable release (1.0.0). Ideas?

Check out some of the cool things people are making with RobotJS! Have your own rad RobotJS project? Feel free to add it!

Contents

Installation

Install RobotJS using npm:

npm install robotjs

It's that easy! npm will download one of the prebuilt binaries for your OS.

You can get npm here if you don't have it installed.

If you need to build RobotJS, see the building section. Instructions for Electron.

Examples

Mouse

// Move the mouse across the screen as a sine wave.
var robot = require("robotjs");

// Speed up the mouse.
robot.setMouseDelay(2);

var twoPI = Math.PI * 2.0;
var screenSize = robot.getScreenSize();
var height = (screenSize.height / 2) - 10;
var width = screenSize.width;

for (var x = 0; x < width; x++)
{
	y = height * Math.sin((twoPI * x) / width) + height;
	robot.moveMouse(x, y);
}
Keyboard
// Type "Hello World" then press enter.
var robot = require("robotjs");

// Type "Hello World".
robot.typeString("Hello World");

// Press enter.
robot.keyTap("enter");
Screen
// Get pixel color under the mouse.
var robot = require("robotjs");

// Get mouse position.
var mouse = robot.getMousePos();

// Get pixel color in hex format.
var hex = robot.getPixelColor(mouse.x, mouse.y);
console.log("#" + hex + " at x:" + mouse.x + " y:" + mouse.y);

Read the Wiki for more information!

API

The RobotJS API is hosted at https://robotjs.io/docs/syntax.

Building

Please ensure you have the required dependencies before installing:

  • Windows
    • windows-build-tools npm package (npm install --global --production windows-build-tools from an elevated PowerShell or CMD.exe)
  • Mac
    • Xcode Command Line Tools.
  • Linux
    • Python (v2.7 recommended, v3.x.x is not supported).
    • make.
    • A C/C++ compiler like GCC.
    • libxtst-dev and libpng++-dev (sudo apt-get install libxtst-dev libpng++-dev).

Install node-gyp using npm:

npm install -g node-gyp

Then build:

node-gyp rebuild

See the node-gyp readme for more details.

Plans

  • √ Control the mouse by changing the mouse position, left/right clicking, and dragging.
  • √ Control the keyboard by pressing keys, holding keys down, and typing words.
  • √ Read pixel color from the screen and capture the screen.
  • Find an image on screen, read pixels from an image.
  • Possibly include window management?

Progress

ModuleStatusNotes
Mouse100%All planned features implemented.
Keyboard100%All planned features implemented.
Screen85%Image search, pixel search.
Bitmap0%Saving/opening, png support.

FAQ

Does RobotJS support global hotkeys?

Not currently, and I don't know if it ever will. I personally use Electron/NW.js for global hotkeys, and this works well. Later on I might add hotkey support or create a separate module. See #55 for details.

Can I take a screenshot with RobotJS?

Soon! This is a bit more complicated than the rest of the features, so I saved it for last. Luckily the code is already there, I just need to write the bindings, and I've already started. Subscribe to #13 for updates.

Why is <insert key> missing from the keyboard functions?

We've been implementing keys as we need them. Feel free to create an issue or submit a pull request!

How about multi-monitor support?

The library doesn't have explicit multi-monitor support, so anything that works is kind of on accident. Subscribe to #88 for updates.

For any other questions please submit an issue.

Story

I'm a huge fan of AutoHotkey, and I've used it for a very long time. AutoHotkey is great for automation and it can do a bunch of things that are very difficult in other languages. For example, it's imagesearch and pixel related functions are hard to reproduce on Mac, especially in scripting languages. These functions are great for automating apps that can't be automated like Netflix. This has never been a big deal since I've always used Windows at work, but for the past few years I've been using Mac exclusively.

I like AutoHotkey, but I like Node.js more. By developing RobotJS I get an AutoHotkey replacement on Mac (finally!), and I get to use my favorite language.

TLDR: There's nothing like AutoHotkey on Mac, so I'm making it.

License

MIT

Based on autopy. Maintained by Jason Stallings.

NPM DownloadsLast 30 Days