7

对于我的代码,我试图AXMenuItemsAXMenu( AXUIElementRef) 中获取一个数组。菜单成功记录,这是我的代码:

NSArray *anArray = [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.dock"];
AXUIElementRef anAXDockApp = AXUIElementCreateApplication([[anArray objectAtIndex:0] processIdentifier]);

CFTypeRef aChildren;
AXUIElementCopyAttributeValue(anAXDockApp, kAXChildrenAttribute, &aChildren);

SafeCFRelease(anAXDockApp);

CFTypeRef aMenu = CFArrayGetValueAtIndex(aChildren, 0);

NSLog(@"aMenu: %@", aMenu);

                    // Get menu items
CFTypeRef aMenuChildren;
AXUIElementCopyAttributeValue(aMenu, kAXVisibleChildrenAttribute, &aMenuChildren);

for (NSInteger i = 0; i < CFArrayGetCount(aMenuChildren); i++) {

    AXUIElementRef aMenuItem = [self copyAXUIElementFrom:aMenu role:kAXMenuItemRole atIndex:i];

    NSLog(@"aMenuItem: %@", aMenuItem); // logs (null)

    CFTypeRef aTitle;
    AXUIElementCopyAttributeValue(aMenuItem, kAXTitleAttribute, &aTitle);


    if ([(__bridge NSString *)aTitle isEqualToString:@"New Window"] || [(__bridge NSString *)aTitle isEqualToString:@"New Finder Window"]) /* Crashes here (i can see why)*/{

        AXUIElementPerformAction(aMenuItem, kAXPressAction);

        [NSThread sleepForTimeInterval:1];

        break;

    }

}

获取列表的正确方法是AXMenuItems什么?

可访问性检查器的屏幕截图:

检查员

4

2 回答 2

20

我已经找到了一个答案,使用@Willeke的答案AXUIElementCopyElementAtPosition()来获取菜单。由于有多个停靠方向和隐藏,我不得不在 .h 文件中创建枚举,因为它比 0、1 或 2 更容易阅读。

// .h
typedef enum {
    kDockPositionBottom,
    kDockPositionLeft,
    kDockPositionRight,
    kDockPositionUnknown
} DockPosition;

typedef enum {
    kDockAutohideOn,
    kDockAutohideOff
} DockAutoHideState;

然后,我添加了获取这些状态的方法.m

// .m
- (DockPosition)dockPosition
{
    NSRect screenRect = [[NSScreen mainScreen] frame];

    NSRect visibleRect = [[NSScreen mainScreen] visibleFrame];

    // Dont need to remove menubar height
    visibleRect.origin.y = 0;

    if (visibleRect.origin.x > screenRect.origin.x) {
        return kDockPositionLeft;
    } else if (visibleRect.size.width < screenRect.size.width) {
        return kDockPositionRight;
    } else if (visibleRect.size.height < screenRect.size.height) {
        return kDockPositionBottom;
    }
    return kDockPositionUnknown;
}

- (DockAutoHideState)dockHidden
{
    NSString *plistPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Preferences/com.apple.dock.plist"];
    NSDictionary *dockDict = [NSDictionary dictionaryWithContentsOfFile:plistPath];

    CFBooleanRef autohide = CFDictionaryGetValue((__bridge CFDictionaryRef)dockDict, @"autohide");
    if (CFBooleanGetValue(autohide) == true) {
        return kDockAutohideOn;
    }
    return kDockAutohideOff;
}

对于未知的停靠位置,我添加了以防它无法从屏幕位置计算它。

然后,我使用了一种方法从菜单栏中获取停靠项:

- (AXUIElementRef)getDockItemWithName:(NSString *)name
{
    NSArray *anArray = [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.dock"];
    AXUIElementRef anAXDockApp = AXUIElementCreateApplication([[anArray objectAtIndex:0] processIdentifier]);

    AXUIElementRef aList = [self copyAXUIElementFrom:anAXDockApp role:kAXListRole atIndex:0];

    CFTypeRef aChildren;
    AXUIElementCopyAttributeValue(aList, kAXChildrenAttribute, &aChildren);
    NSInteger itemIndex = -1;

    for (NSInteger i = 0; i < CFArrayGetCount(aChildren); i++) {

        AXUIElementRef anElement = CFArrayGetValueAtIndex(aChildren, i);

        CFTypeRef aResult;

        AXUIElementCopyAttributeValue(anElement, kAXTitleAttribute, &aResult);

        if ([(__bridge NSString *)aResult isEqualToString:name]) {

            itemIndex = i;
        }
    }
    SafeCFRelease(aChildren);

    if (itemIndex == -1) return nil;

    // We have index now do something with it

    AXUIElementRef aReturnItem = [self copyAXUIElementFrom:aList role:kAXDockItemRole atIndex:itemIndex];
    SafeCFRelease(aList);
    return  aReturnItem;
}

这个SafeCFRelease()方法是一个非常简单的方法,它检查传递的值是否不是 nil,然后释放(之前有一些问题)。

void SafeCFRelease( CFTypeRef cf )
{
    if (cf) CFRelease(cf);
}

这种方法[copyAXUIElementFrom: role: atIndex:]@Willeke的一种方法,它回答了我的另一个问题:

- (AXUIElementRef)copyAXUIElementFrom:(AXUIElementRef)theContainer role:(CFStringRef)theRole atIndex:(NSInteger)theIndex {
    AXUIElementRef aResultElement = NULL;
    CFTypeRef aChildren;
    AXError anAXError = AXUIElementCopyAttributeValue(theContainer, kAXChildrenAttribute, &aChildren);
    if (anAXError == kAXErrorSuccess) {
        NSUInteger anIndex = -1;
        for (id anElement in (__bridge NSArray *)aChildren) {
            if (theRole) {
                CFTypeRef aRole;
                anAXError = AXUIElementCopyAttributeValue((__bridge AXUIElementRef)anElement, kAXRoleAttribute, &aRole);
                if (anAXError == kAXErrorSuccess) {
                    if (CFStringCompare(aRole, theRole, 0) == kCFCompareEqualTo)
                        anIndex++;
                    SafeCFRelease(aRole);
                }
            }
            else
                anIndex++;
            if (anIndex == theIndex) {
                aResultElement = (AXUIElementRef)CFRetain((__bridge CFTypeRef)(anElement));
                break;
            }
        }
        SafeCFRelease(aChildren);
    }
    return aResultElement;
}

使用所有这些代码,我将其放入我的一种方法中:

// 检查是否在dock中(否则不能这样做)

                if ([self isAppOfNameInDock:[appDict objectForKey:@"AppName"]]) {

                    // Get dock item

                    AXUIElementRef aDockItem = [self getDockItemWithName:[appDict objectForKey:@"AppName"]];

                    AXUIElementPerformAction(aDockItem, kAXShowMenuAction);

                    [NSThread sleepForTimeInterval:0.5];

                    CGRect aRect;

                    CFTypeRef aPosition;
                    AXUIElementCopyAttributeValue(aDockItem, kAXPositionAttribute, &aPosition);
                    AXValueGetValue(aPosition, kAXValueCGPointType, &aRect.origin);
                    SafeCFRelease(aPosition);

                    CFTypeRef aSize;
                    AXUIElementCopyAttributeValue(aDockItem, kAXSizeAttribute, &aSize);
                    AXValueGetValue(aSize, kAXValueCGSizeType, &aRect.size);
                    SafeCFRelease(aSize);

                    SafeCFRelease(aDockItem);

                    CGPoint aMenuPoint;

                    if ([self dockHidden] == kDockAutohideOff) {
                        switch ([self dockPosition]) {
                            case kDockPositionRight:
                                aMenuPoint = CGPointMake(aRect.origin.x - 18, aRect.origin.y + (aRect.size.height / 2));
                                break;
                            case kDockPositionLeft:
                                aMenuPoint = CGPointMake(aRect.origin.x + aRect.size.width + 18, aRect.origin.y + (aRect.size.height / 2));
                                break;
                            case kDockPositionBottom:
                                aMenuPoint = CGPointMake(aRect.origin.x + (aRect.size.width / 2), aRect.origin.y - 18);
                                break;
                            case kDockPositionUnknown:
                                aMenuPoint = CGPointMake(0, 0);
                                break;
                        }
                    } else {

                        NSRect screenFrame = [[NSScreen mainScreen] frame];

                        switch ([self dockPosition]) {
                            case kDockPositionRight:
                                aMenuPoint = CGPointMake(screenFrame.size.width - 18, aRect.origin.y + (aRect.size.height / 2));
                                break;
                            case kDockPositionLeft:
                                aMenuPoint = CGPointMake(screenFrame.origin.x + 18, aRect.origin.y + (aRect.size.height / 2));
                                break;
                            case kDockPositionBottom:
                                aMenuPoint = CGPointMake(aRect.origin.x + (aRect.size.width / 2), screenFrame.size.height - 18);
                                break;
                            case kDockPositionUnknown:
                                aMenuPoint = CGPointMake(0, 0);
                                break;
                        }
                    }

                    if ((aMenuPoint.x != 0) && (aMenuPoint.y != 0)) {

                        AXUIElementRef _systemWideElement = AXUIElementCreateSystemWide();

                        AXUIElementRef aMenu;
                        AXUIElementCopyElementAtPosition(_systemWideElement, aMenuPoint.x, aMenuPoint.y, &aMenu);

                        SafeCFRelease(_systemWideElement);

                        // Get menu items
                        CFTypeRef aMenuChildren;
                        AXUIElementCopyAttributeValue(aMenu, kAXVisibleChildrenAttribute, &aMenuChildren);

                        NSRunningApplication *app = [[NSRunningApplication runningApplicationsWithBundleIdentifier:[appDict objectForKey:@"BundleID"]] objectAtIndex:0];

                        for (NSInteger i = 0; i < CFArrayGetCount(aMenuChildren); i++) {

                            AXUIElementRef aMenuItem = [self copyAXUIElementFrom:aMenu role:kAXMenuItemRole atIndex:i];

                            CFTypeRef aTitle;
                            AXUIElementCopyAttributeValue(aMenuItem, kAXTitleAttribute, &aTitle);

                            // Supports chrome, safari, and finder
                            if ([(__bridge NSString *)aTitle isEqualToString:@"New Window"] || [(__bridge NSString *)aTitle isEqualToString:@"New Finder Window"]) {

                                AXUIElementPerformAction(aMenuItem, kAXPressAction);

                                NSInteger numberOfWindows = [self numberOfWindowsOpenFromApplicationWithPID:[app processIdentifier]];
                                // Wait until open
                                while ([self numberOfWindowsOpenFromApplicationWithPID:[app processIdentifier]] <= numberOfWindows) {
                                }
                                break;
                            }
                        }
                        SafeCFRelease(aMenu);
                        SafeCFRelease(aMenuChildren);
                    }
                }

这很复杂,但它确实有效。我可能无法解释它,但我已经对这段代码进行了压力测试,它工作得很好。

于 2016-03-20T14:50:27.610 回答
1

对“如何从 AXMenu 获取 AXMenuItems 数组?”问题的回答:aMenuChildren是菜单项列表。确保您可以按角色过滤kAXMenuItemRole

回答“为什么它不起作用?”这个问题:菜单不是菜单。当您在 Axccessibility Inspector 中检查菜单时,它会显示警告“父元素不会将元素报告为其子元素之一”。Dock 应用程序有一个子项,即一个停靠项列表。

于 2016-03-06T13:42:52.867 回答