how to set Xcode Applescript focus based on return key?

I’ve got a small data entry app, with a GUI composed of a window, an inset border, an NSdatepicker date field, a couple of buttons (“Quit” and “Enter”), and a number of text fields of various kinds of data.

What I’d like to do is to have the enter (button or key) trigger processing of the input data, clear the processed fields and reset the cursor position to the first field – and I have that all working fine… almost.

The one thing that I cannot get to happen is to have pressing the enter key trigger the code that sets the first responder to the field I specify. If I click the enter button it works perfectly, but if I press the return key (which is specified as a key equivalent for the enter button via the IB Inspector panel), the focus remains with the last field that was previously occupied by data. Everything else works fine.

Here’s some snippets of the code:

on positionCursor(theWindow, theField)
	set first responder of theWindow to text field theField of theWindow
end positionCursor

on clicked theObject
	(*Add your script here.*)
	set objName to the name of theObject as Unicode text
	set theWindow to the window of theObject
	if objName = "Quit" then -- the "Quit" button
	else if objName = "Enter" then -- the "Enter" button
		set dataInput to false
		if dataInput then
			if insertData() then
				set dataInput to false
			end if
		end if
		set cursorField to "type01"
		positionCursor(theWindow, cursorField)
	else if objName = "txndate" then
		set txnDate to (content of theObject)
		-- format date using NSDateFormatter, extract {yyyy,mm,dd} fields
		set txnYYYY to year of txnDate as small integer
		set txnMM to (month of txnDate as integer) as small integer
		set txnDD to day of txnDate as small integer
	end if
end clicked

I’m guessing that some field – possibly the last text field with data in it (where the focus was when I pressed Enter), or maybe the highest-numbered object in the GUI is also getting the enter key-press, but as I have no other code that references the positionCursor routine, I have no idea how it is resetting the first responder.

Or more likely, I’m 'way out in left field, and trying to do this in a totally inappropriate manner.

Anything obvious leap out at anyone?

Without testing anything, I could suggest setting the action of the text fields to ‘performClick:’ on the button, by control dragging from the field to the button.
Or another option could be set the action of the button (using the same process) to the ‘selectText:’ method of the ‘type01’ field.

If those don’t work, then integrating objective-c is bound to, which we can help with.

Thanks – I tried the first suggestion and it worked perfectly.

Now, just for my education, exactly what kind of connection did I establish by assigning/linking/connecting (whatever the correct verb is) the action of the text fields to ‘performClick:’ on the button? My guess is that an action in a text field now generates a click event to the appropriate button. But I don’t see why any keystroke into the text field does not also trigger the button.

And is there a good reference of all the various standard actions that one can assign/link/connect for each kind of GUI element?

This whole area of how Xcode and IB streamline the linking of actions to control/GUI elements is pretty foggy. I think the world could use an IB for Dummies book with a great many examples and explanations for how this all works. I suppose that eventually, a blinding flash of insight will occur, and I’ll see things clearly. But until then, much of this aspect of Cocoa GUI programming is too much like magic.