What is your best practice for setting a script in a script?

Geoff Canyon gcanyon at gmail.com
Sat Jul 15 19:14:48 EDT 2023


Custom properties would definitely be my go-to, similar to what others have
said. That said, I think the merge command is your friend here.

This script in a button does what you want:

on mouseUp
   set the script of button "Sample" to \
         merge("on mouseUp[[cr]]" &\
         "ask [[quote]]Please enter a number between 1 and
100[[quote]][[cr]]" &\
         "if it is not a number then exit mouseUp[[cr]]" &\
         "if it < 1 OR it > 100 then exit mouseUp[[cr]]" &\
         "put it into field [[quote]]Number[[quote]][[cr]]" &\
         "end mouseUp")
end mouseUp

The use of \ to continue, and [[quote]] and [[cr]] results in a pretty
readable script to my eye at least.

gc

On Sat, Jul 15, 2023 at 8:07 AM Paul Dupuis via use-livecode <
use-livecode at lists.runrev.com> wrote:

> So in the instances where you have a script that creates an object and
> then sets the script of that object (example below), what is you best
> practice for having the script in a script and still be readable?
>
> Example:
> BUTTON "Make"
> on mouseUp
>    local tScript
>    create button "Sample"
>    put "on mouseUp"&cr into tScript
>    put "  ask 'Please enter a number between 1 and 100'" &cr after tScript
>    put "  if it is not a number then exit mouseUp" &cr after tScript
>    put "  if it < 1 OR it > 100 then exit mouseUp" &cr after tScript
>    put "  put it into field 'Number'" &cr after tScript
>    put "end mouseUp" after tScript
>    replace "'" with quote in tScript
>    set the script of button "Sample" to tScript
> end mouseUp
>
> This create a button "Sample" with the script
> on mouseUp
>    ask "Please enter a number between 1 and 100"
>    if it is not a number then exit mouseUp
>    if it < 1 OR it > 100 then exit mouseUp
>    put it into field "Number"
> end mouseUp
>
> However, I reading the script for button Sample in button Make with the
> "pu" statements and quotes (double and single) and cr and so one
> cumbersome to read. Yes there are behavior scripts, but this is about
> the best practice of placing a script in a script (to set the script of
> an object) and having it as clear as possible.
>
> I wish there was a command like "put begin block" where you could follow
> it any  number of lines and then "end block into tVar". i.e.
>
> put begin block
> on mouseUp
>    ask "Please enter a number between 1 and 100"
>    if it is not a number then exit mouseUp
>    if it < 1 OR it > 100 then exit mouseUp
>    put it into field "Number"
> end mouseUp
> end block into tScript
>
> Is there a better way to have a script written in a script?
>
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>


More information about the use-livecode mailing list