home ~ projects ~ socials

Get Coordinates For Windows With Mac's JavaScript For Automation

This pulls the window coordinates for a single app.

#!/usr/bin/env osascript -l JavaScript

const appName = "iTerm2";
const windowCount = Application(appName).windows.length;

for (let windowIndex = 0; windowIndex < windowCount; windowIndex ++) {
    const window = Application(appName).windows[windowIndex];
    const windowBounds = Application(appName).windows[windowIndex].bounds();
    console.log(window.name());
    console.log(
      `x: ${windowBounds.x} - y: ${windowBounds.y} - width: ${windowBounds.width} - height: ${windowBounds.height}`
    );
}

NOTE: This may not work with all apps. When setting window positions a different method needs to be used.

-- end of line --