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.
npm run import -- "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
// 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;
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.
assert
: for assert functionffi-rs
: for Foreign Function Interface (FFI) bindings to dynamic librariespath
: for path manipulationos
: for determining the current platformenumWindows()
Enumerates the titles of visible windows on the system.
sog
using open
function from ffi-rs
.enumWindows
function from the library to get the count of visible windows.getTitle
function from the library to get the title of each window.close
function from ffi-rs
.IGNORE_PROGRAMS
list.dynamicLib
: the path to the dynamic library file based on the current platform.IGNORE_PROGRAMS
: an array of programs to ignore.a
and b
: unused variables (constants 1 and 100 respectively).count
: the count of visible windows returned by enumWindows
function.result
: the title of each window returned by getTitle
function.windows
: the array of window titles.platform
: the current platform determined by os.platform()
.