trying to put dynamic text into a text field

Hi I am new to Applescripting in Xcode and I am trying to make a very simple app. However I am having the worst time trying to get applescript to post text into my interface nib. I am using Xcode 3.1.3 and interface builder 3.1.2. The project is an Applescript Application.

set the contents of text field “textA” to “test”

This returns a compile time error of:
/Sync Folders/Sync Folders.applescript:65: A “”" can’t go after this identifier. (-2740)

If I do
set the contents of “textA” to “test”

It returns a run time error of:
Can’t set contents of “textA” to “test” (-10006)

also tried variations of
set the contents of text field “textA” of window “mainWin” to “test”
set the contents of text field “textA” of window 1 to “test”
set contents of text field “textA” to “test”
set title of text field “textA” to “test”

In the Nib I tried a bunch of different objects as containers for the text… labels, buttons, and text fields. I have set “textA” as the name of the text field object.

Nothing works please help!

Here is more code in case some wanted to see more. Everything else works fine except the set contents statement.

on clicked theObject

tell application "Finder"
	if name of theObject = "A_Button" then
		try
			set the source_folder to choose folder with prompt ¬
				"Pick a folder to Mirror:"
		end try
		set tmp to source_folder as text
		tell window "mainWin" of theObject
			--		tell theObject with name "pathA"
			-- set title of "pathAText" to tmp as text
			set the contents of "textA" to "test" as string
			-- set the contents of text field "textA" of window "mainWin" to "test"
			-- end tell
		end tell
	else if name of theObject = "B_Button" then
		try
			set the des_folder to choose folder with prompt ¬
				"Pick the other folder to be synced:"
		end try
		tell window 1
			set contents of pathBText to des_folder as string
		end tell
	
	else if name of theObject = "Sync_Button" then
	       if (source_folder is null or des_folder is null) then
	       -- do nothing	
                   else
                        Mirror(source_folder, des_folder)
                   end if
	end if
end tell

end clicked

Hi,

never put AppleScript Studio code in an (AppleScript) application tell block.
This causes unwanted errors in almost all cases.

Instead of contents (of a text field) it’s recommended to use string value instead

PS: the Finder is actually not needed at all in your snippet

Thank you for the quick reply!
Now I am even more confused. So building an applescript application is not the same as applescript studio? What syntax I should be using as reference and examples when building my applescript application in Xcode? I just thought applescript was applescript. =/

I changed my code to:
set the string value of text field “textA” to “test”

Compiles great but… I get run time error "The specified object is a property, not an element. (-10008)

What is it referring too as the property?

The syntax is the same in AppleScript and AppleScript Studio but AppleScript Studio code can cause conflicts in application tell blocks.

For example don’t do this:


tell application "Finder"
	set theName to name of file 1
	set string value of text field "field" of window "main" to theName
end tell

this is recommended


tell application "Finder"
	set theName to name of file 1
end tell
set string value of text field "field" of window "main" to theName

All objects must be full referenced. Your text field “textA” is an element if the main window, write this

text field "textA" of window "mainWin"

Don’t mix up references like theObject variable
in the on clicked handler theObject is the full reference to the object which triggers the handler.
Avoid writing this


tell window "mainWin" of theObject

unless you know exactly what you’re doing

In most cases this is sufficient

tell window "mainWin"

because this is the absolute reference

Ahhh. Thanks for the tips, I feel a bit dumb about the finder telling now that I thought about it. You are right I am not even working with the finder. Anyways I finally got the code to work.

this worked:
set the string value of text field “textA” of window 1 to “test”

this did not:
set the string value of text field “textA” of window “mainWin” to “test”

I had to refer to the window as window 1. Calling it by its name gave me a run time error that it could not change window “mainWin” to “text”. So what does 1 represent? I don’t see that tag in the inspector. Maybe thats a relative address? Saying parent window of the object?

window 1 is always the frontmost window. If you don’t want to reference elements dynamically (as in this case) avoid reference by index

To reference the window properly by name you have to name it in Interface Builder in the AppleScript tab

Stefan,
I tied to use the absolute referencing to the “mainWin” and it gave me an error:
"Can’t set window “mainWin” to “text” (-10006)

set the string value of text field “textA” of window “mainWin” to “text”

I checked project builder 10 times on the spelling and case. It is the correct name of the window under the applescript tab. Buttons all work but the referring of the window does not. Saying window 1 is the only thing that works. Is there something else I am missing? I put scope [global] and Script [sync.applescript]

leave the script empty (none) unless you connect the window to an event handler (like will open).

Is there another tell block involved?

Ok I was clicking on the background of the window to get the inspector data for the window. That was the window view!! Clicking on the window bar gave me the inspection for the window. It works now! Sadly I think this is why I know so many iphone developers that don’t use interface builder.

Thanks for your help Stefan!