我一直无法订阅当前窗口的变化,但你可以问无障碍API当前应用程序,而目前应用最前台窗口。
假设你有一个名为CurrentAppData类,具有下列数据:
@interface CurrentAppData : NSObject {
NSString* _title;
AXUIElementRef _systemWide;
AXUIElementRef _app;
AXUIElementRef _window;
}
的代码,以查找当前的应用程序看起来是这样的:
-(void) updateCurrentApplication {
// get the currently active application
_app = (AXUIElementRef)[CurrentAppData
valueOfExistingAttribute:kAXFocusedApplicationAttribute
ofUIElement:_systemWide];
// Get the window that has focus for this application
_window = (AXUIElementRef)[CurrentAppData
valueOfExistingAttribute:kAXFocusedWindowAttribute
ofUIElement:_app];
NSString* appName = [CurrentAppData descriptionOfValue:_window
beingVerbose:TRUE];
[self setTitle:appName];
}
在这个例子中_systemWide变量初始化在类初始化函数为: _system = AXUIElementCreateSystemWide();
类功能valueOfExistingAttribute看起来是这样的:
// -------------------------------------------------------------------------------
// valueOfExistingAttribute:attribute:element
//
// Given a uiElement and its attribute, return the value of an accessibility
// object's attribute.
// -------------------------------------------------------------------------------
+ (id)valueOfExistingAttribute:(CFStringRef)attribute ofUIElement:(AXUIElementRef)element
{
id result = nil;
NSArray *attrNames;
if (AXUIElementCopyAttributeNames(element, (CFArrayRef *)&attrNames) == kAXErrorSuccess)
{
if ([attrNames indexOfObject:(NSString *)attribute] != NSNotFound
&&
AXUIElementCopyAttributeValue(element, attribute, (CFTypeRef *)&result) == kAXErrorSuccess
)
{
[result autorelease];
}
[attrNames release];
}
return result;
}
先前的功能是从苹果UIElementInspector例子,这也是学习的辅助功能API一个很好的资源拍摄。