Method to rename stack

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller, kevinmiller

Post Reply
mvillion
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 74
Joined: Sun Feb 24, 2013 12:29 pm

Method to rename stack

Post by mvillion » Tue Jun 12, 2018 1:07 pm

Hi

Is there a method to rename a stack and still preserve 'behaviour's?

Problem
If I rename a stack just by giving it a new name in the properties of the stack, all my behaviours no longer work as they reference the name of the stack. I cannot see how to do a global replace.

Reason
My stack has a sucky name! I did not realise it would be 'sticky'

Thanks

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2198
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Method to rename stack

Post by jmburnod » Tue Jun 12, 2018 1:36 pm

Hi.
Is there a method to rename a stack and still preserve 'behaviour's?
Yes,
When you define a button script as behavior this is the long id of the button and its change if you change the name of your stack.
I've taken the habit to reset behavior links at open stack/card.
Best regards
Jean-Marc

mvillion
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 74
Joined: Sun Feb 24, 2013 12:29 pm

Re: Method to rename stack

Post by mvillion » Tue Jun 12, 2018 3:22 pm

Hi Jean-Marc

Ok. You have my interest.

Do you script the updating of behaviours? If so, it means you will need to keep a list of all objects that have behaviours associated so the script can perform the re-association. Is that what you meant?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5394
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Method to rename stack

Post by dunbarx » Tue Jun 12, 2018 4:18 pm

You have my interest as well.

Firstly, I only just found out that behaviors may now (since v.8?) reference a stack as well as a button. Good. I always thought a stack was far more appropriate than a button anyway.

The "long ID" as relates to behaviors neglects the full pathname, rather stopping at the stack name. The dictionary states that this is to allow a stack to be moved and not break the behavior reference. Well and good.
The value of the behavior property is a reference to a button or stack containing the script to use. The format stored in the object it's assigned to is similar to a long ID. The main difference is that where a long ID includes the full path to the stack file, the form stored in the behavior includes only the stack name, allowing the reference to continue to work after the stack file has been moved to another computer. If you set the behavior to a long ID, LiveCode converts it to a rugged form without the stack file path.
But doesn't this mean that one can manipulate a stack-referenced behavior:

Code: Select all

set the behavior of button "yourButton" to the long id of stack the name of this stack
or
set the behavior of button "yourButton" to the long id of stack the mainStack of this stack
That sort of thing?

Craig Newman

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5962
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Method to rename stack

Post by FourthWorld » Tue Jun 12, 2018 4:26 pm

mvillion wrote:
Tue Jun 12, 2018 1:07 pm
My stack has a sucky name!
"There are only two hard things in Computer Science: cache invalidation and naming things."
- Martin Fowler
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4460
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Method to rename stack

Post by jacque » Tue Jun 12, 2018 5:31 pm

That happened to me once and I had to go through the whole stack and reset the behaviors. I complained and people agreed that relative references would be better, but then LC introduced stacks as behavior containers and that was the end of it.

If you're pretty sure you won't be changing the name again you can edit everything manually. For automated updating, you'll need to scan every control in the stack and if it has a behavior, replace the old stack name with the new one and set the behavior to the edited reference.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2198
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Method to rename stack

Post by jmburnod » Tue Jun 12, 2018 10:40 pm

Hi mvillion,
Do you script the updating of behaviours?
Yes,
As Jacqueline explaned
When i use a behavior I reset behavior of controls which have a behavior
Behavior are scripts of btns named "behaviorName_BEH" of a card "cMyBEH"

I use this script at the first opening of one card:

Code: Select all

on firstOpenCD
   set the behavior of group "grPictosMes" to the long id of btn "bPictoMes_BEH" of cd "cMyBEH"
end firstOpenCD
I only tested this method with a single stack app

You will save a lot of time by applying the motto recalled by Richard 8)

Best regards
Jean-Marc

mvillion
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 74
Joined: Sun Feb 24, 2013 12:29 pm

Re: Method to rename stack

Post by mvillion » Tue Jun 12, 2018 11:22 pm

Ok. This is interesting
but then LC introduced stacks as behavior containers and that was the end of it.
I am not sure what this means.
Currently, I have a card with different buttons with my behaviours in them and I reference the behaviours via that mechanism.
If you're pretty sure you won't be changing the name again you can edit everything manually.
Hmm. The name is sucky but not that sucky. 8-D
For automated updating, you'll need to scan every control in the stack and if it has a behavior, replace the old stack name with the new one and set the behavior to the edited reference.
Okay. Not sure what is meant by scan. Manually or is there an automated way to find them?

My current references look like this
button id 6379414 of stack "Sucky Stack name"

You mention set the behaviour 'to the edited reference.' What do you mean by this?

I understand I could place a script on every card to recode each behaviour and I think I will do that going forwards as it is a good coding model.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4460
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Method to rename stack

Post by jacque » Wed Jun 13, 2018 7:53 am

mvillion wrote:
Tue Jun 12, 2018 11:22 pm
but then LC introduced stacks as behavior containers and that was the end of it.
I am not sure what this means.
When behaviors were only stored in buttons, it would be possible to make the behavior reference relative to the stack by omitting the stack name. But later, whole stack scripts were introduced as containers for behaviors, and in that case you really do need the stack name. So relative references weren't persued.
For automated updating, you'll need to scan every control in the stack and if it has a behavior, replace the old stack name with the new one and set the behavior to the edited reference.
Okay. Not sure what is meant by scan. Manually or is there an automated way to find them?
Basically you use a repeat loop to look at every object on each card and see if it has a behavior assigned. If so, you edit the behavior to include the new stack name. You ony need to do that once after changing the name. Or if you only have a few objects with behaviors, you can just write a script that lists them and changes each one in the list.
You mention set the behaviour 'to the edited reference.' What do you mean by this?
Something like this:

Code: Select all

get the behavior of btn "xyz"
replace "suckyName" with "goodName" in it
set the behavior of btn "xyz" to it
I understand I could place a script on every card to recode each behaviour and I think I will do that going forwards as it is a good coding model.
It's really only necessary once, unless the stack name is going to change a lot. Jean-Marc's method is insurance but I don't do it that way. Usually I just run a temporary handler to reassign the behavior buttons.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

mvillion
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 74
Joined: Sun Feb 24, 2013 12:29 pm

Re: Method to rename stack

Post by mvillion » Wed Jun 13, 2018 9:13 am

I just want to say thanks to everyone who has contributed on this and all my other threads. I would be lost without you. :D

One script per card that checks for behaviours is a neat way to do it
get the behavior of btn "xyz"
replace "suckyName" with "goodName" in it
set the behavior of btn "xyz" to it
So now, one last question on this thread. How would you (by script) find all of the objects on a card (without having to define the list)? I can't think of a way. [Now I am sure someone has figured this out too and when I see it I will go doh! I was thinking, if there is a way to enumerate child objects then the script could be constructed to enumerate the cards in a stack then the objects in the cards and thus a single script to rename all behaviours!
[Surely it can't be that easy]!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5394
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Method to rename stack

Post by dunbarx » Wed Jun 13, 2018 3:26 pm

Hi.

"All the objects on a card" can be translated to "All the controls on a card"

Then you can (pseudo):

Code: Select all

repeat with y = 1 to the number of controls
  doSomeThingToEachControlIfRequired
  
Craig Newman

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2198
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Method to rename stack

Post by jmburnod » Wed Jun 13, 2018 6:33 pm

Hi mvillion
...if there is a way to enumerate child objects...
Yes, something like that:

Code: Select all

on setAllBeh pOldName,pNewName
   set the lockmessages to true
   repeat with u = 1 to the num of cds
      open cd u
      put the num of controls into tNbC
      repeat with i = 1 to tNbC
         get the  behavior of control i 
         if it <> empty then
            replace pOldName with pNewName in it
            set  the behavior of control i  to it
         end if
      end repeat
   end repeat
end setAllBeh
Best regards
Jean-Marc

mvillion
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 74
Joined: Sun Feb 24, 2013 12:29 pm

Re: Method to rename stack

Post by mvillion » Wed Jun 13, 2018 9:34 pm

Sweet.

I did not know that LC had 'number of controls'. I should have known. 8-D

'Copy -> paste' into my code snippet bin.

Many thanks

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”