scripting the script of a btn?

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
melristau
Posts: 56
Joined: Tue Jul 14, 2015 5:15 pm
Contact:

scripting the script of a btn?

Post by melristau » Sat Jul 16, 2016 3:21 am

Help Please. For the life of me I cannot figure why this errors out:

Code: Select all

on mouseUp
   put field "scriptHolder" into theScript
   repeat with x = 2 to 118
     set the script of the btn x to theScript
   end repeat
end mouseUp
rebuilding visual programming app originally created in 1993 as Hypercard stack

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10330
Joined: Wed May 06, 2009 2:28 pm

Re: scripting the script of a btn?

Post by dunbarx » Sat Jul 16, 2016 4:57 am

Hi.

I did not make a test card with 118 buttons, but did try it with three. No problems. May I assume you actually have all 118 buttons resident?

What is the error you are getting?

Craig Newman

Pistris
Posts: 148
Joined: Mon Jul 20, 2015 2:40 am

Re: scripting the script of a btn?

Post by Pistris » Sat Jul 16, 2016 5:00 am

First check the spelling of the field name, make sure is right

Second I think instead of BTN you should have BUTTON

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10330
Joined: Wed May 06, 2009 2:28 pm

Re: scripting the script of a btn?

Post by dunbarx » Sat Jul 16, 2016 5:25 am

Hi.

"button", "BUTTON", "btn", whatever, are all synonyms.

But one thing I never do, though it seems to not throw an error, is to use the construction "the field" or "the button" when saying something like

"set the whatever of the btn buttonReference to..."

I never use the superfluous "the". I think this is bad form, in that "the" should indicate a property, not an object reference.

Craig
Last edited by dunbarx on Sat Jul 16, 2016 5:29 am, edited 1 time in total.

Pistris
Posts: 148
Joined: Mon Jul 20, 2015 2:40 am

Re: scripting the script of a btn?

Post by Pistris » Sat Jul 16, 2016 5:27 am

Did you tried it?

melristau
Posts: 56
Joined: Tue Jul 14, 2015 5:15 pm
Contact:

Re: scripting the script of a btn?

Post by melristau » Sat Jul 16, 2016 5:28 am

Thanks!

Tried it with 3 buttons as well and worked fine.

This is the error i'm getting on the 118 button cd:

image "distributeScript": execution error at line 4 (Chunk: no such object), char 1
rebuilding visual programming app originally created in 1993 as Hypercard stack

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10330
Joined: Wed May 06, 2009 2:28 pm

Re: scripting the script of a btn?

Post by dunbarx » Sat Jul 16, 2016 5:31 am

Hmmm.

So do you have 118 buttons? You must, you know, or the handler is sooner or later going to fail when it cannot find, say, button 109, because there isn't one.

Craig

melristau
Posts: 56
Joined: Tue Jul 14, 2015 5:15 pm
Contact:

Re: scripting the script of a btn?

Post by melristau » Sat Jul 16, 2016 5:36 am

Weird.
I change the repeat to 1 to 13 and it works fine.
Change back to 1 to 118 and it errors out.
rebuilding visual programming app originally created in 1993 as Hypercard stack

Lagi Pittas
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 366
Joined: Mon Jun 10, 2013 1:32 pm

Re: scripting the script of a btn?

Post by Lagi Pittas » Sat Jul 16, 2016 6:41 am

Hi

First off place a put x in the loop to see where it fails adjust x to find which one. If you can see all 118 buttons chances are you have made some into a group and the number of the buttons in the group will start from 1 again

Lagi

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: scripting the script of a btn?

Post by [-hh] » Sat Jul 16, 2016 11:01 am

Code: Select all

on mouseUp
   put fld "scriptHolder" into theScript
   repeat with x = 2 to min(118,the num of btns)
     set script of btn x to theScript
   end repeat
end mouseUp
shiftLock happens

melristau
Posts: 56
Joined: Tue Jul 14, 2015 5:15 pm
Contact:

Re: scripting the script of a btn?

Post by melristau » Sat Jul 16, 2016 4:33 pm

When assigning the behavior of buttons 2-118 to btn 1 I see that the btn names do not correspond to bracketed number.
(I used Align Selected Controls and Duplicate Objects to build the matrix of buttons.)
Maybe I need to rebuild by adding one button at a time?
Attachments
behaviorBtnList.gif
rebuilding visual programming app originally created in 1993 as Hypercard stack

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: scripting the script of a btn?

Post by FourthWorld » Sat Jul 16, 2016 5:29 pm

Integers can be problematic as object names, since the syntax for ordinal references and name references is the same.

For example, if you have a button named "44" and it's the second of a dozen buttons on the card, this:

Code: Select all

get the long id of btn 2
...will work, but this:

Code: Select all

get the long id of btn 44
...will not, since LC will see the integer and attempt to find the object by its ordinal number, and since only a dozen buttons exist on the card there is no button 44.

When a layout can benefit from having names that include numbers, this can be done safely when the name also includes any alphabetic characters, thus forcing LC to treat it as a string, e.g. "b44".

This will allow unique naming of sets of objects created via scripts, but without the ambiguity introduced by using purely integer names, e.g.:

Code: Select all

repeat with i = 1 to 100
    set the name of the template button to "b"& i
    create button
end repeat
All that said, in some cases involving large numbers of similar objects you may find you don't need to name them at all, since using "the target" or "me" will allow you to identify a specific button the user is interacting with.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

melristau
Posts: 56
Joined: Tue Jul 14, 2015 5:15 pm
Contact:

Re: scripting the script of a btn?

Post by melristau » Sat Jul 16, 2016 7:37 pm

That makes perfect sense now...
I've deleted all but 1 button, added 2 more but as shown in attached the stack shows memory of the number of buttons previously used. Each new button I create is an addition to the 118 btn sequence. How to purge?

Ultimately, I need to use the button's position within a 9x13 matrix of buttons to look at the compatibility of images of its surrounding buttons. Previously I used the button name (1,2,3,4, etc.) in simple math to accomplish this.
So, if I name the 118 buttons x1, x2, x3, etc., and use the script below, am I creating a future problem for myself?

Code: Select all

on mouseUp
   global gIconName,gIconNumber
   put the short name of img id (the icon of me) into gIconName
   put the short name of me into gIconNumber
   put "" into char 1 of gIconNumber
   add 0 to gIconNumber -- force to number?
   pass mouseUp
end mouseUp
rebuilding visual programming app originally created in 1993 as Hypercard stack

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: scripting the script of a btn?

Post by [-hh] » Sun Jul 17, 2016 1:13 am

You get the numbers you need as follows.

Code: Select all

on mouseUp
  global gIconName, gIconNumber
  put the short name of img id (the icon of me) into gIconName
  put char 2 to -1 of the short name of me into gIconNumber # <--
  pass mouseUp
end mouseUp
melristau wrote:How to purge?

Code: Select all

on mouseUp
  lock screen; lock messages
  repeat with i=999 down to 119
    if there is a btn ("x"&i) then delete btn ("x"&i)
  end repeat
  unlock screen; unlock messages
end mouseUp
Instead of create btn ("x"&i) you could write

Code: Select all

if there is no btn ("x"&i) then create btn ("x"&i)
Then you don't have to purge.
shiftLock happens

Post Reply