Creating Stack via script help?

J. Landman Gay jacque at hyperactivesw.com
Sat Nov 20 01:01:21 EST 2004


On 11/19/04 4:09 PM, John Patten wrote:

> I?m attempting to create a script, ?fishing through many of the
> similar questions in the archive.
> 
> I?ve been getting all kinds of strange results. I can get it to work,
> sort of,  but it seems to create multiple instances of the new stack
> at times.  And when I go into the Application Browser I see even more
> of my inept attempts at getting this to work.

I don't see anything in your script that would cause multiple stacks to 
be created, so what I suspect is happening in that you aren't deleting 
your previous trials before starting a new one. Closing a stack won't 
necessarily delete it from memory; you must specifically remove it. You 
can do that by choosing "Close and remove from memory" from the File 
menu, or by right-clicking the stack in the app browser and choosing the 
same thing from the contextual menu.

>  Here?s what I?ve been trying to do:
 >
> On mouseDown
> Ask file ?Save stack as:? --set path and filename for new stack
> Put it into theNewStackname
> Ask ?How many questions/cards??
> Put it into x
> Create stack theNewStackName with background ?quizTemplate?
> Set the defaultstack to theNewStackName
> Put 1 into y
>  Repeat x
>     Clone this card  
>      Put y into cd fld ?TheCardNumber? --assuming that when you clone a card it becomes current card
>      Add 1 to y
>  End repeat
> Put ?? into x
> Put ?? iinto y
> Put ?? into theNewStackname
> End mousedown

I'll have a go at this. First, I'd not trigger the action on a mousedown 
event; that prevents the user from changing their mind. Using mouseup is 
usually preferable, because it allows the user to click down on a 
button, change their mind, and slide off again without committing to the 
action. So I'd do this:

on mouseUp
   ask file "Save stack as:" -- get path
   if it = "" then exit mouseup -- no name entered, or clicked "cancel"
   Put it into theStackPath
   ask "How many questions/cards?"
   if it = "" then exit mouseup -- let them back out here too
   Put it into x

The "ask file" command returns a path (note it does not save the file to 
disk; it just gives you back a path) but you probably don't want the 
whole thing in the stack's title bar, so I'd extract a shorter name from 
the path to use as the stack name itself:

  set the itemDelimiter to "/"
  put last item of theStackPath into theNewStackName -- get a short name
  create stack theNewStackName with background "quizTemplate"
  set the defaultstack to theNewStackName

Now to make the cards. Creating the stack already made one card, so we 
need one less than the user specified when running the repeat loop. We 
don't need to set a counting variable ("y" in your script) because we 
can use a built-in counter in the repeat loop itself. We do need to put 
a "1" into the field on the first card though, since we won't start 
counting until card 2:

  put "1" into fld "theCardNumber" -- on first card
  repeat with count = 2 to x -- we already have the first card, so start 
with 2
    create card
    put count into fld "theCardNumber"
  end repeat

I usually use "create card" rather than "clone". I suppose this is 
personal preference, but it seems cleaner to me. A newly created card 
will automatically include the background (provided the group's 
backgroundBehavior is set to true,) so unless you need to copy over 
specific card properties, "create" works fine. If you've set up a bunch 
of card properties that you want to retain though, then cloning is one 
way to carry them over and you could use that instead. Another (possibly 
preferable) way to set up card properties is by setting properties in 
the templatecard before creating new cards.

Finally, the stack hasn't been saved to disk yet. We have a file path 
chosen by the user, but we haven't done anything with it. So the last 
steps are to save the stack to disk:

  set the filename of this stack to theStackPath
  save this stack
end mouseUp

That's all you need to do, the handler can end here. You don't need to 
clean up any variables or memory, because local variables are 
automatically poofed out of existence when the handler ends. There isn't 
anything left to clean up. (This isn't true of script-local variables or 
global variables, but your handler doesn't have those.)

The final script looks like this then:

on mouseUp
   ask file "Save stack as:" -- get path from user
   if it = "" then exit mouseup -- escape clause
   put it into theStackPath -- store path here for later
   ask "How many questions/cards?"
   if it = "" then exit mouseup -- let them back out here too
   Put it into x -- store card count
   set the itemDelimiter to "/"
   put last item of theStackPath into theNewStackName -- get a short name
   create stack theNewStackName with background "quizTemplate"
   set the defaultstack to theNewStackName
   put "1" into fld "theCardNumber" -- on first card
   repeat with count = 2 to x -- start creating at 2nd card
     create card -- automatically adopts background
     put count into fld "theCardNumber" -- current card is latest one
   end repeat
   set the filename of this stack to theStackPath
   save this stack -- saves to disk
end mouseUp

> Eventually, I would like to create the new stack from a standalone,
> so I?m also concerned that there will be other issues I need to be
> aware of when doing this from a standalone? True? If so, like what?

It should work fine from a standalone.

-- 
Jacqueline Landman Gay         |     jacque at hyperactivesw.com
HyperActive Software           |     http://www.hyperactivesw.com


More information about the use-livecode mailing list