I am playing with Cocoa/Objective-C and I would like to ask you if it is possible to get window information such as pid, window name from a window that is inactive. What I precisely mean is, in the case there are two non full screen (nor maximized) windows A and B of different tasks, lets say Chrome(A) and Firefox(B), with active window A and mouse cursor above window B, can I get such info without having to click into window B and bring it in foreground?
I have noticed that for example scrolling in an inactive window scrolls the context as if it was in foreground, so my guess is that this is doable.
Any tips would be really welcome.
Thank you
I will answer my own question: It possible with accessibility api & carbon a) register a wide event:
AXUIElementRef _systemWideElement = AXUIElementCreateSystemWide();
b)convert carbon to screen point
- (CGPoint)carbonScreenPointFromCocoaScreenPoint:(NSPoint)cocoaPoint {
NSScreen *foundScreen = nil;
CGPoint thePoint;
for (NSScreen *screen in [NSScreen screens]) {
if (NSPointInRect(cocoaPoint, [screen frame])) {
foundScreen = screen;
}
}
if (foundScreen) {
CGFloat screenMaxY = NSMaxY([foundScreen frame]);
thePoint = CGPointMake(cocoaPoint.x, screenMaxY - cocoaPoint.y - 1);
} else {
thePoint = CGPointMake(0.0, 0.0);
}
return thePoint;
}
c) get process under mouse
NSPoint cocoaPoint = [NSEvent mouseLocation];
if (!NSEqualPoints(cocoaPoint, _lastMousePoint)) {
CGPoint pointAsCGPoint = [self carbonScreenPointFromCocoaScreenPoint:cocoaPoint];
AXUIElementRef newElement = NULL;
if (AXUIElementCopyElementAtPosition( _systemWideElement, pointAsCGPoint.x, pointAsCGPoint.y, &newElement ) == kAXErrorSuccess) {
NSLog(@"%@",newElement);
}
_lastMousePoint = cocoaPoint;
}
Credits to https://developer.apple.com/library/mac/samplecode/UIElementInspector/Introduction/Intro.html
nslog gives something like <AXUIElement 0x6000000583c0> {pid=39429}
ps aux | grep 39429
:39429 0.2 5.5 5109480 916500 ?? U 1:57PM 3:34.67 /Applications/Xcode.app/Contents/MacOS/Xcode
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With