Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get window values under mouse

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

like image 374
Panos Karampis Avatar asked Dec 20 '14 22:12

Panos Karampis


Video Answer


1 Answers

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
like image 157
Panos Karampis Avatar answered Oct 20 '22 04:10

Panos Karampis