使用Cocoa的Accessibility API获取应用程序停靠图标的位置

时间:2011-07-13 19:11:01

标签: cocoa accessibility dock

如何使用辅助功能API获取应用程序停靠图标的位置?

2 个答案:

答案 0 :(得分:6)

发现它!使用this forum post作为参考,我能够将给定的示例代码塑造为我需要的内容:

- (NSArray *)subelementsFromElement:(AXUIElementRef)element forAttribute:(NSString *)attribute
{
    NSArray *subElements = nil;
    CFIndex count = 0;
    AXError result;

    result = AXUIElementGetAttributeValueCount(element, (CFStringRef)attribute, &count);
    if (result != kAXErrorSuccess) return nil;
    result = AXUIElementCopyAttributeValues(element, (CFStringRef)attribute, 0, count, (CFArrayRef *)&subElements);
    if (result != kAXErrorSuccess) return nil;

    return [subElements autorelease];
}

- (AXUIElementRef)appDockIconByName:(NSString *)appName
{
    AXUIElementRef appElement = NULL;

    appElement = AXUIElementCreateApplication([[[NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.dock"] lastObject] processIdentifier]);
    if (appElement != NULL)
    {
        AXUIElementRef firstChild = (__bridge AXUIElementRef)[[self subelementsFromElement:appElement forAttribute:@"AXChildren"] objectAtIndex:0];
        NSArray *children = [self subelementsFromElement:firstChild forAttribute:@"AXChildren"];
        NSEnumerator *e = [children objectEnumerator];
        AXUIElementRef axElement;
        while (axElement = (__bridge AXUIElementRef)[e nextObject])
        {
            CFTypeRef value;
            id titleValue;
            AXError result = AXUIElementCopyAttributeValue(axElement, kAXTitleAttribute, &value);
            if (result == kAXErrorSuccess)
            {
                if (AXValueGetType(value) != kAXValueIllegalType)
                    titleValue = [NSValue valueWithPointer:value];
                else
                    titleValue = (__bridge id)value; // assume toll-free bridging
                if ([titleValue isEqual:appName]) {
                    return axElement;
                }
            }
        }
    }

    return nil;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    AXUIElementRef dockIcon = [self appDockIconByName:@"MYAPPNAME"];
    if (dockIcon) {
        CFTypeRef value;
        CGPoint iconPosition;
        AXError result = AXUIElementCopyAttributeValue(dockIcon, kAXPositionAttribute, &value);
        if (result == kAXErrorSuccess)
        {
            if (AXValueGetValue(value, kAXValueCGPointType, &iconPosition)) {
                NSLog(@"position: (%f, %f)", iconPosition.x, iconPosition.y);
            }
        }
    }
}

答案 1 :(得分:1)

对于Mac OS El Capitan,看起来您不应该使用Accessibility API获取图标的位置。问题是该图标不在应用程序的辅助功能对象层次结构中 - 它可以在系统Dock应用程序的层次结构中找到。沙盒应用不应该访问其他应用的辅助功能对象。

批准答案中的代码不会在控制台中产生sandboxd守护程序的任何警告,看起来它不违反任何规则。它使用函数AXUIElementCreateApplication创建顶级辅助功能对象。文档说明:

Creates and returns the top-level accessibility object for the
application with the specified process ID.

不幸的是,这个顶级对象不是Dock图标的祖先。 我试图运行代码,它计算第一个应用程序的主菜单项(与应用程序本身具有相同标题)的位置。比较发生在这一行:

if ([titleValue isEqual:appName]) {

因此我的应用的输出始终相同:

position: (45.000000, 0.000000)

尝试访问其他应用程序的辅助功能对象时,在控制台中发出警告。我想还有另一种计算图标位置的方法。