5

I would like to create a system tool / application which has the capacity to aid in window management. I'm trying to find documentation about the following topics, if they are indeed possible given the security sandboxing of OSX.

  • Show a list of running applications with the name & icon, and allow the user to choose one
  • Manipulate the frame(s) of said application's windows (eg, resize, reposition) from my app (with animations -- though I assume this will be trivial once I can perform the actual change)
  • Hide or show these applications from task managers, etc.
  • Be able to launch (or terminate) instances of the given application

It seems to me that Quicksilver accomplishes many of these things, but the lack of AppStore availability makes me wonder if it possible to do this while remaining in the OSX sandbox.

4

1 回答 1

10

有很多软件可以进行窗口管理。您可以查看我一直在开发的名为Amethyst的平铺窗口管理器。此类软件背后的基本理念依赖于可访问性(您可以在此处找到相关文档)。作为快速概述,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:].

请注意,使用辅助功能需要用户Enable access for assistive devices打开System Preferences > Accessibility.

于 2013-06-09T15:23:08.647 回答