Another newbie question

Hi:

Still playing around with Applescript and xCode (I’ve slugged thru the first couple of chapters in Shane’s book which has helped) but I’m still missing something as far as output goes.

Here’s my code:


script AppDelegate
	property parent : class "NSObject"
    
    property fieldOne : missing value
    property fieldTwo : missing value
    property fieldThree : missing value
    
    on setWindowFields_(sender)
    set fieldOneValue to fieldOne's stringValue()
    set fieldTwoValue to fieldTwo's stringValue()
    
   -- set concatenatedString to current application's NSString's stringWithFormat_("%@%@", fieldOneValue, fieldTwoValue)
   
    fieldThree's setStringValue_("20")
    end setWindowFields_

	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

My interface is just three text fields binded to their respective properties.

What I’m trying to do is enter text into the first two fields and then concatenate them into the third when I hit the push button.

When I couldn’t get that to work, I commented out the concatenate line and just tried to write a value back to the third box. When I hit the push button, I get:

2012-09-05 09:56:52.060 test3[86996:303] -[__NSCFString stringValue]: unrecognized selector sent to instance 0x102669e50
2012-09-05 09:56:52.070 test3[86996:303] *** -[AppDelegate setWindowFields:]: -[__NSCFString stringValue]: unrecognized selector sent to instance 0x102669e50 (error -10000)

When I control click on the AppDelegate cube the three referencing bindings are shown. Then all three properties are also listed as output so I thought I should connect fieldThree to the Field Three text box as an outlet but that produced all sorts of errors.

Could someone point me in the right direction on what I need to do in the interface to write back a value to a text field?

Thanks.

–jon

Hi,

the error message says, that the receiver of the stringValue message is of class NSString,
so you probably bound the value of the text field instead of the text field itself.

In this case the fieldOne etc. properties contain the string value of the respective text fields and setting the properties sets automatically the value of the text fields because they are dynamically connected

Hi Jon,

Both your original code and test code work on my Mac. I get the expected behavior when I hit the button.

I suspect that Outlets or Received Action might be set incorrectly in your xib file.
When right-clicking the App Delegate cube, you should see only the below connections:

-under Outlets:
fieldOne connected to a Text Field
fieldTwo connected to a Text Field
fieldThree connected to a Text Field

-under Referencing Outlets:
delegate connected to File’s Owner

-under Received Actions:
setWindowsFields: connected to Push Button

If you have other connections in there, get rid of them, and let me know how it goes.
Best of luck,

Max

Max:

So I killed the referenced bindings to the three properties and connected all three in the Output area to the three text fields and now it’s working.

Thanks. I obviously really need to finish Shane’s book and work thru Craig Williams’ tutorials.

Appreciate the help.

–jon

Thanks Jon,

I’m glad it works. I was under the impression you had indeed set Cocoa bindings instead of outlets. Cocoa bindings are a powerful feature that can greatly simplify your code, but they might not be the best choice in this case, especially because, in my experience, empty text fields result in “null” string values that extra lines of code need to replace with “” strings.

I’m happy I was able to help. Enjoy!

Max