views:

105

answers:

2

hi developers, how can i get current focused window id using objective c, the return value should be an int, help is highly appreciated! I just need to get the following line working

CGImageRef windowImage = CGWindowListCreateImage(CGRectNull, kCGWindowListOptionIncludingWindow, winNum, kCGWindowImageBoundsIgnoreFraming);

but no matter which window that i focus at, winNum is always 0.

+3  A: 
NSWindow *keyTarget = [[NSApplication sharedApplication] keyWindow];
NSInteger winNum = [keyTarget windowNumber];

or in one line:

[[[NSApplication sharedApplication] keyWindow] windowNumber];
Kenny Winker
Perfect username + icon.
nall
A: 

i have tried it out, but no matter which window that i focus at, winNum is always 0, any idea or am i missing anything? I just need to get the following line working

CGImageRef windowImage = CGWindowListCreateImage(CGRectNull, kCGWindowListOptionIncludingWindow, winNum, kCGWindowImageBoundsIgnoreFraming);

*please note that i'm not trying to get the window id of my own application. it should be an application that i'm focusing at

Daniel
That's because keyWindow is only looking at windows in your application. Check this question for leads: http://stackoverflow.com/questions/891801/cocoa-api-to-grab-other-applications-window-document-name
Kenny Winker
This is a question-and-answer site. You posted this as an answer to your question, but it's obviously not an answer. If you want to add to your question, just edit it.
Chuck