More focusing a text field

A question I asked a while back about returning focus to a text field was answered and I can make it work, kind of…

I can get the focus to the field I need but the only way I can do it is to remove the return key function from a button, which I’d rather not do. Otherwise the focus remains in the text field last selected. Not all text fields are always used in my app.

Any direction or help is appreciated.

PreTech

Hi PreTech,

Just to clarify… Do you have a button that when clicked sets the focus properly but when you click the button by pressing the return key the focus stays in the last edited text field?

If so, this seems to be a glitch.


on clicked theObject
	tell window of theObject to set first responder to text field "t1"
end clicked

CarbonQuark

Hi pretech,

I hope, I understood:

when clicking the Button or when hitting return when the button is first responder then everything works fine, but when an other text field is first responder and you hit return, then the first responder flashes for a short time to the new text field, but then comes back to the text field, that was first responder. Correct?

I found a description and solution for this problem here: http://www.cocoadev.com/index.pl?ChangeFirstResponderFromATextFieldSActionMethod

you can implement it in your AppleScript Studio projekt like this:

Add these 2 files in your project:

file: MyNSWindowAdditions.h

#import <Cocoa/Cocoa.h>

@interface NSWindow (Additions)
- (void)makeFirstResponder:(NSResponder *)responder withDelay:(int)delay;

@end

file: MyNSWindowAdditions.m

#import "MyNSWindowAdditions.h"

@implementation NSWindow (Additions)
- (void)makeFirstResponder:(NSResponder *)responder withDelay:(int)delay{
	[self performSelector:@selector(makeFirstResponder:) withObject:responder afterDelay:delay];
}
@end

Now you can call this new NSWindow method instead of ‘set first responder to …’:

on clicked theObject
	tell window "main"
		call method "makeFirstResponder:withDelay:" of it with parameters {(text field "tf2"), 0}
	end tell
end clicked

:wink: D.

Thanks Dominik! :smiley:

That works perfectly! Even more amazing is that I actually was able to plug this in!

Thanks to CarbonQuark for taking an interest in the problem! :wink:

PreTech

Hey,

I’ve got a little question:

in ASOC you can use performSelector_wihtObject_afterDelay for executing some code after e.g. clicking a button:

performSelector_withObject_afterDelay_(“SkripttoExecute:”, sender, delay)

how can I do this using ASS?

I tried to modify the Code Dominik posted, but it doesn’t work.

Could someone please post the code for it?

Thanks!