试图在Mac上获取FocuesApp

时间:2013-06-15 17:27:53

标签: objective-c error-code

我正在尝试构建一个程序,输出我实际选择的应用程序。 但出于某种原因,我得到了AXError:-25204。 根据参考,这个错误暗示我尝试meassage的应用程序是以太忙或没有响应。我不明白为什么......

我在本教程的指导下工作: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;

}

有人有想法吗?

1 个答案:

答案 0 :(得分:0)

我认为您希望使用NSAccessibilitykAXFocusedWindowAttribute kAXSizeAttribute来记录here,而不是{{1}} API。根据我的经验,您不希望混合使用2个API,并且希望使用C API来访问其他应用程序的元素。