I’m trying to write a logic for comparing the contents of two text fields. The user has to enter their short name twice, and if they match, the button come enabled, if they don’t, an x or something appears stating they don’t match. Any ideas??
Simple.
property field1 : missing value
property field2 : missing value
property continueButton : missing value
on checkFields_(sender)
if field1's stringValue() = field2's stringValue() then
continueButton's setEnabled_(true)
xImage's setHidden_(true)
else
xImage's setHidden_(false)
continueButton's setEnabled_(false)
end if
end checkFields
Field1 and field2 are the text fields, xImage is an image of a red x or whatever, and continueButton would be a button that goes on.
Model: Mac Mini
AppleScript: ?
Browser: Safari
Operating System: Mac OS X (10.7)
Hi,
solution with bindings
Implement this code in your App Delegate script
property textValue1 : ""
property textValue2 : ""
on keyPathsForValuesAffectingEnableButton()
return current application's NSSet's setWithObjects_("textValue1", "textValue2")
end keyPathsForValuesAffectingEnableButton
on enableButton()
return (my textValue1 as text is equal to my textValue2 as text) and length of textValue1 > 0
end enableButton
In Interface Builder
¢ bind value of text field 1 to App Delegate script → Model Key Path textValue1
¢ bind value of text field 2 to App Delegate script → Model Key Path textValue2
¢ bind Enabled of the button “ and Hidden of the “x” image “ to App Delegate script → Model Key Path enableButton
TheWayToAppleScript’s code a little shorter would be:
property field1 : missing value
property field2 : missing value
property continueButton : missing value
on checkFields_(sender)
continueButton's setEnabled_(field1's stringValue() = field2's stringValue())
xImage's setHidden_(field1's stringValue() = field2's stringValue())
end checkFields
This works, however it doesn’t seem to like letters in the text fields…I’m missing something simple aren’t I
You can’t compare NSStrings with an “=” sign, you should use the NSString method isEqualToString:
on checkFields_(sender)
continueButton's setEnabled_(field1's stringValue()'s isEqualToString_(field2's stringValue()))
xImage's setHidden_(field1's stringValue()'s isEqualToString_(field2's stringValue()))
end checkFields_
Since an NSString is an object, I think what you are comparing when you use an = sign are the pointers to the place where the data are actually stored. Even though the data may be the same, the pointers are not, they are pointing to two different places in memory.
Another way to do it would be to coerce the stringValues to applescript strings first, and then you can use the equal sign.
continueButton's setEnabled_(field1's stringValue() as string = field2's stringValue() as string)
Ric
@Ric: What you think is entirely true. I forget sometimes that Objective-C doesn’t control operators like C++ or AppleScript do. In my opinion one of the disadvantages of Objective-C compared with C++.