Skip to content

Instantly share code, notes, and snippets.

@nomeyer
Created February 24, 2016 17:45
Show Gist options
  • Save nomeyer/785504305406ab931c2c to your computer and use it in GitHub Desktop.
Save nomeyer/785504305406ab931c2c to your computer and use it in GitHub Desktop.
Use accessibility to get the focused window of the front most app on OS X
#import <AppKit/AppKit.h>
#import <Foundation/Foundation.h>
#include <CoreFoundation/CoreFoundation.h>
NSRunningApplication* frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
pid_t pid = [frontApp processIdentifier];
AXUIElementRef app = AXUIElementCreateApplication(pid);
AXUIElementRef window;
AXUIElementCopyAttributeValue(app, kAXFocusedWindowAttribute, (CFTypeRef *)&window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment