首页 > 解决方案 > 使用 JXA 获取所有正在运行的全屏应用程序(或空间)的列表

问题描述

鉴于有应用程序以全屏模式运行,我想知道是否有办法使用 JXA 列出它们。类似于下面的内容,但适用于所有正在运行的全屏应用程序。

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

用例:我正在尝试创建一个 Alfred 工作流来按名称导航全屏应用程序。

谢谢!

标签: javascript-automation

解决方案


干得好:

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);
})();

推荐阅读