Mouse Position on the screen with a trick

HI all,

since holding down apple-shift-4 changes the cursor to display the position on the screen is it possible to copy the coordinates to the clipboard to use elsewhere?

For example to tell another script where to position a window next to another.

Using System events as

tell application “System Events”
keystroke “4” using using {command down, shift down}
end tell

doesnt return anything or it appears and disappear so fast to make it useless …

Thanks

Hi DanWan,

I am not sure, what you are trying to achieve. If your intent is to get the coords of the mouse. You can always do this:

tell application "System Events"
	set mousePosition to position of the mouse
end tell

Hope it helps

Rgds
JaiM

This can be done using cocoa, so if you have the developer tools installed, you can create a command line tool to return the coordinates to applescript. Do this…

  1. create a text file on your desktop called MouseLocation.m with these contents
#import <AppKit/AppKit.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSPoint mouseLoc = [NSEvent mouseLocation]; //get current mouse position
	NSString* locString = [NSString stringWithFormat:@"%.3f, %.3f", mouseLoc.x,mouseLoc.y];
	printf("%s\n", [locString UTF8String]);
	
    [pool drain];
    return 0;
}
  1. open the Terminal and cd to the desktop, then enter this command
gcc -o MouseLocation MouseLocation.m -framework AppKit
  1. you can delete the MouseLocation.m file
  2. in applescript you can use the command line tool like this
set unixExe to (path to desktop folder as text) & "MouseLocation"
do shell script quoted form of POSIX path of unixExe

Hello.

I was so tired of having to type stuff like applescript text item’s delimiters from AS Editor, so I thought I could have a script installed in its contextual menu to do that for me. Then I realized that there were no keyboard shortcut to invoke the contextual menu with.

First of all I looked for something that could actually make the click with the control key. I found cliclick by Carsten Blühm which can be downloaded from here.

Then I discovered this post to obtain the mouse position. Regulus solution works wonderful, I just did a minor change in order to get more "integer like coordinates.

[code]#import <AppKit/AppKit.h>
const char origin[] = “http://www.macscripter.net/viewtopic.php?id=33468 Regulus6633” ;

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSPoint mouseLoc = [NSEvent mouseLocation]; //get current mouse position
NSString* locString = [NSString stringWithFormat:@“%.0f, %.0f”, mouseLoc.x,mouseLoc.y];
printf(“%s\n”, [locString UTF8String]);

[pool drain];
return 0;
}[/code]
Then I created this AppleScript, which I have installed as a short key cut with Spark which can be downloaded from here

You will have to change the hardcoded paths to what suits your installation policy.


-- macscripter.net/viewtopic.php?pid=132022#p132022 McUsr
set mouseLoc to (do shell script "/usr/local/opt/MouseLocation")
set {astid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
tell mouseLoc to set {mouseX, mouseY} to {"c" & it's text item 1, it's text item 2}
set mouseY to 1200 - (mouseY as integer)
set mouseLoc to mouseX & " " & mouseY
do shell script "/usr/local/opt/cliclick " & mouseLoc
set AppleScript's text item delimiters to astid

I have assigned it to ctrl opt cmd space Since all other combinations with space is taken. Logically it should really have been put on ctrl space, as you simulate a mouse click with a space, but there may be both quicksilver and spotlight on these places so.

Edit: It appears to be some discrepancies with the mouse position given of the mouse location returned by MouseLocation, so I wonder What I will have to do to adjust the geometry, and if this is possible.
It looks like the Y coordinate is put some 400 pixels below its original position, this may have something to do with the short cut I use, as it changes to crosshairs. I’ll update this when I have investigated it more thoroughly

Hi McUsr, I’m glad you got some use out of it. :cool:
Note, I posted MouseLocation on my website to make it easier for people… it’s already compiled… anyone can get it from here.

Hello Hank, it is cool. :cool:
Thanks!

The problem I had with the coordinates and the script, was that using opt made the editor use block editing mode, aka the crosshair mouse cursor. The other problem was that the position is measured from the bottom for the y coordinate.

I have hardcoded my screen resolution into the script, which has a depth of 1200 pixels.
It is as such just a hack, but a useful hack if you’d like to invoke the contextual menu from the keyboard. :slight_smile:
Now I use shift cmd space to invoke it.


set mouseLoc to (do shell script "/usr/local/opt/MouseLocation")
set {astid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
tell mouseLoc to set {mouseX, mouseY} to {"c" & it's text item 1, it's text item 2}
set mouseY to 1200 - (mouseY as integer)
set mouseLoc to mouseX & " " & mouseY
do shell script "/usr/local/opt/cliclick " & mouseLoc
set AppleScript's text item delimiters to astid

You will need cliclick and MouseLocation, and some shortcut utility, all which are referenced in the posts above in order to make it to work.

FYI: I know about this. Actually I’m working on another tool called MouseTools which allows you to do many things with the mouse, including getting mouse location, clicking at a location, clicking with modifier keys and a few other things. It’s mostly done but needs a few more minor adjustments.

The reason I mention that is I have code in the new tool for getting the mouse location from the upper left corner instead of the bottom left. You can have that now if you wish. Here’s that code so just replace the code and rebuild the tool.
Replace…

NSPoint mouseLoc = [NSEvent mouseLocation]; //get current mouse position
NSString* locString = [NSString stringWithFormat:@"%.0f, %.0f", mouseLoc.x,mouseLoc.y];
printf("%s\n", [locString UTF8String]);

With…

CGEventRef mouseEvent = CGEventCreate(NULL);
CGPoint mouseLoc = CGEventGetLocation(mouseEvent);
NSString* locString = [NSString stringWithFormat:@"%.0f, %.0f", (float)mouseLoc.x, (float)mouseLoc.y];
printf("%s\n", [locString UTF8String]);
CFRelease(mouseEvent);

NOTE: in your applescript you never reset text item delimiters to astid.

Hello Hank.

Corrected the code to reset AppleScript text item delimiters, Thanks.

I have to think about using the new tool, I really think you should have a switch for that, for System events’s windows for instance uses positions relative to the bottom of the screen, where as Finder uses positions from the upper left, at least it is so in Snow Leopard.

The switch is in the new tool so there will be one! :smiley: