robot | node screenshots | get screen size | Search

The code requires the node-screenshots library and exports a single function, screenshotWindow, which takes a window argument and returns a PNG screenshot of the corresponding desktop application window. The function identifies and captures the window, converts the image to PNG format, and returns it as the result.

Run example

npm run import -- "node window screenshot"

node window screenshot


const fs = require("fs");
const { Window } = require("node-screenshots");


async function screenshotWindow(window) {

  if(!window) {
    window = 0
  }

  let windows = Window.all();
  let item
  if(!Number.isNaN(parseInt(window)))
    item = windows[window]
  else
    item = windows.filter(w => {
      console.log(w.title, w.app_name)
      return w.title.includes(window) || w.app_name && w.app_name.includes(window)
    })[0]

  if(!item)
    return void 0

  /*console.log({
    title: item.title,
    app_name: item.app_name,
    id: item.id,
    x: item.x,
    y: item.y,
    width: item.width,
    height: item.height,
    rotation: item.rotation,
    scaleFactor: item.scaleFactor,
    isPrimary: item.isPrimary,
  })*/

  let image = await windows[window].captureImage()

  return await image.toPng()
}

module.exports = screenshotWindow

What the code could have been:

const fs = require('fs');
const { Window } = require('node-screenshots');

/**
 * Captures a screenshot of a specific window.
 * 
 * @param {string|number} window - The title or index of the window to capture.
 * @returns {string|undefined} The screenshot as a PNG string or undefined if the window is not found.
 */
async function screenshotWindow(window) {
  // Validate input
  if (typeof window!=='string' && typeof window!== 'number') {
    throw new Error('Invalid window input. Must be a string or number.');
  }

  // Get all windows
  const windows = await Window.all();

  // Find the window to capture
  let item = await getWindowItem(windows, window);

  // If no window found, return undefined
  if (!item) {
    return;
  }

  // Capture the window image
  const image = await item.captureImage();

  // Return the screenshot as a PNG string
  return await image.toPng();
}

/**
 * Finds a window by title or index.
 * 
 * @param {Window[]} windows - The array of windows to search.
 * @param {string|number} window - The title or index of the window to find.
 * @returns {Window|undefined} The found window or undefined if not found.
 */
async function getWindowItem(windows, window) {
  // Try to find by index (if it's a number)
  if (!isNaN(parseInt(window))) {
    return windows[window];
  }

  // Find by title (case-insensitive)
  return windows.find(w => w.title.toLowerCase().includes(window.toLowerCase()));
}

module.exports = screenshotWindow;

Code Breakdown

Requirements

The code requires two modules:

Function: screenshotWindow

An asynchronous function that takes a single argument window. It captures a screenshot of a desktop application window and returns the screenshot as a PNG image.

Function Flow

  1. Window Identification:
  2. Window Selection:
  3. Screenshot Capture:
  4. Image Conversion:
  5. Return Value:

Module Export

The screenshotWindow function is exported as the module's default export.