Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Get Coordinates For Windows With Mac's JavaScript For Automation

This pulls the window coordinates for a single app.

Code

#!/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.