Setting Multiple Interface Text Fields with Repeat?

I have been coding out things the long way for quite some time and as my apps grow, the code gets sloppier and sloppier.

Anyways…

I am constantly needing to update multiple textfield at one time.

For instance I have a text field which I enter a sentence into.
I then process each word in that text fields string and pull out individual words based on length.


set theText to aTextFieldValue as string
if the length of (word 1 of theText) as string is greater than 1 then
  set my tag1 to (word 1 of theText) as string
end if
if the length of (word 2 of theText) as string is greater than 1 then
  set my tag2 to (word 2 of theText) as string
end if

… and so on.
This works, however I run into numerous situations where I have 20+ “tags”…

I want to do the following however I am missing something.


set theText to aTextFieldValue as string
set wordCount to count words of aTextFieldValue
repeat with i from 1 to wordCount
if the length of (word i of theText) is greater than 1 then
  set my ("tag" & i) to (word 1 of theText) as string
end if


Obviously I am doing something wrong in the line :
set my (“tag” & i) to (word 1 of theText) as string
I always throw a “[<NSObject 0x7fff74eb8810> setValue:forUndefinedKey:]”

How do you go about changing multiple text fields in such a manner?

Hi,

you can’t change variable names this way at runtime.

Alternatively put the tag variables into a list


set tagList to {my tag1, my tag2, my tag3}

set theText to aTextFieldValue as string
set wordCount to count words of aTextFieldValue
repeat with i from 1 to wordCount
	if the length of (word i of theText) is greater than 1 then
		set item i of tagList to (word i of theText) -- no coercion needed, theText is AS text anyway
	end if
end repeat

StefanK ! Thank you!

I had tried numerous lists…however I was never including the “my” in the actual list!

(bangs hand against head)

Thank you!

I am able to build and run the updated code fine, however it does not update the “tag” text fields…

It doesn’t throw any errors, and when logging everything appears to be fine.
I have the bindings set to “continuously updates value” as well…

What am I missing now? (lol)

assuming the tag. properties are references to the text fields, you have to call the setStringValue() method,
try this:


set tagList to {my tag1, my tag2, my tag3}

set theText to aTextFieldValue as string
set wordCount to count words of aTextFieldValue
repeat with i from 1 to wordCount
	if the length of (word i of theText) is greater than 1 then
		set currentTextField to item i of tagList
		currentTextField's setStringValue(word i of theText)
	end if
end repeat

The “tag” properties are references bound to the text field values.

When attempting to call the setString() method I receive “Unrecognized function setStringValue.”

then try to bind (actually connect) the properties to the text field instances rather than their values

I am a bit rusty as I haven’t been coding for awhile…
Am I connecting the text field instance as a referencing object in the connections inspector?

I don’t believe I have ever connected the instance … not sure how to do it!

uncheck the checkbox in the Bindings Inspector, select the target class on the left side and control-drag from the blue cube to the specific text field then select the appropriate property.

It’s even easier using the Assistant Editor.

I figured it out!
I had it set up right, I just overlooked :
“currentTextField’s setStringValue(word i of theText)”
should be
currentTextField’s setStringValue:(word i of theText)

Thanks for all the help!