Get metadata about all windows (title, PID, owner) on macOS

Himanshu Agrawal
BYJU’S Exam Prep Engineering
2 min readMay 12, 2020

Github Repo Npm Package

At times we need details of open windows on the system like Title, Owner, Window Size, etc. In our scenario, during the automation of OBS, we wanted to use the Window Capture functionality. OBS provides various scripting options using C++ and Python, but we used obs-WebSocket to interact from our Electron app to OBS.

Since OBS needs certain details of the window to capture and broadcast the same, it is a bit tedious to grab those details from the system using an electron app, thus we decided to use an npm package to get the required details. Similar functionality has already been implemented by sindresorhus but it only provided details of the active window. We tweaked around with this package and created another package all-windows which provides details of all the open windows.

Here is how to get started with this:

Installation:

npm install all-windows

Usage:

const allWindows = require('all-windows');(async () => {
console.log(await allWindows());
/*
[
{
title: 'Unicorns - Google Search',
id: 5762,
bounds: {
x: 0,
y: 0,
height: 900,
width: 1440
},
owner: {
name: 'Google Chrome',
processId: 310,
bundleId: 'com.google.Chrome',
path: '/Applications/Google Chrome.app'
},
url: 'https://www.google.com/search?q=unicorn',
memoryUsage: 11015432
}
]
*/
})();

The response contains an array of objects with information like title, id, bounds, URL (only when a browser window), and memoryUsage.

OBS specific implementation:

obs.sendCallback(
'SetSourceSettings',
{
sourceName: captureList[0].name,
sourceSettings: {
window: pid,
window_name: title,
owner_name: owner,
},
},
(error, data) => {
if(error) console.log(error);
if (data) console.log(`window updated`);
}
);

The package is written in Swift which gathers information about all windows. This package is built into a binary (main) which is used by the npm package to get all the details with the help of an async function.

A similar implementation can also be done for windows. Microsoft offers its APIs which can be used in C# and an executable can be generated using the available APIs. Further, the npm package can execute the exe and export the details as a JSON array.

--

--

Himanshu Agrawal
BYJU’S Exam Prep Engineering

Software Engineer, Exploring new tools, working on POCs, finding peace in optimising software solutions as well as debugging colleague's issues.