使用 JXA 获取所有 运行 个全屏应用程序(或空间)的列表

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

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

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

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

谢谢!

给你:

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

以下是上述答案的更详细版本。

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

/**
 * A JXA script to list all the fullscreen windows.
 * Note: In macOS Mojave this method lists all the maximized windows as well.
 * So we don't know which ones are fullscreen.
 */

ObjC.import('CoreGraphics');

const unwrap = ObjC.deepUnwrap.bind(ObjC);
const getBounds = x => ['X', 'Y', 'Width', 'Height'].map(k => x.kCGWindowBounds[k]);

const windowInfo = unwrap($.CGWindowListCopyWindowInfo($.kCGWindowListOptionAll, $.kCGNullWindowID))
const applicationWindows = windowInfo.filter(x => x.kCGWindowLayer === 0 && x.kCGWindowName)
const menubar = windowInfo.filter(x => x.kCGWindowName === 'Menubar')[0]
const desktop = windowInfo.filter(x => x.kCGWindowName === 'Desktop')[0]

const fullFrameSize = getBounds(desktop);
const results = applicationWindows.filter(x => {
  const windowSize = getBounds(x)
  if (JSON.stringify(windowSize) === JSON.stringify(fullFrameSize)) {
    return true
  }
  return false
}).map(x => ({
  app: x.kCGWindowOwnerName,
  pid: x.kCGWindowOwnerPID,
  winTitle: x.kCGWindowName,
  winInfo: x,
}))

console.log("[DEBUG] results =", JSON.stringify(results, null, 2))