javascript-automation

Get the list of all running fullscreen apps (or spaces) using JXA


Given there are apps running in fullscreen mode, I'm wondering if there's a way to list them using JXA. Something similar to below but for all running fullscreen apps.

var list = Application('System Events').applicationProcesses.where({ backgroundOnly: false }).windows.name();

Use case: I'm trying to create a Alfred workflow to navigate fullscreen apps by name.

Thanks!


Solution

  • Here you go:

    ObjC.import('CoreGraphics');
    
    unwrap = ObjC.deepUnwrap.bind(ObjC);
    
    (function run() {
            const bounds = x => ['X', 'Y', 'Width', 'Height'].map(k => x.kCGWindowBounds[k]);
    
            const windowInfo = unwrap($.CGWindowListCopyWindowInfo(
                                            $.kCGWindowListOptionAll,
                                            $.kCGNullWindowID)),
                  applicationWindows = windowInfo.filter(x => x.kCGWindowLayer==0),
                  menubar = windowInfo.filter(x => x.kCGWindowName=='Menubar')[0],
                  desktop = windowInfo.filter(x => x.kCGWindowName=='Desktop')[0],
                  fullframe = bounds(desktop);
    
            return applicationWindows.filter(x => {
                    return bounds(x).reduce((ξ, y, i) => {
                            return ξ && (y==fullframe[i]);
                    }, true);
            }).map(x => x.kCGWindowOwnerName);
    })();