Cocoa - Get window ID on click, as used in Grab

Discussion in 'Mac Programming' started by JBurke, May 4, 2009.

  1. JBurke macrumors newbie

    May 4, 2009
    Hey everyone, I have been searching for this answer and I realized that I don't even really know what to search for. Here's what I'm trying to do, and maybe someone can give me at least what to search for, if not the answer itself!

    I'm building a Cocoa app right now in which I want to be able to click any window that is open and get the process serial or any kind of window ID number that I can compare to other windows. This is similar to the functionality of the Grab screen-shot utility that comes with OS X.

    If you open Grab, then select Capture->Window, it comes up with a message box and you click "Choose Window", and the cursor becomes a camera, and any window you click while it is a camera is what Grab takes the screenshot of. I want to be able to click a window and return its ID. Any thoughts??

    Any and all help is greatly appreciated!
  2. kainjow Moderator emeritus


    Jun 15, 2000
    This should be doable with the CGWindow API in Leopard (spotlight "CGWindow.h"). You'll have to loop through all the windows, and find the top one whose bounds contains the mouse location.
  3. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    Worst case scenario you could ktrace grab and see what sorts of things it's doing when you take a screenshot with it. It may not be using public APIs, but you should be able to see the calls it's making and look any up that look "interesting".

  4. JBurke thread starter macrumors newbie

    May 4, 2009
    @kainjow, thanks alot! I will check that out and see if it can do exactly what I need. Just reading glancing over that header file makes me think it will do nicely.

    @lee1210, I'll definitely keep that in mind, and that's where I'm going if CGWindow doesn't exactly cut it. Thanks again for the speedy help!

Share This Page