有很多软件可以进行窗口管理。您可以查看我一直在破解的名为 Amethyst 的平铺窗口管理器。此类软件背后的基本思想依赖于可访问性(您可以找到 here 的文档)。作为快速概览,API 通过获取对具有属性(隐藏、位置、大小等)的可访问性元素(应用程序、窗口、按钮、文本字段等)的引用来工作,其中一些是可写的。
例如,假设您想将每个正在运行的应用程序中的所有窗口移动到屏幕的左上角。该代码可能看起来像
for (NSRunningApplication *runningApplication in [[NSWorkspace sharedWorkspace] runningApplications]) {
AXUIElementRef applicationRef = AXUIElementCreateApplication([runningApplication processIdentifier]);
CFArrayRef applicationWindows;
AXUIElementCopyAttributeValues(applicationRef, kAXWindowsAttribute, 0, 100, &applicationWindows);
if (!applicationWindows) continue;
for (CFIndex i = 0; i < CFArrayGetCount(applicationWindows); ++i) {
AXUIElementRef windowRef = CFArrayGetValueAtIndex(applicationWindows, i);
CGPoint upperLeft = { .x = 0, .y = 0 };
AXValueRef positionRef = AXValueCreate(kAXValueCGPointType, &upperLeft);
AXUIElementSetAttributeValue(windowRef, kAXPositionAttribute, positionRef);
}
}
这说明了如何获取对应用程序及其窗口的引用、如何从辅助功能元素复制属性以及如何设置辅助功能元素的属性。
NSWorkspace 中记录了各种通知,用于启动和终止应用程序,辅助功能框架还具有通知意义,例如应用程序创建或销毁窗口,或窗口小型化或小型化。
对窗口的更改进行动画处理并非易事,我还没有弄清楚如何做到这一点,尽管它可能是可能的。如果不使用私有 API,可能根本不可能。但是您列出的其他事情应该是可能的。例如,可以通过在应用程序可访问性元素上设置kAXHiddenAttribute 来隐藏应用程序。启动应用程序实际上可以通过-[NSWorkspace launchApplication:] 完成。
请注意,使用辅助功能需要用户在System Preferences > Accessibility 中打开Enable access for assistive devices。