Page 1 of 1

repeat for each... object?

Posted: Fri Feb 26, 2016 11:52 am
by ittarter
Hello!

I'm trying to create a function that will change the labels of all buttons/fields with visible labels.

I thought I might be able to use "repeat for each", but it doesn't seem to work for objects.

For example, I store the English version of a button label as a custom property of the button, called cEnglish.

Code: Select all

put "c" & gNativeLanguage into tLanguage
   repeat for each button tButton in card "dashboard"
       if the tLanguage of tButton <> empty then
          set the label of tButton to the tLanguage of tButton
       end if
    end repeat
I get a code termination error on line 2, and I assume it's because this control structure can't be used in this way. Perhaps there is a command that lists all existing objects, or all objects by type, and I could use "repeat for each line" on that list? Is that the best way to do this?

******

On a related note,

Code: Select all

global gNativeLanguage; put the ("c" & gNativeLanguage) of fld "username"

returns a "bad factor" error. If gNativeLanguage is "English", and cEnglish of fld "username" is "Welcome", how can I properly define the custom property before using it?

Re: repeat for each... object?

Posted: Fri Feb 26, 2016 2:58 pm
by dunbarx
Hi.

You are trying to use a syntax that looks sound, but is not really applicable to the gadgets you are trying to change.

I am not sure that made sense.

Try this

Code: Select all

 repeat with y = 1 to the number of buttons
       if the tLanguage of btn y <> empty then
          set the label of btn y to the tLanguage of btn y
       end if
    end repeat
In other words, object references are not like chunk instances.

I hope that makes sense.

Craig Newman

Re: repeat for each... object?

Posted: Fri Feb 26, 2016 4:16 pm
by ittarter
Thanks, Craig :)

Re: repeat for each... object?

Posted: Fri Feb 26, 2016 4:48 pm
by ittarter
Ok, I'm trying to use your code to do what I need to do. Two questions.

QUESTION 1

This bit of code is in my Stack script:

Code: Select all

put "cEnglish" into gNativeLanguage
      repeat with y = 1 to the number of buttons of stack "Destination English"
          if the gNativeLanguage of btn y <> empty then
             set the label of btn y to the gNativeLanguage of btn y
          end if
       end repeat
What I want it to do is change the label of any button in the stack that has something for its custom property cEnglish to change the label of that button.

What it does is only change the buttons on the card -- not the entire stack. Do I have to do each card separately or can I do it all at once?

QUESTION 2

Code: Select all

repeat with z = 1 to the number of fields of card "setup"
          if the gNativeLanguage of fld z <> empty then
             set the label of fld z to the gNativeLanguage of fld z
          end if
   end repeat
On the second line I get the error "no such object". I don't understand what this error means, since the repeat should only function once for each existing field.

Re: repeat for each... object?

Posted: Fri Feb 26, 2016 5:17 pm
by dunbarx
Question 1:

First off, when you

Code: Select all

put "cEnglish" into gNativeLanguage
you are not setting a custom property. You are putting text into a variable. This ought to affect the accuracy of your handler, though it will not throw an error. You need to:

Code: Select all

set the gNativeLanguage 
For the second part of your question, it is simply the way LC works, in that you have to navigate to each card in turn and deal with the controls on that card. Not too onerous, but the way you wrote it, only the first card will be accessed when you "go" to another stack as in "... of stack "Destination English"

Question 2

Are you sure there is a field on the card? Step through and see.

Craig

Re: repeat for each... object?

Posted: Fri Feb 26, 2016 5:29 pm
by quailcreek
If you're putting part of your script into the stack then this might get you started. I'm assuming you have more then one language defined so that's why gNativeLanguage is a variable, right? If this is the case you could write this as a command or a function and then pass gNativeLanguage instead of using a global variable.

As far as your error. Fields don't have a label property. Put gNativeLanguage into fld ...

This will handle the btns, do one for the flds or add code to this one to include flds.

Code: Select all

on setTheLables
   repeat with theCards = 1 to the num of cds -- this will do all the cds in the stack
      repeat with theBtns = 1 to the num of btns of cd theCards -- this will do all of the btns on he cd
         if the gNativeLanguage of btn theBtns of cd theCards is not empty then
            put the gNativeLanguage of btn theBtns of cd theCards into tNativeLanguage
            set the label of btn theBtns of cd theCards to tNativeLanguage
         end if
      end repeat
   end repeat
end setTheLables

Re: repeat for each... object?

Posted: Fri Feb 26, 2016 6:40 pm
by ittarter
Thanks, guys. Everything works now. This is a wonderful way to make an app multilingual.

For each button and field with visible text, make custom variables (e.g. cEnglish, cFrench) for each, and put the correct text variations in them (e.g. Hello, Bonjour). You can add as many languages as you want.

Here's my final code, for posterity.

Code: Select all

repeat with theCards = 1 to the num of cds
      repeat with y = 1 to the number of buttons of cd theCards
          if the gNativeLanguage of btn y of cd theCards <> empty then
             set the label of btn y of cd theCards to the gNativeLanguage of btn y of cd theCards
          end if 
       end repeat
       repeat with y = 1 to the number of fields of cd theCards
          if the gNativeLanguage of fld y of cd theCards <> empty then
             put the gNativeLanguage of fld y of cd theCards into fld y of cd theCards
          end if
       end repeat
    end repeat

Re: repeat for each... object?

Posted: Sat Feb 27, 2016 6:11 pm
by jacque
Also just for posterity, this multi-language issue is exactly what LiveCode profiles are meant to solve. Once a profile for each language is set up, you can switch all names, labels, etc. with a single line of code.

I believe profiles are explained in the user guide. The setup is a bit tedious but the result is simpler code.

Re: repeat for each... object?

Posted: Sat Feb 27, 2016 6:57 pm
by quailcreek
Hi Jacqueline,
Always a wealth of information. Are you talking about revProfile? I've been looking for how to set this up. Do you have any more information on this? Which user guide is it in?

Re: repeat for each... object?

Posted: Sat Feb 27, 2016 10:02 pm
by jacque
Instructions for property profiles are in the User Guide under the Help menu. It's section 4.5 in the guide.

Edit: there are more entries under other sections. Search for "property profile" in the PDF to see them all. There are quite a few.

Re: repeat for each... object?

Posted: Sat Feb 27, 2016 11:11 pm
by quailcreek
Sorry, Jacqueline. I've been using LC 8 and they have the User Guide pretty messed up so I was thinking there was another place to look. Had to open LC 7 to get to the PDF. Thanks.