Mouse coordinates

Is there any way i can get mouse coordinates?

MacBook Pro, Mac OS X (10.6.1)

Posted on May 6, 2010 12:15 AM

Reply
Question marked as Top-ranking reply

Posted on May 6, 2010 9:35 AM

It does not use an AppleScript, Shell Script or anything else, but if you are just looking to get the coordinates of the mouse on the screen, +command, shift 4+ will give you the coordinates. Then press escape when you have them.
6 replies

May 6, 2010 8:31 AM in response to Cande

Like Craig mentioned, you'll have to use " Xcode" in order to achieve this. Here are some basic instructions for building an app that will get the current x and y locations of your mouse...




1) Launch " Xcode"

2) Click "*Create a new Xcode project*"

3) Select "*Cocoa-AppleScript Application*" and click " Choose…"

4) Give your new app a name, choose where to save it, and click " Save"

5) Click the disclosure triangle located to the left of the folder named " Classes"

6) Double click the " YourAppNameDelegate.applescript" file to open it in a new editor window

7) Replace the code that reads…

<pre style="width:630px;height:auto;overflow-x:auto;overflow-y:hidden;"
title="Copy this code and paste it into your Script Editor application.">
property parent : class "NSObject"
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
</pre>

with this code…

<pre style="width:630px;height:auto;overflow-x:auto;overflow-y:hidden;"
title="Copy this code and paste it into your Script Editor application.">
property parent : class "NSObject"
property pNSEvent : class "NSEvent"
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened
my performSelectorwithObject_afterDelay("getMouseXY", missing value, 0)
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
on getMouseXY()
set mouseLocation to pNSEvent's mouseLocation() as list
set xMouse to item 1 of mouseLocation as integer
set yMouse to item 2 of mouseLocation as integer
display dialog "X=" & xMouse & ", Y=" & yMouse giving up after 1
my performSelectorwithObject_afterDelay("getMouseXY", missing value, 0.1)
end getMouseXY
</pre>

8) Press *Command + S* to save the script and then press *Command + R* to compile and run your new app.




After Xcode compiles and runs your new app, all you will see is a generic window. This window can be modified using "*Interface Builder*", but I won't go into how to do that right now. What you will notice the app doing is presenting you with a dialog over and over and over again until you click cancel or quit the app. This dialog will tell you the x and y location of your mouse.

Hope this helps...

May 6, 2010 8:40 AM in response to Chachi

Hi Chachi,

Actually, there are compiled command line utilities available for download at that link. I put the code there so viewers can see how easy it is to do. The downloads also have the source projects included.

Also, NSEvent only works if you have an application. If the goal is to get mouse coordinates as part of a workflow or as a single event (like from the terminal), then the cli is best.

Good example of using NSEvent within an application. You demonstrated several techniques the OP might benefit from. 😉

Regards,

Craig

May 6, 2010 9:31 AM in response to Craig.Williams

Hey Craig, well put my friend.

So, we can sum things up for Cande by saying, there's no "prepackaged" method for obtaining mouse coordinates directly from within plain old, vanilla AppleScript. It's either by using a *do shell script* call to a command line utility using your method, which requires installing additional files for the functionality to work, or by creating an AppleScriptObjC application using Xcode.

I think we have both shown that it's not that hard to acquire the functionality with just a few extra steps. 😉

Cheers...

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Mouse coordinates

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.