Benches macOS

8

Mouse coordinates

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Is there any way i can get mouse coordinates?

8 replies

Sign in or join Beachball Help to reply.

  1. 0

    Craig.Williams

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    See this article.

    http://allancraig.net/index.php?option=com_content&view=article&id=137:getting-m ouse-coordinates&catid=39:objective-c&Itemid=86

  2. 0

    Chachi

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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...

  3. 0

    Craig.Williams

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    See this article.

    http://allancraig.net/index.php?option=com_content&view=article&id=137:getting-m ouse-coordinates&catid=39:objective-c&Itemid=86

  4. 0

    Chachi

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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...

  5. 0

    Craig.Williams

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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

  6. 0

    Chachi

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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...

  7. 0

    Kelly Coull

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    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.

  8. 0

    Cande

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Thanks.

    I found Cliclick and it is easiest for me to get mouse coordinates without using Xcode:

    http://discussions.apple.com/message.jspa?messageID=11430717#11430717