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:
https://www.dropbox.com/sh/f4mz9sozpw14nw4/AAC9tdc4G38jPxlkAaM_TY6ja
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"}
retrieveDefaults_(me)
end applicationWillFinishLaunching:
on applicationShouldTerminate:sender
saveDefaults_(me)
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)