Page 1 of 1

[SOLVED] LC Syntax

Posted: Sun Jul 12, 2020 7:08 pm
by anmldr
I am creating a card dynamically along with controls on the card. One control is a button. In trying to set the script of the button with:

Code: Select all

set the script of button "theNewButton" to "on mouseUp" & CR & tab & "go to card "firstCard"" & CR & "end mouseUp"
I get the error (Handler: can't find handler) near "firstCard"

If I remove the quotes around "firstCard" (in the script above) then all works just fine. When I click on "theNewButton", it goes to "firstCard" just fine without the quotes.

After the card and button are created, if I open the script for the button, there (of course) are no quotes around "firstCard" in the script. I thought that it was proper syntax in script to surround a control's or card's name in quotes.

Thank you,
Linda

Re: LC Syntax

Posted: Sun Jul 12, 2020 7:19 pm
by richmond62
Well, there's a funny question, mainly because I'd never call a card something (such as "firstCard")
that seemed remarkably near to some sort of internal name the LiveCode engine might be using.

If I had called a card "KARD ONE" and wanted to move there I'd certainly type this:

Code: Select all

go to card "KARD ONE"

Re: LC Syntax

Posted: Sun Jul 12, 2020 7:30 pm
by Klaus
Hi Linda,

LC sees everything inside of quotes as a string!
So you need to build your script this way:

Code: Select all

set the script of button "theNewButton" to "on mouseUp" & CR & "go to card" && QUOTE & "firstCard" & QUOTE & CR & "end mouseUp"
No TAB neccessary, that is only added in the editor to make the script readable. :-)


Best

Klaus

Re: LC Syntax

Posted: Sun Jul 12, 2020 7:35 pm
by FourthWorld
Klaus has you covered on the string concatenation. I'm more curious as to why it's necessary to dynamically write scripts for navigation tasks. Can you tell us a bit more about the goal with that? I'll bet we can find a simpler way.

Re: LC Syntax

Posted: Sun Jul 12, 2020 7:45 pm
by anmldr
mainly because I'd never call a card something (such as "firstCard")
that seemed remarkably near to some sort of internal name the LiveCode engine might be using.
Thank you, Richmond. The name "firstCard" would have never made it to a real app. I just wrote that for posting here. Thanks for the tip though.

Fourthworld, thank you. Also, to answer this question:
I'm more curious as to why it's necessary to dynamically write scripts for navigation tasks. Can you tell us a bit more about the goal with that?
It isn't necessary. I am learning syntax by using it. Now I understand what was wrong with my script. I should have used the constant.

However, I do eventually intend to write an app that (may) create and destroy cards/controls on the fly to keep the size of the app smaller. It potentially would have hundreds and hundreds of cards. I am thinking now about having some "template" cards that I can clone, rename, populate the labels of buttons and other controls and their scripts dynamically and then when I am finished with it, delete the newly created card. I don't know how practical this will be. Really, the whole point at the moment is just to learn LC syntax.

Thank you Klaus.
Linda

Re: [SOLVED] LC Syntax

Posted: Sun Jul 12, 2020 8:03 pm
by FourthWorld
You definitely have a good learning path; you ask some of the better questions I see here. Keep us posted as new challenges arise...

Re: [SOLVED] LC Syntax

Posted: Sun Jul 12, 2020 8:04 pm
by anmldr
For future newbie:

Create a new stack. It's name should be "KARD1". Put this in the script of a button.

Code: Select all

on mouseUp
   if exists (card "theNewCard") then delete card "theNewCard"
   create card "theNewCard"
   create button "theNewbutton"
   set the style of button "theNewButton" to roundRect
   set the script of button "theNewButton" to \
         "on mouseUp" & CR \
         & "go to card" && QUOTE \
         & "KARD1" & QUOTE & CR \
         & "end mouseUp"
end mouseUp
The stack has one card as a standalone when launched. It creates a card on the fly with a button and the above script. You can see that you can set properties of controls in script (see theNewButton above). The button navigates back to the original card of the stack "KARD1". The next time that the button on "KARD1" is clicked, there will be a card called "theNewCard" and it will be deleted before a new card is created.

If in the app, I use a template card that I can clone, rename, add labels to buttons, add content to fields and add scripts to controls then delete the card when finished with it, then the final version of the app will be so much smaller in size (memory).

OK. Enough for this theory. On to another subject where I will probably need help and or guidance.

Oops. One more thing about the syntax of the script above.
In the first line of the on mouseUp, I have to use parenthesis around (card "theNewCard) when it is after "if exists" but NOT around it when it comes after "delete card". What is the difference?

Linda

Re: [SOLVED] LC Syntax

Posted: Sun Jul 12, 2020 9:08 pm
by SparkOut
HI Linda
The "one more thing" is simply that exists() is a function (returning true or false), and the arguments it takes are delivered in parentheses.
Whereas delete [card] is a command and its argument is a string literal (in this case). If you had a variable tCard containing "theNewCard" as its value, you could pass that as the argument too. In this circumstance, it is often helpful to the engine to force resolution of a variable by wrapping in parentheses as well, although not necessarily necessary, and not the required syntax for a command.

Re: [SOLVED] LC Syntax

Posted: Sun Jul 12, 2020 9:11 pm
by anmldr
Got it.
Linda

Re: [SOLVED] LC Syntax

Posted: Sun Jul 12, 2020 9:30 pm
by bogs
Since SparkOut addressed the actual question, and provided one way to deal with it, I'll suggest yet another.

The first line of code -

Code: Select all

on mouseUp
   if exists (card "theNewCard") then delete card "theNewCard"
- could also be written thus -

Code: Select all

on mouseUp
   if "theNewCard" is among the lines of the cardNames [of stack] then delete card "theNewCard"
"Of stack" in the above can be represented in a number of ways, if you are in the message box, the mouseStack works, other wise you would most likely use "this stack" or a directly named stack, i.e. of stack "my stack"

aPic_cardNames1.png
Now you see it....
aPic_cardNames1.png (18.48 KiB) Viewed 7155 times
aPic_cardNames2.png
...and now you don't...

Re: [SOLVED] LC Syntax

Posted: Mon Jul 13, 2020 5:43 pm
by AndyP
To make adding the code for a control easier you can put the script into a hidden field and set the script to the contents of the field. This way you don't have to worry about concatenating an quotes.

Re: [SOLVED] LC Syntax

Posted: Wed Jul 15, 2020 1:51 am
by anmldr
Nice idea, Andy.

Gracias,
Linda