Page 1 of 1

scripting the script of a btn?

Posted: Sat Jul 16, 2016 3:21 am
by melristau
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

Re: scripting the script of a btn?

Posted: Sat Jul 16, 2016 4:57 am
by dunbarx
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

Re: scripting the script of a btn?

Posted: Sat Jul 16, 2016 5:00 am
by Pistris
First check the spelling of the field name, make sure is right

Second I think instead of BTN you should have BUTTON

Re: scripting the script of a btn?

Posted: Sat Jul 16, 2016 5:25 am
by dunbarx
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

Re: scripting the script of a btn?

Posted: Sat Jul 16, 2016 5:27 am
by Pistris
Did you tried it?

Re: scripting the script of a btn?

Posted: Sat Jul 16, 2016 5:28 am
by melristau
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

Re: scripting the script of a btn?

Posted: Sat Jul 16, 2016 5:31 am
by dunbarx
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

Re: scripting the script of a btn?

Posted: Sat Jul 16, 2016 5:36 am
by melristau
Weird.
I change the repeat to 1 to 13 and it works fine.
Change back to 1 to 118 and it errors out.

Re: scripting the script of a btn?

Posted: Sat Jul 16, 2016 6:41 am
by Lagi Pittas
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

Re: scripting the script of a btn?

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

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

Re: scripting the script of a btn?

Posted: Sat Jul 16, 2016 4:33 pm
by melristau
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?

Re: scripting the script of a btn?

Posted: Sat Jul 16, 2016 5:29 pm
by FourthWorld
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.

Re: scripting the script of a btn?

Posted: Sat Jul 16, 2016 7:37 pm
by melristau
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

Re: scripting the script of a btn?

Posted: Sun Jul 17, 2016 1:13 am
by [-hh]
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.