call method "rowAtPoint:" -- is this right?

I’m trying to do this in my Table’s right mouse up handler:

on right mouse up theObject event theEvent
	set theTable to table view 1 of scroll view 1 of window "mainWindow"
	set {posX, posY} to location of theEvent
	set the_row_number to (call method "rowAtPoint:" of theTable with parameter {posX, posY})
	log ("the_row_number: " & the_row_number)
end right mouse up

But I never get the correct row number. Is my syntax wrong?

Thanks, as always.

  1. What is your overall goal?

  2. Given situation X, what row number do you expect, and what row number do you actually get? You say it’s wrong, but you don’t say how it’s wrong.

Prediction:

You aren’t converting the location in window to the view in which you want to track the mouse event. If it’s like the Cocoa version, “location of event” will give you the location in window, which then needs to be converted.

This is commonly seen in the mouse event methods of NSView subclasses, for example:

NSPoint eventLocation = [theEvent locationInWindow]; NSPoint localPoint = [self convertPoint:eventLocation fromView:nil];

I need the index of the row of the table that is right-clicked on to grab various cell data from that row to to determine the contents of a contextual menu. (BTW, I would rather not use clicked row in on clicked since I don’t necessarily want the row to be selected or de-selected when clicking it; and if a row is already selected, the should select handlers aren’t called before on clicked.)

When row 1 is right clicked the result is 21, row 2 is 20; the results are always between 1 and 21, so I suspect that your prediction is true–I’m only getting the coords for the window not for their relative position in the table.

How would I perform the conversion in order to obtain the correct row?

Thanks.

Got it:

on right mouse up theObject event theEvent
	set theWindow to window "mainWindow"
	set theTable to table view 1 of scroll view 1 of theWindow
	set the_row_number to (call method "rowAtPoint:" of theTable with parameters {(call method "convertPoint:fromView:" of theTable with parameters {call method "mouseLocationOutsideOfEventStream" of window "mainWindow", (container of theWindow)})}) + 1
	log ("the_row_number: " & the_row_number)
end right mouse up