FIRST RESPONDER issue...

Hi everyone,

I am building an Applescript application using xcode and I am having an issue with setting the first responder.

I have a very simple interface in my xcode project with just two text fields and a button.
The button can be activated by mouse clicking it or by hitting return on the keyboard.

The code that I am using to set the first responder works great if the user “clicks” the button…
However, if the user hits return to activate the button the code sets the first responder to text box 1 for a moment and then for some reason the cursor snaps back to text box 2.

Is there a way to keep this from happening?

Thanks,
CarbonQuark

my code:


on clicked theObject
	if name of theObject is "myButton" then
		tell window "myWindow"
			set first responder to text field "myTextbox1"
			-- Additional processes go here
		end tell
end if
end clicked

Moving to AS Studio & Xcode…

I still haven’t figured this out… does anyone have any suggestions? Thanks, CarbonQuark

I still need help with this does anyone have any ideas?

I think it might be a window focus issue… but I’m not sure how to test.

Any information would be greatly appreciated!
CarbonQuark

OK I have replicated what you describe. I believe that it has to do with the text field intercepting the return keystroke. When I do it, I type into the first box, press tab to go into the second box, type some letters, and then press return. The focus goes to box 1 then goes to box 2, and the text is all highlighted. Key is that the text is highlighted! For some reason the script is treating the return keystroke as “end editing” command. I tried switching the text frame 2 to only be “send action on - enter only” but that did nothing.
BUT now I changed the button to be keystroke Return PLUS Command, and now that seems to work OK as you want it to. So it does seem regular return is just selecting/highlighting the text of the field and somehow forces it to highlight text in the frame, regardless of your script.

SuperMacGuy,

Thanks for taking the time to replicate and troubleshoot!

I will try what you suggested. Maybe that will work… I’ll just have to remember to hold command before I hit return.

Cheers,
CarbonQuark

I think you’re getting yourself confused as to what is actually happening in your interface. To get an idea what is happening use the following code. Note that you have to hook your text fields to the “on action” handler in IB. Then start your application and move between the 3 items by 1) tabbing between objects, 2) clicking between objects, and 3) hitting the return key when the focus is on the 3 objects. Note the logged items to see what is happening as you try the different methods of moving between the objects.

Next you need to understand how you can change the action of the text fields. Go to interface builder and change the action from “sent on end editing” to “sent on enter only” for the text fields. Then tab and click and hit return again and watch the logged messages. You will see that it acts differently… the button isn’t always clicked when you hit return while you are in a text field like before. Also the “on action” handler isn’t always run when you click or tab between objects.

These 2 tests will help you understand what is happening. Decide which behavior is desirable.

on clicked theObject
	if name of theObject is "myButton" then
		log "on clicked the button"
	end if
end clicked

on action theObject
	if name of theObject is "myTextbox1" then
		log "on action textbox 1"
	else if name of theObject is "myTextbox2" then
		log "on action textbox 2"
	end if
end action

Now if your goal is to have the focus of the cursor always go to the first text field whenever you hit the return key then the following code will do it regardless of which action you choose for your text fields.

on clicked theObject
	if name of theObject is "myButton" then
		setFocusToTextbox1()
	end if
end clicked

on action theObject
	if name of theObject is "myTextbox1" then
		setFocusToTextbox1()
	else if name of theObject is "myTextbox2" then
		setFocusToTextbox1()
	end if
end action

on setFocusToTextbox1()
	tell window "myWindow" to set first responder to text field "myTextbox1"
end setFocusToTextbox1