Update text field immediately

I have made (as an example project) an application that has two text fields: one with a number formatter and one with my own handler to check if the text field is empty. It works when tab is pressed, or if the other text field is clicked in. It does not work if I press a check box or a push button.

I have not built this with bindings because I want to make the user defaults programmatically, otherwise I know that you can use “Continuously Updates Value”

Does anyone have a way I can get the text fields to respond immediately?

Here’s a link to the project:

Here’s the code:

--  AppDelegate.applescript
--  Format Text
--  Created by David Whitehead on 15/05/2014.
--  Copyright (c) 2014 David Whitehead. All rights reserved.

script AppDelegate
	property parent : class "NSObject"
	property numberField : missing value
	property nameField : missing value
	property theName : missing value
	property myDefaults : missing value -- contains preferences
	on applicationWillFinishLaunching:aNotification
		tell current application's NSUserDefaults to set myDefaults to standardUserDefaults()
		tell myDefaults to registerDefaults:{theNumber:"10"}
		tell myDefaults to registerDefaults:{theName:"PDF"}
	end applicationWillFinishLaunching:
	on applicationShouldTerminate:sender
		return current application's NSTerminateNow
	end applicationShouldTerminate:
	on retrieveDefaults:sender
		tell myDefaults to set theText to objectForKey_("theNumber")
		tell numberField to setIntegerValue:theText
		tell myDefaults to set theText to objectForKey_("theName")
		tell nameField to setStringValue:theText
	end retrieveDefaults:
	on saveDefaults:sender
		set theText to numberField's integerValue()
		tell myDefaults to setObject:theText forKey:"theNumber"
		set theName to nameField's stringValue()
		tell myDefaults to setObject:theName forKey:"theName"
	end saveDefaults:
	on checkText:sender
		set theText to sender's stringValue()
		set theText to theText as text
		if length of theText is 0 then
			display dialog "You must enter some text." buttons "Okay" default button "Okay" giving up after 10
			tell sender to setStringValue:"PDF"
		end if
	end checkText:
	on pressGo:sender
		set theName to nameField's stringValue
		log "theName: " & theName
	end pressGo:
end script

Model: MacBook Pro 2.7 GHz Core i7 16GB RAM
Browser: Safari 537.31
Operating System: Mac OS X (10.8)

What you need to do is to make sure none of the text fields is the first responder; if they won’t give up first responder status, there’s a problem. The easiest way to do this is to make the window’s first responder itself, which you can do by using missing value. So add this to your button handler:

        set theResult to theWindow's makeFirstResponder_(missing value)
        if theResult as boolean is false then return

You’ll need an outlet for the window.

Thanks for that Shane.

I also made a handler using that code and attached it to the check box so that if that is clicked it does the same.