System Events lists application windows in their z-order, but I wonder if the the z-order of all active windows is accessible ?
The Core Graphics framework puts a fair amount of useful information about windows within direct reach of KM Execute ... Script
actions, but I don’t think that includes their z order. Any thoughts ?
JavaScript for Automation:
(() => {
'use strict';
ObjC.import('CoreGraphics')
// CORE GRAPHICS KEYS ----------------------------------------------------
const ks = [
'kCGWindowLayer', 'kCGWindowAlpha', 'kCGWindowMemoryUsage',
'kCGWindowIsOnscreen', 'kCGWindowSharingState', 'kCGWindowOwnerPID',
'kCGWindowNumber', 'kCGWindowOwnerName', 'kCGWindowStoreType',
'kCGWindowBounds', 'kCGWindowName'
];
// GENERIC FUNCTIONS -----------------------------------------------------
// comparing :: (a -> b) -> (a -> a -> Ordering)
const comparing = f =>
(x, y) => {
const
a = f(x),
b = f(y);
return a < b ? -1 : (a > b ? 1 : 0);
};
// headDef :: [a] -> a
const headDef = xs => xs.length ? xs[0] : undefined;
// show :: Int -> a -> Indented String
// show :: a -> String
const show = (...x) =>
JSON.stringify.apply(
null, x.length > 1 ? [x[1], null, x[0]] : x
);
// INFORMATION ON ACTIVE WINDOWS -----------------------------------------
return show(2,
ObjC.deepUnwrap(
$.CGWindowListCopyWindowInfo(
$.kCGWindowListOptionOnScreenOnly +
$.kCGWindowListExcludeDesktopElements,
$.kCGNullWindowID
)
)
.map(
x => [
'kCGWindowOwnerName',
'kCGWindowName',
'kCGWindowBounds',
'kCGWindowNumber'
].map(k => x[k])
)
.sort(comparing(headDef))
);
})();