Mouse Location?

How do you get the mouse’s location in a window? Is there a simple way to access this information in response to a mouseDown event? I can get the location when I click on a button if I include the following in the button’s action method:

set mouseLoc to NSEvent’s mouseLocation
log mouseLoc

(i also have: property NSEvent: class “NSEvent” at the top of my program)

If anyone has sample code for dragging objects around in a window that would be really helpful.

It’s not clear to me what you’re after. You’ve posted the code that will tell you the mouse location. Are you wanting to know how to get mouseDown events? Your reference to dragging suggests you really want to track mouseMoved:.

What are you trying to do exactly?


I am primarily interested in learning how to use the mouse to drag objects around in a window, but I was trying to start out slow and just figure out how to intercept a mouseDown event and do something with it.

You just need a mouseDown_ handler on a responder, and call locationInWindow() on the returned event.

For example, you could make a new view subclass like this:

script MyView
	property parent : class "NSView"
	on initWithFrame_(frame)
		continue initWithFrame_(frame)
		-- Initialization code here.
		return me
	end initWithFrame_
	on mouseDown_(theEvent)
		set {x:x1, y:y1} to (theEvent's locationInWindow()) as record
		log x1
		log y1
	end mouseDown_
end script

Then change your window’s view to be of class MyView, and click away.


I think that there is something really basic that I’m not understanding here, because I don’t understand where this scriplet would go in my project. Lets say I create a project called moveStuff. When I do this, Xcode will automatically create an applescript called moveStuffAppDelegate with the parent property set to class NSObject. As I understand it, from the ASOC release notes, this applescript is an ObjectiveC class and so it appears under the Classes folder in the Groups and Files pane. So where does the scriplet you provided go? Do I add it under the other script in the script editor, or does it replace that one, or something else?
Also, why do you need to make a new view subclass in the first place? Why can’t the window’s content view, which is an NSView, handle these mouse events directly?


subclassing is the most elegant way to add custom functions to classes without creating a bunch of delegates and notifications. The technique is to override existing methods. The advantage is that you can call the extended method directly as well as the original method by sending the message to super.

To subclass a class create Objective-C .m and .h files with the object declaration of the parent class e.g.

[code]@interface myView : NSView {}

and assign the name of the class (myView) to the object in Interface Builder > Inspector > [class] identity

Sorry for not being clear. You create it as a new script object/class, by going to File → New File. Where it lives in the Groups & Files panel is basically cosmetic.

It does handle them, but the default behavior is essentially to ignoring them. Subclassing is a way of customizing an object’s behavior.


Thanks for your response. I had tried adding a file before, but had mistakenly added a Cocoa file instead of an Applescript one, so of course I got a new .M and .H file instead of a new applescript. Anyway, I now have the mouseDown and mouseDragged methods working.