Page 1 of 2

renaming a group

Posted: Mon Jun 16, 2014 12:03 am
by jalz
Hi all,

How do I rename a group? I've got the following code which selects the fields on a layout, then groups them but I need to rename that group into something more meaningful. So far I have tried the following

---
//attempt 1
group "grp_card1"
---
//attempt 2
group
set the name of group to "grp_card1"
--
//attempt 3
group
set the name of last group to "grp_card1"
// This renames an group I pasted in before I selected objects to be in my new group
--
//attempt 4
group
set the name of it to "grp_card1"


I've run out of other ideas/concepts to try. I'm using the keyword group to group my objects, whats the next line of code I need to change the name of the newly created group into something more meaningful.

Many thanks
Jalz

Re: renaming a group

Posted: Mon Jun 16, 2014 12:28 am
by [-hh]
..........

Re: renaming a group

Posted: Mon Jun 16, 2014 4:54 am
by dunbarx
Hi.

There is an anomaly (bug?) in LC that makes the "last" keyword unstable with groups. It works fine for all other objects. There are several threads about this, going back a year or three. Check the user notes in the dictionary under the "last" keyword. (they are mine).

So it is perfectly fine to say:

set the name of the last field to "myLastField"

where that field might have been created, say, under script control. But you cannot trust:

set the name of the last group to "myLastGroup"

The best way out of this is to name the templateGroup. The "last" group thus formed will take that property, and that is how you can isolate it and deal with it as you wish. Of course, you should change the name of that last group, so that any subsequently created groups will be able, in their turn, to be identified in the same way.

Write back if none of this makes sense.

Craig Newman

Re: renaming a group

Posted: Mon Jun 16, 2014 3:39 pm
by jalz
Hi Craig,

Yes, I think I'm coming across that bug. I cant rely on the command
set the name of the last group to "myLastGroup" as that is renaming another group on my layout, and I have several grouped objects.

What I want to do in a script is select all objects and then simply group and then name that group. I've got everything apart from renaming the group.

I have not really looked into templateGroup, so didn't quite understand your solution - yet. I'll try and see if I can find some info in the forums. Pity LC has this anomaly, as the info I found, set the name of the last group to "myLastGroup" seemed to be the answer.

Thanks

Jalz

Re: renaming a group

Posted: Mon Jun 16, 2014 3:46 pm
by FourthWorld
Craig, that sounds like a bug to me. Did you file a report?

Re: renaming a group

Posted: Mon Jun 16, 2014 4:42 pm
by jacque
It may not be a bug if there is a background group in place, where the last group would be the background one. If that's the case then asking for the "last card group" instead of just the "last group" should work.

Re: renaming a group

Posted: Mon Jun 16, 2014 7:25 pm
by bn
Craig,

if you file a bug report could you propose that Livecode returns the group involved in "group" in the it variable. Analoguous to "create".
Or is there something I am missing that prevents LC to put the long id of the group that groups into it?

Kind regards
Bernd

Re: renaming a group

Posted: Mon Jun 16, 2014 9:08 pm
by jalz
Hi all,

Just thought I'd recreate a dummy card with objects and a script similar to the one I am working on so you guys can verify whether its a bug or me. I was trying to group all the objects and then put a v scrollbar around the group. Notice how it groups everything as it should, but renames a different group when I use the set name ..last... command.

Thanks
Jalz

Re: renaming a group

Posted: Mon Jun 16, 2014 9:26 pm
by [-hh]
..........

Re: renaming a group

Posted: Mon Jun 16, 2014 9:36 pm
by bn
Hi Jalz,

either do as Hermann suggest with the highest group (did not try this) or do as I suggest look for the highest ID of all group and that should be the one just created to group your groups.

Code: Select all

on mouseUp
   repeat with x = 1 to the number of controls
      put "field,button,graphic,image" into tObjects
      put the long id of control x into tLong
      if tLong contains "group" AND  word 1 of tLong is among the items of tObjects then
         next repeat
      else
         set the selected of the long id of control x to true
      end if
      
   end repeat
   group
   
   -- get the highest id of groups, since "group" created a new group that is the one we are looking for
   repeat with i = 1 to the number of groups of this card
      put the short id of group i & comma after tCollect
   end repeat
   delete last char of tCollect -- a return
   put max (tCollect) into tHighestID -- here we extract the highest id
   set the name of  group id tHighestID to "grp_card1"
   set the vScrollbar of group "grp_card1" to true
end mouseUp
Kind regards
Bernd

Re: renaming a group

Posted: Mon Jun 16, 2014 9:53 pm
by Simon
This is going on way too long:

Code: Select all

   set the name of the templateGroup to "Misery"
   group
   --set the name of the last group to "grp_card1"
   set the vScrollbar of group "Misery" to true
Sorry Craig.

Now in your stack that header is wide so you wont see the vertical scrollbar unless you increase the size of the stack.

Now from this is the "template object", just about everything has one and it is used when "create" or in this case "group" is used. New objects are created using their "template", so you just set it up before making the new object.

Easy yes?

Simon

Re: renaming a group

Posted: Mon Jun 16, 2014 10:01 pm
by [-hh]
..........

Re: renaming a group

Posted: Mon Jun 16, 2014 10:03 pm
by jalz
:oops: Sorry all! Its now working, with Simons latest addition - so embarrassed
Bernd, your code looking for the highest grouped number and then applying a scrollbar also seems to work

Re: renaming a group

Posted: Mon Jun 16, 2014 10:20 pm
by Simon
Hi jalz,
No Worries, you've sparked an interesting conversation, without it I would never have seen Craig's post. Now it's stuck in my head for something to watch out for.

Simon

Re: renaming a group

Posted: Mon Jun 16, 2014 10:43 pm
by [-hh]
..........