Cloning a template card with code

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: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Cloning a template card with code

Post by mrcoollion » Thu Sep 17, 2015 6:19 pm

I am trying to clone a card I use as a template to a new card with a new name.
And I also need to delete a card of which I know the name

I use the code:
clone card "Template"
And this works but the name of the clone is identical to the original (duhh :shock: ).
After cloning I do not know which card to change the name of?

Is there a way to give the new card name with the clone or other command?

Any idea's?

Regards,

Paul (mrcoollion)

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am
Location: Bordeaux, France

Re: Cloning a template card with code

Post by Dixie » Thu Sep 17, 2015 6:31 pm

Clone the card and then set the name of 'the last card of this stack' to whatever you want to call it... :-)

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

Re: Cloning a template card with code

Post by dunbarx » Thu Sep 17, 2015 6:38 pm

Hi.

Go to the dictionary, and look up the keyword "last". This is so useful, but may not help here at all.

So why mention it? Only because it is very convenient when doing what you intended with controls on a card, since any newly created control is automatically the "last" on. (Do watch out when using this keyword for groups. It is not stable). But this may not work with cards, since you can create (or clone) a new card in the middle of other cards in a stack, and that reference is likely broken.

However, if you set the name of the templateCard to something unique, or to something that follows a sequence you have already pre-arranged, you can then identify it and find it. You will already have set its name, and you can even find it within any other cards in the stack, and do what you will...

As regards deleting a named card, why not just delete the card? (Back to the dictionary?)

Craig Newman

mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Cloning a template card with code

Post by mrcoollion » Thu Sep 17, 2015 7:03 pm

Dixie wrote:Clone the card and then set the name of 'the last card of this stack' to whatever you want to call it... :-)
I already tried this and it does change a card name but unfortunately it does not change the correct card but another card already existing :( .
I used the statement: 'set the name of the last card of this stack to thenewcardname' which is the same as you suggested.

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am
Location: Bordeaux, France

Re: Cloning a template card with code

Post by Dixie » Thu Sep 17, 2015 7:06 pm

Yes, 'last' is very useful... and it does help here !
Changing the name of the card directly after issuing the clone command works here... I've just tried it cloning the last card of a stack and cloning one in the middle of a stack of cards...

mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Cloning a template card with code

Post by mrcoollion » Thu Sep 17, 2015 7:10 pm

Tried the following:

Code: Select all

 clone card "Template"
   set the name of card "Template" of this stack to thenewcardname
This works. It probably changes the first card it finds that is named Template but that does not matter because they are clones anyway.

Thanks for the help.

:D

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

Re: Cloning a template card with code

Post by FourthWorld » Thu Sep 17, 2015 7:49 pm

"last" will refer to the last card in a stack, which may not be the newly created one if the creation happened while on any but the last card.

However, once a card is create it becomes the current card, so a reference to it can be obtain with "this", e.g.:

get the long id of this cd
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: Cloning a template card with code

Post by dunbarx » Thu Sep 17, 2015 7:52 pm

Guys.

The" last" card is almost never the "latest" card. It is not sound, and certainly not robust, to rely on this sort of thinking.

This is, as I said above, OK with controls. since any newly created button, say, is always the last button.

But with cards, this is not necessarily true at all, since a card can be pasted, created or cloned into the middle of a stack. Don't do it.

Craig

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am
Location: Bordeaux, France

Re: Cloning a template card with code

Post by Dixie » Thu Sep 17, 2015 7:53 pm

Richard... I am not seeing that. If the card is cloned and then renamed directly on the next line, I am seeing that it is the last card that has taken the name and it is the clone...

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

Re: Cloning a template card with code

Post by dunbarx » Thu Sep 17, 2015 8:00 pm

Richard's point, that you can rely on a newly created, cloned or pasted card to be the current card in the defaultStack has merit. Well and good, though it requires that you work on that card in that place; changing its name, say.

Just don't rely on "last" when dealing with copies of cards.

Craig

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

Re: Cloning a template card with code

Post by dunbarx » Thu Sep 17, 2015 8:04 pm

No.
Richard... I am not seeing that. If the card is cloned and then renamed directly on the next line, I am seeing that it is the last card that has taken the name and it is the clone...
Make a stack with several cards. Find a way to show their numbers. Go to a middle card. Put a button on it, and this in the button script:[codeon mouseUp
clone this cd
set the name of last card to "mmm"
end mouseUp.[/code]

Now check out the name of the card you are on (the cloned card) and see if its name is "mmm"

Craig

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

Re: Cloning a template card with code

Post by dunbarx » Thu Sep 17, 2015 8:23 pm

Rereading Richard and myself, I wonder if there isn't a misunderstanding. It seems that everyone knows that the cloned card is the current card, and that the last card is the last card, and that the two may be, but usually will not be, the same card.

So, use the templateCard, or work directly and immediately on the current card, but do not use the last card. Right?

Craig

mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Cloning a template card with code

Post by mrcoollion » Wed May 20, 2020 7:35 pm

One can clone a card immediately into another name with

Code: Select all

clone this card as "NewCardname"

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

Re: Cloning a template card with code

Post by dunbarx » Wed May 20, 2020 8:03 pm

Certainly. It is one of the things that makes cloning so useful. Copying has more bells and whistles, but is not able to set the name of the copy.

But I still emphasize one has to watch card order. If you have a stack of ten cards, and you clone cd #5, the clone will be cd # 6, not cd 11.

Not the last card.

Craig
Last edited by dunbarx on Wed May 20, 2020 8:21 pm, edited 1 time in total.

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

Re: Cloning a template card with code

Post by Klaus » Wed May 20, 2020 8:15 pm

One can also check IT!
From the dictionary about "clone":
...
IT: The clone command places the long id property of the newly created object in the it variable.
...

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”