Where is stack?

Robert Brenstein rjb at robelko.com
Wed Feb 21 00:26:18 EST 2007


At 22:49 -0800 19/02/07, Bill Vlahos wrote:
>I want to be able to have the user easily make a copy of the stack 
>they are working on as a way to backup the stack. This would be 
>something like "Save a Copy".
>
>However, I don't want to change the active path of the stack. I 
>still want the stack they are working on to stay put and be the 
>working stack.
>
>The following button script doesn't work and gives this error
>	Chunk: source is not a container
>
>on mouseUp
>   answer folder "Select destination folder for backup: "
>   if it is not "Cancel" then
>     revCopyFile this stack, it
>   end if
>end mouseUp
>
>How can I make this work?
>
>Bill Vlahos

Bill , this should work better and ensure that backup works 
regardless whether the button is in a mainstack or substack:

on mouseUp
   answer folder "Select destination folder for backup: "
   if the result is "Cancel" then exit mouseUp
   revCopyFile (the effective fileName of this stack), it
end mouseUp

Note that button clicked is returned in the result not in it.

If you want to ensure that the working directory is not changed add:

on mouseUp
   answer folder "Select destination folder for backup: "
   if the result is "Cancel" then exit mouseUp
   put the defaultFolder into d
   revCopyFile (the effective fileName of this stack), it
   set the defaultFolder to d
end mouseUp

I'd actually save the backup folder location, so it is asked only 
when it is not set or when user holds the option/alt key down. It 
quickly becomes boring to have to choose backup folder each time 
unless backup is done sporadically only.

on mouseUp
   if the backupFolderPath of me is empty or the optionKey is down then
      answer folder "Select destination folder for backup:"
      if the result is "Cancel" then exit mouseUp
      set the backupFolderPath of me to it
   else
      get the backupFolderPath of me
   end if
   revCopyFile (the effective fileName of this stack), it
   save this stack
end mouseUp

Another variation is to hold the shift key down to first save the 
stack. It can also be reversed, not saving when the key is held. 
Personally, I like to be able to backup without saving the stack in 
memory. It happens often enough that I start making changes and then 
realize that I may want to roll them back and, of course, I forgot to 
create a backup.

on mouseUp
   answer folder "Select destination folder for backup: "
   if the result is "Cancel" then exit mouseUp
   if the shiftKey is down then save this stack
   revCopyFile (the effective fileName of this stack), it
end mouseUp

Finally, since a folder can contain only a single copy of a file with 
the same name and you want to keep a single backup, you'd need to 
delete the previous file.

on mouseUp
   answer folder "Select destination folder for backup: "
   if the result is "Cancel" then exit mouseUp
   if there is a file it then delete file it
   revCopyFile (the effective fileName of this stack), it
end mouseUp

However, having multiple backup files from the way project develops 
is often desired. This makes the script a tad more complicated as you 
need to keep track of the last backup number and insert it into the 
filename.

on mouseUp
   if the backupFolderPath of me is empty or the optionKey is down then
      answer folder "Select destination folder for backup: "
      if the result is "Cancel" then exit mouseUp
      if the backupFolderPath of me is not it then put set the 
backupNumber of me to "0"
      set the backupFolderPath of me to it
   else
      get the backupFolderPath of me
   end if
   put (the backupNumber of me)+1 into b
   put the number of chars of it into p
   repeat with i=p down to 1
     if char i of it is "." then
       put i into p
       exit repeat
     end if
   end repeat
   put "-" & (char 1 to (4-length(b)) of "00000") & b into bb
   if p is the number of chars of it then
     put bb after it
   else
     put bb before char p of it
   end if
   if there is a file it then delete file it -- should check for errors
   if the shiftKey is down then save this stack
   revCopyFile (the effective fileName of this stack), it
   if the result is empty then
      set the backupNumber of me to b
   else
      answer error "Error backing up:" && result()
   end if
   save this stack -- need to preserve backup path and counter
end mouseUp

This handler assumes an OS that allows long file names. It inserts a 
4-digit serial number before prefix. The counter is reset if a new 
backup folder is selected.

If your button is in a stack that users of your program will use, you 
may of course let them decide which of these options are active.

Robert



More information about the use-livecode mailing list