How to group multiple objects by scripts?

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
penopia
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 25
Joined: Fri Apr 25, 2008 2:46 pm

How to group multiple objects by scripts?

Post by penopia » Wed Mar 05, 2014 2:52 pm

Hi ^^

I'm trying to group multiple buttons by scripts.
The following scripts works;

group button 1 and button 2

But, following scripts make error. saying (Chunk: error in object expression) near "button 1 and button 2",

put "button 1 and button 2" into tButtonList
group tButtonList

How should I put [ObjectList] after group command to select multiple objects ?

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: How to group multiple objects by scripts?

Post by Thierry » Wed Mar 05, 2014 3:28 pm

penopia wrote:Hi ^^
How should I put [ObjectList] after group command to select multiple objects ?
Hi,

From the dictionnary and down to User contribution,
I found:

Code: Select all

tAPKS =
button id 1282 of card id 1002 of stack "drag" of stack XXXXX
button id 1283 of card id 1002 of stack "drag" of stack XXXXX
button id 1284 of card id 1002 of stack "drag" of stack XXXXX
button id 1285 of card id 1002 of stack "drag" of stack XXXXX
button id 1286 of card id 1002 of stack "drag" of stack XXXXX

Code: Select all

put the number of lines of the tAPKS of stack "drag" into tNum //Get number of lines to process
put 1 into tLine //start from line 1
put line tLine of the tAPKS of stack "drag" into tList //put the first long id from tAPKS into the tList variable
repeat until tLine = tNum //Use a repeat function that allows you to go through every line of tAPKS
   put tLine + 1 into tLine //Use a simple counter for the repeat function
   put " and " & line tLine of the tAPKS of stack "drag" after tList //Add and "and" (standard grouping syntax) aswell as the next line of tAPKS to the tList variable
end repeat
group tList //Group everything in the tList variable
Best,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

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

Re: How to group multiple objects by scripts?

Post by Klaus » Wed Mar 05, 2014 3:34 pm

Hi penopia,

you need to tell the engine to actually DO/Execute the supplied "string"
and not handle it as a simple string, which it is in fact :D

Your example is one of the rare cases where one has to use DO. This works:

Code: Select all

on mouseup
   put "button 1 and button 2" into tButtonList
   do ("group" && tButtonList)
end mouseUp
Best

Klaus

penopia
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 25
Joined: Fri Apr 25, 2008 2:46 pm

Re: How to group multiple objects by scripts?

Post by penopia » Thu Mar 06, 2014 2:32 am

Thanks Klaus.
You gave me the very simple and perfect solution !
(I can not fully understand the reason to use "do" script, but I work :D )

on mouseUp
local tButtonList
put the name of btn 1 into tButtonList
put " and " after tButtonList
put the name of btn 2 after tButtonList
do("group" && tButtonList)
put the long name of btn 1 into fld 1
end mouseUp

gave me the followings in fld 1:

button "Button" of group id 1007 of card id 1002 of stack "Untitled 1"

To Tierry;

Thanks for your advice.
The following scripts also doesn't work. I think group command does not accept 'string list' as a parameter regardless of the infotmation in it, as Klaus said.

on mouseUp
local tButtonList
put the long name of btn 1 into tButtonList
put " and " after tButtonList
put the long name of btn 2 after tButtonList
group tButtonList
end mouseUp

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: How to group multiple objects by scripts?

Post by Thierry » Thu Mar 06, 2014 8:00 am

penopia wrote:I think group command does not accept 'string list' as a parameter..
Umm, I was only quoting "the wrong code" from the dictionary! :evil:

Glad that Klaus gave you the solution :)

Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

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

Re: How to group multiple objects by scripts?

Post by Klaus » Thu Mar 06, 2014 1:16 pm

Hi friends,

you can also use a list like Thierry posted earlier to use it to group all object in it!
But in a slightly other way :D

Using Thierrys example variable: tAPKS =
button id 1282 of card id 1002 of stack "drag" of stack XXXXX
button id 1283 of card id 1002 of stack "drag" of stack XXXXX
button id 1284 of card id 1002 of stack "drag" of stack XXXXX
button id 1285 of card id 1002 of stack "drag" of stack XXXXX
button id 1286 of card id 1002 of stack "drag" of stack XXXXX

Code: Select all

...
## You can also set the SELECTED property of any object instead of "select btn xyz"
repeat for each line i in tAPKS
  set the selected of i to TRUE
end repeat

## Now everything is selected:
GROUP

## Clean up:
select empty
...
:D


Best

Klaus

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: How to group multiple objects by scripts?

Post by Thierry » Thu Mar 06, 2014 2:47 pm

Klaus wrote:Hi friends,
Using Thierrys example variable:

Code: Select all

...
GROUP
...
Hi Klaus,
I prefer to give credit where it is due, especially when it's not working :)
so this code sample comes from the Dictionary!

Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

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

Re: How to group multiple objects by scripts?

Post by Klaus » Thu Mar 06, 2014 2:52 pm

Ahm yes, found it in the dictionary. OK, tAPKS was custom prop in that example.

As the poster wrote: "Here's an inefficient but quick way to group a list of long ids."
He is right :D

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: How to group multiple objects by scripts?

Post by Thierry » Thu Mar 06, 2014 3:06 pm

Klaus wrote:As the poster wrote: "Here's an inefficient but quick way to group a list of long ids."
He is right :D
Klaus,
You win the gold medal of "marketing"
Here is your price: http://www.ina.fr/video/I06268518 ... in French :)

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

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

Re: How to group multiple objects by scripts?

Post by Klaus » Thu Mar 06, 2014 3:18 pm

Merci, mon ami! :D

But my french is not good enough to understand that guy.
And from watching I confess I don't find him funny at all 8)

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: How to group multiple objects by scripts?

Post by Thierry » Thu Mar 06, 2014 3:30 pm

Klaus wrote:Merci, mon ami! :D
But my french is not good enough to understand that guy.
ah, humour.. so difficult to share.. :)

Best,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3990
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: How to group multiple objects by scripts?

Post by bn » Thu Mar 06, 2014 3:48 pm

There is a subtle difference between the two ways of grouping objects by script.

the first

Code: Select all

on mouseup
   put "button 1 and button 2" into tButtonList
   do ("group" && tButtonList)
end mouseUp
works but not on objects already grouped

the second

Code: Select all

...
## You can also set the SELECTED property of any object instead of "select btn xyz"
repeat for each line i in tAPKS
  set the selected of i to TRUE
end repeat

## Now everything is selected:
GROUP

## Clean up:
select empty
...
also works on objects within a group if they are within the same subgroup

Kind regards
Bernd

penopia
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 25
Joined: Fri Apr 25, 2008 2:46 pm

Re: How to group multiple objects by scripts?

Post by penopia » Sat Mar 08, 2014 8:04 am

Hi,
Thanks for letting me know another cool way to group multiple objects by scripts. For the beginners like me, I will write again simple scripts.

In case there are 3 buttons, and you want group all of these buttons;

repeat with i=1 to 3
set the selected of button i to true
end repeat
group

What a nice use of property "selected" !!!
Thank all of you again for getting me out of troubles.

TorstenHolmer
Posts: 57
Joined: Mon Oct 28, 2013 1:23 pm
Location: Dresden, Germany

Re: How to group multiple objects by scripts?

Post by TorstenHolmer » Sun Sep 02, 2018 10:30 am

Hi,

I had the same question and found a good hint in the famous book "Cooking with HyperCard" frm Dan Winkler and Scott Knaster, in which are recipies for doing the same thing to a group of objects.

The key is "repeat with i = 1 to the number of buttons" where you don't have to specify each button in advance.

I am using this script in order to create a cardboard-like interface where buttons or fields can be moved around the card and the canvas can be bigger than the visible card size.

Code: Select all


on mouseUp
   
   lock screen
   if the number of groups > 0 then exit mouseup
   
   repeat with i = 1 to the number of buttons
      set the selected of button i to TRUE
   end repeat
   GROUP
   
   select empty
   unlock screen
   
end mouseUp

Cheers,
Torsten

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”