dylib | | Cell 1 | Search

This Node.js module exports a function enumWindows that enumerates the titles of visible windows on the system, filters out certain programs, and returns an array of window titles. The function uses Foreign Function Interface (FFI) bindings to a dynamic library sog to interact with the system's window management API.

Run example

npm run import -- "include a dynamic lib"

include a dynamic lib

const {
  equal
} = require('assert')
const {
  load,
  DataType,
  open,
  close,
  arrayConstructor,
  define
} = require('ffi-rs')
const path = require('path')
const {platform} = require('os')
const a = 1
const b = 100
const dynamicLib = platform === 'win32' ? '../sog.dll' : (platform === 'linux' ? '../sog.so' : '../sog.dylib')
// First open dynamic library with key for close
const IGNORE_PROGRAMS = [
  'Window Server',
  'Macs Fan Control',
  'Control Center',
  'Spotlight',
  'Dock',
  'SSMenuAgent',
  'Menubar'
]
function enumWindows() {
  // It only needs to be opened once.
  open({
    library: 'sog', // key
    path: path.join(__dirname, dynamicLib) // path
  })
  const count = load({
    library: "sog", // path to the dynamic library file
    funcName: 'enumWindows', // the name of the function to call
    retType: DataType.I32, // the return value type
    paramsType: [], // the parameter types
    paramsValue: [] // the actual parameter values
    // freeResultMemory: true, // whether or not need to free the result of return value memory automatically, default is false
  })
  let windows = []
  for(let i = 0; i < count; i++) {
    const result = load({
      library: "sog", // path to the dynamic library file
      funcName: 'getTitle', // the name of the function to call
      retType: DataType.String, // the return value type
      paramsType: [DataType.I32], // the parameter types
      paramsValue: [i] // the actual parameter values
      // freeResultMemory: true, // whether or not need to free the result of return value memory automatically, default is false
    })
    windows.push(result.split(/:\s/g)[1])
  }
  // Release library memory when you're not using it.
  close('sog')
  return windows.filter((a, i, arr) => arr.indexOf(a) == i && !IGNORE_PROGRAMS.includes(a))
}

module.exports = enumWindows

What the code could have been:

// Import required modules
const { assert } = require('assert');
const { load, DataType, open, close, arrayConstructor, define } = require('ffi-rs');
const path = require('path');
const { platform } = require('os');

// Define constant for library path based on platform
const dynamicLibPath = path.join(__dirname, getDynamicLibPath());

// Define a function to get dynamic library path based on platform
function getDynamicLibPath() {
  switch (platform) {
    case 'win32':
      return '../sog.dll';
    case 'linux':
      return '../sog.so';
    default:
      return '../sog.dylib';
  }
}

// Define a function to enumerate windows and get their titles
function enumWindows() {
  // Open dynamic library with key for close
  const lib = open({
    library:'sog',
    path: dynamicLibPath,
  });

  // Define a function to get the count of windows
  const getCount = load({
    library:'sog',
    funcName: 'getCount',
    retType: DataType.I32,
    paramsType: [],
    paramsValue: [],
  });

  // Get the count of windows
  const count = getCount();

  // Define a function to get the title of a window
  const getTitle = load({
    library:'sog',
    funcName: 'getTitle',
    retType: DataType.String,
    paramsType: [DataType.I32],
    paramsValue: [0],
  });

  // Create a list to store the titles of windows
  const titles = [];

  // Iterate over the count of windows
  for (let i = 0; i < count; i++) {
    // Load the getTitle function with the current index
    getTitle.paramsValue[0] = i;

    // Get the title of the current window
    const title = getTitle();

    // Add the title to the list of titles
    titles.push(title.split(/:\s/g)[1]);
  }

  // Close the library
  close(lib);

  // Filter out the windows with titles that match the ignored programs
  const ignoredPrograms = [
    'Window Server',
    'Macs Fan Control',
    'Control Center',
    'Spotlight',
    'Dock',
    'SSMenuAgent',
    'Menubar',
  ];

  return titles.filter((title, index, array) => array.indexOf(title) === index &&!ignoredPrograms.includes(title));
}

// Export the enumWindows function
module.exports = enumWindows;

Code Overview

This code is a Node.js module that exports a function enumWindows which enumerates the titles of visible windows on the system and filters out certain programs.

Dependencies

Functions

enumWindows()

Enumerates the titles of visible windows on the system.

  1. Opens the dynamic library sog using open function from ffi-rs.
  2. Calls the enumWindows function from the library to get the count of visible windows.
  3. Loops through the count and calls the getTitle function from the library to get the title of each window.
  4. Pushes the title into an array.
  5. Closes the library using close function from ffi-rs.
  6. Filters out certain programs from the array based on the IGNORE_PROGRAMS list.
  7. Returns the filtered array of window titles.

Variables