1

我正在尝试构建一个输出我实际选择的应用程序的程序。但由于某种原因,我得到了 AXError:-25204 。根据参考资料,此错误意味着我尝试消息的应用程序太忙或没有响应。我不明白为什么...

我在本教程的指导下工作:http: //cocoatutorial.grapewave.com/2010/01/retrieving-the-window-that-has-focus/

#import <Foundation/Foundation.h>
#import <AppKit/NSAccessibility.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

if(!AXAPIEnabled()){
    NSLog(@"AXAPI not Enabled");
    return 0;
}

AXUIElementRef _systemWideElement;
AXUIElementRef _focusedApp;
CFTypeRef _focusedWindow;
CFTypeRef _position;
CFTypeRef _size;
AXError e;


//Get the window that has the focus
//Get the app that has the focus
_systemWideElement = AXUIElementCreateSystemWide();
NSLog(@"%d",_systemWideElement);
e=AXUIElementCopyAttributeValue(_systemWideElement,
                              (CFStringRef)kAXFocusedApplicationAttribute,
                              (CFTypeRef*)&_focusedApp);
NSLog(@"%d,%d",kAXErrorSuccess,e);

e=AXUIElementCopyAttributeValue((AXUIElementRef)_focusedApp,(CFStringRef)NSAccessibilityFocusedWindowAttribute,(CFTypeRef*)&_focusedWindow);
if( e == kAXErrorSuccess) {
    NSLog(@"App ausgewaehlt");

    if(CFGetTypeID(_focusedWindow) == AXUIElementGetTypeID()) {
        //Get the Window's Current Position
        if(AXUIElementCopyAttributeValue((AXUIElementRef)_focusedWindow,
                                         (CFStringRef)NSAccessibilityPositionAttribute,
                                         (CFTypeRef*)&_position) != kAXErrorSuccess) {
            NSLog(@"Can't Retrieve Window Position");
        }
        //Get the Window's Current Size
        if(AXUIElementCopyAttributeValue((AXUIElementRef)_focusedWindow,
                                         (CFStringRef)NSAccessibilitySizeAttribute,
                                         (CFTypeRef*)&_size) != kAXErrorSuccess) {
            NSLog(@"Can't Retrieve Window Size");
        }
    }
    //NSLog(@"%@",_size);

}else {
    NSLog(@"%d",e);
}
[pool drain];
return 0;

}

有人有想法吗?

4

1 回答 1

0

而不是NSAccessibility我相信您想要使用的 API 东西,kAXFocusedWindowAttribute并且可能在此处kAXSizeAttribute记录。根据我的经验,您不想混合使用这 2 个 API,而是希望使用 C API 来访问其他应用程序的元素。

于 2013-07-24T17:14:00.090 回答