Window coordinates to screen coordinates

I’m looking for a way to transform location coordinates from a window’s coord system to the screen’s coord system. Any ideas?

Hey Charlie,

That doesn’t make obvious sense. Can you be more specific about what you’re doing?

It can be done. See this thread for some ideas:

Show current window dimensions

Once you have both window and screen coordinates it’s just math.

-Chris

There is an application called ‘cliClick’ which will move the mouse using command line input. It works using screen coordinates as input. I have an application which chases the of a location in the window that moves around depending on choices that the user makes but it gets the mouse’s location in window coordinates. Thus I must convert the window coords to screen coords in order to use cliClick to move the mouse. I have tried every technique that I have found on the web including getting the screen coords of the lower left corner of the window and adding the window coords to them to get the location in screen coors but it doesn’t work.

Hey Charlie,

What app?

I'm familiar with cliclick, but why do you need it when you have Keyboard Maestro?

Please review your question from the standpoint of someone who has never seen your issue.

You haven't given us a macro to test or nearly enough information about your process to be able to help you.

-Chris

The application is one I have written in Objective C using Cocoa. It
implements a 4 dimensional Rubik cube. The individual tessies (pieces of a
tesseract, the 4th geometric dimension embodiment of a rubik hypercube)
move around when the ‘faces’ are twisted. The user can click on a tessie in
order to see where it has moved to. I use a popover to display the name of
the location and I would like to move the mouse pointer to that location so
that continuation of the permutation can more easily be followed.

Whether done using Cocoa methods or cliclick (cliclick is easier to
implement) I need to convert window coords to screen coords and using the
Cocoa transformation methods don’t do the job. It is likely my technical
prowess is insufficient to use the Cocoa methods so I am seeking 'outside’
means of accomplishing my goal.

I think it is pretty straight-forward.

Assume:

  1. Left,Top corner is the origin point for all window coordinates, and is known in screen coordinates.

So,
Xscreen = WinLeft + Xwin
Yscreen = WinTop + Ywin

Window Left,Top in Screen Coord = KM Token %FrontWindowPosition%

Questions?

That does not work. The screenX coord is ok but the screen Y coord is way
off the bottom of the screen.

I believe the the screen coord origin is at the lower left of the screen
with the y coord going negative going up but assuming this still gets the y
coord wrong.

I know I am doing something wrong but I can not figure out what. I am
giving up.

We can't be of much help unless you reveal (post) the details of your calculations and macro. Have you compared the coordinates of the Window as provided by the app with those provided by the KM token %FrontWindowFrame% ?

I’ve finally figured it out!!!
The y coordinate GETS SMALLER as you go UP in a window and you have to take the menubar height into account.

The following method positions the mouse pointer at the lower left corner of the input NSRect. The input rect is in window coords.

  • (void) clickPoint: (NSRect) rect {
    NSRect windowFrame = [[self window] frame];
    PRINT_RECT(“windowFrame”, windowFrame);
    NSRect contentRect = [[self window] contentRectForFrameRect: windowFrame];
    PRINT_RECT(“contentRect”, contentRect);

    float contentHeight = contentRect.size.height;
    float menubarHeight = windowFrame.size.height - contentHeight;
    printf("\n menubarHeight = %.1f", menubarHeight);

    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSString *filePath = [NSString stringWithFormat: @"%@", path];
    NSString *cPath = [filePath stringByAppendingString:
    @"/Contents/Resources/cliclick"];
    NSMutableString *command = [NSMutableString stringWithString: cPath];
    [command appendFormat: @" m:%d,%d",
    (int) (windowFrame.origin.x + (rect.origin.x)),
    (int) (windowFrame.origin.y - (rect.origin.y + rect.size.height + menubarHeight))];
    system(STRING(command));
    }