[SOLVED] LC Syntax

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
anmldr
Posts: 459
Joined: Tue Sep 11, 2012 11:13 pm

[SOLVED] LC Syntax

Post by anmldr » Sun Jul 12, 2020 7:08 pm

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
Last edited by anmldr on Sun Jul 12, 2020 7:45 pm, edited 1 time in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: LC Syntax

Post by richmond62 » Sun Jul 12, 2020 7:19 pm

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"

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: LC Syntax

Post by Klaus » Sun Jul 12, 2020 7:30 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9824
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: LC Syntax

Post by FourthWorld » Sun Jul 12, 2020 7:35 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

anmldr
Posts: 459
Joined: Tue Sep 11, 2012 11:13 pm

Re: LC Syntax

Post by anmldr » Sun Jul 12, 2020 7:45 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9824
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: [SOLVED] LC Syntax

Post by FourthWorld » Sun Jul 12, 2020 8:03 pm

You definitely have a good learning path; you ask some of the better questions I see here. Keep us posted as new challenges arise...
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

anmldr
Posts: 459
Joined: Tue Sep 11, 2012 11:13 pm

Re: [SOLVED] LC Syntax

Post by anmldr » Sun Jul 12, 2020 8:04 pm

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

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: [SOLVED] LC Syntax

Post by SparkOut » Sun Jul 12, 2020 9:08 pm

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.

anmldr
Posts: 459
Joined: Tue Sep 11, 2012 11:13 pm

Re: [SOLVED] LC Syntax

Post by anmldr » Sun Jul 12, 2020 9:11 pm

Got it.
Linda

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: [SOLVED] LC Syntax

Post by bogs » Sun Jul 12, 2020 9:30 pm

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 5315 times
aPic_cardNames2.png
...and now you don't...
Image

AndyP
Posts: 615
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

Re: [SOLVED] LC Syntax

Post by AndyP » Mon Jul 13, 2020 5:43 pm

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.
Andy Piddock
https://livecode1001.blogspot.com Built with LiveCode
https://github.com/AndyPiddock/TinyIDE Mini IDE alternative
https://github.com/AndyPiddock/Seth Editor color theming
http://livecodeshare.runrev.com/stack/897/ LiveCode-Multi-Search

anmldr
Posts: 459
Joined: Tue Sep 11, 2012 11:13 pm

Re: [SOLVED] LC Syntax

Post by anmldr » Wed Jul 15, 2020 1:51 am

Nice idea, Andy.

Gracias,
Linda

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”