Page 1 of 1

Command to execute some code

Posted: Fri Dec 20, 2013 6:04 pm
by lohill
Is there a LiveCode command that can be used in a routine to cause some other code contained in a field to execute as part of the original routine?
Is so, what is it?

Thanks,
Larry

Re: Command to execute some code

Posted: Fri Dec 20, 2013 6:07 pm
by Klaus
Hi Larry,

there is the "DO" command, maybe that is what you are looking for?
If not, maybe you can give a quick example (in pseudo-code)?


Best

Klaus

Re: Command to execute some code

Posted: Fri Dec 20, 2013 6:15 pm
by dunbarx
Hi.

What Klaus suggested is:

Code: Select all

on yourRoutine
  dostuff
  do field "yourField"
end yourRoutine
Field "yourField" has to have valid, clean LC statements.

Craig Newman

Re: Command to execute some code

Posted: Fri Dec 20, 2013 6:34 pm
by lohill
Thanks Craig and Klaus,

'Do' looks promising, I'll give it a try. Instead of do field "your field" could it also be "do tVariable' where tVariable contains the code?

I also se 'script' so it looks like I could set the script of some invisible button and then send "mouseUp" to it. In that case would it do the button script immediately before executing the rest of the calling routine?

Merry Christmas,

Larry

Re: Command to execute some code

Posted: Fri Dec 20, 2013 7:27 pm
by dunbarx
Hi.

Yes, "do" can do the contents of any container, including variables: "do it"

But "do" is usually reserved for cases where an extra level of evaluation is required in order to make a line of code work properly. There are examples of this in the dictionary. There is also another, little documented set of circumstances where "do" is required in order for the LC parser to be able to make sense of what seems like valid code, but is not quite up to its standards. This is essentially performing another level of evaluation, which sets things right, and you will run across it one day.

In general, I like to keep my code in one place. If you need to send a message to an object somewhere, fine, but it is quite different to send, say, "mouseUp" to a button, which may be useful, than to do what you suggested, to keep a hidden button with code and use it as a library of some sort. This might work, but I would discourage it. Why break your handlers up if not really necessary? Better to use command or function calls, either elsewhere in the object script itself or perhaps higher in the natural hierarchy. I do not know your setup, but I bet you are complicating it.

Craig Newman

Re: Command to execute some code

Posted: Fri Dec 20, 2013 11:30 pm
by lohill
Thanks for the good advice Craig.

Larry

Re: Command to execute some code

Posted: Sat Dec 21, 2013 3:41 pm
by lohill
Thganks, 'Do' was exactly what I needed. Here is a snippit of the code:

Code: Select all

            put the dgDataofIndex[tIndex] of group "DataGrid Rules" into tArray
            put tArray["Steps"] into tSteps
             put tArray["Notes"] into tNotes
            if tNotes is not empty then
               do tNotes
            end if
Before getting to do tNotes the values of the variables were:
tSteps: aprc>0
aprc>5
tNotes: if offset(tRelation,">=")>0 and tNum>0 then delete line 1 of tSteps

tRelation had a value of ">" and tNum was 5

tSteps after execution was just 'aprc>5'

Re: Command to execute some code

Posted: Sat Dec 21, 2013 4:41 pm
by Klaus
lohill wrote:,...Before getting to do tNotes the values of the variables were:
tSteps: aprc>0
aprc>5
tNotes: if offset(tRelation,">=")>0 and tNum>0 then delete line 1 of tSteps
tRelation had a value of ">" and tNum was 5
tSteps after execution was just 'aprc>5'
Aha! :D