getting and testing two strings from input boxes

I’m trying to compare two strings entered by the user. Even if I enter the same thing nothing happens. When I get rid of the if statement the strings are then perfectly displayed.

It’s probably something simple. Any help ?

script TestAppDelegate
	property parent : class "NSObject"
    property test1 : missing value
    property test2 : missing value
    property res : missing value
    property res2 : missing value

    on test_(sender)
        set test1 to test1's stringValue()
		set test2 to test2's stringValue()
        if test1 = test2 then
        res's setStringValue_(test1)
        res2's setStringValue_(test2)
        end if 
    end test_
	
	on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened 
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
end script

I’m not sure where test1 and test2 are getting any value – they are just missing value in your script.

Perhaps a more general answer will help:

You can compare two AppleScript strings using “if x = y then”.

To compare two NSStrings, which are really just pointers to strings, you should use “if x’s isEqualToString_(y) as boolean then”.

My bad for leaving some info out. I’m making a cocoa applescript app. Thus this part of your answer solved my problem.

So now I have this :

    on test_(sender)
        set test1 to test1's stringValue()
		set test2 to test2's stringValue()
       if test1's isEqualToString_(test2) as boolean
        res's setStringValue_("ok")
        else
            res's setStringValue_("No match")
       end if 
    end test_

The first time I hit the button it works, but after that I always get a “-[NSStringWrapper stringValue]: unrecognized selector sent to instance 0x200088700 (error -10000)” and nothing updates.

Do I have to dealloc/clear something in between ?

Thanks for the help.

It would appear to me that test1 & test2 are attached to your NSTextFields initially, but after the lines:

set test1 to test1’s stringValue()
set test2 to test2’s stringValue()

haven’t you changed what test1 & test2 refer to? In other words they become the contents of the NSTextFields instead of being linked to the NSTextFields.

try something like:

set test1String to test1’s stringValue()
set test2String to test2’s stringValue()

and then compare test1String to test2String

Just a thought, I could be wrong as I’m pretty new to working with ASOC myself.

Brad Bumgarner

You are absolutely right.

It also makes perfect sense.

Thanks !