-- the scripts of all objects on the pages (backgrounds) for removal.
-- The scripts of viewers are not modified because TB 1.53 doesn't
-- support viewers.

to handle add_tags
   local REM

   set sysCursor to 4  -- show hourglass
   put   "-- REMOVE THIS SCRIPT" into REM
-- mark the book script

   if  script of this book <> null \
   and first textline of script of this book <> REM then
      put REM & crlf before first textline of script of this book
   end if

-- for all pages of this book

   step p from 1 to pagecount of this book

   -- mark the script of this page

     if  script of page p <> null \
     and first textline of script of page p <> REM then
        put REM & crlf before first textline of script of page p
     end if

     put objects of page p into objs

  -- for all objects of this page

     step i from 1 to itemcount (objs)

     -- mark the script of this object

        if  script of item i of objs <> null \
        and first textline of script of item i of objs <> REM then
          put REM & crlf before first textline of script of item i of objs
       end if

     -- if this object is a group

        if "group" is in item i of objs
           send nested (item i of objs)
        end if
     end step
   end step

-- for all backgrounds of this book
   put 0 into num
   step b from 1 to backgroundcount of this book
      do
        clear sysError
        set sysSuspend to FALSE
        get name of background id num
        set sysSuspend to TRUE
        if sysError <> null
           increment num
        end if
      until sysError is null

   -- mark the script of this background

      if  script of background id num <> null \
      and first textline of script of background id num <> REM then
         put REM & crlf before first textline of script of background id num
      end if

      put objects of background id num into objs

   -- for all objects of this background

      step i from 1 to itemcount (objs)

      -- mark the script of this object

         if  script of item i of objs <> null \
         and first textline of script of item i of objs <> REM then
            put REM & crlf before first textline of script of item i of objs
         end if

      -- if this object is a group

         if "group" is in item i of objs
            send nested (item i of objs)
         end if
      end step
      increment num
   end step
   set sysCursor to 1
end add_tags

to handle nested grp
   local REM

   put   "-- REMOVE THIS SCRIPT" into REM

   put objects of grp into grps
   step g from 1 to itemcount (grps)
      if  script of item g of grps <> null \
      and first textline of script of item g of grps <> REM then
          put REM & crlf before first textline of script of item g of grps
      end if
      if "group" is in item g of grps
         send nested (item g of grps) -- a little recursion here
      end if
   end step
end nested

-- dump all the scripts of a book (objects, pages, backgrounds) to a text
-- file (same name as the book with an extension of 'txt' or 'sxt').
-- This is used to verify 'add_tags' inserted comments as requested.
-- Order of scripts is; book, background, objects of background,
-- page, objects of page. All backgrounds will appear before any page.

to handle dumpScript
   local tag

   put ">>-- script of" into tag
   set sysCursor to 4  -- indicate something is happening
   put name of this book into fileName
   clear last char of fileName
   clear last char of fileName
   put "xt" after last char of fileName
   createFile fileName

   writeFile tag && this book & crlf to fileName
   if script of this book <> null
      writeFile script of this book to fileName
   end if

-- for all backgrounds of this book

   put 0 into num
   step b from 1 to backgroundcount of this book
      do
        clear sysError
        set sysSuspend to FALSE
        get name of background id num
        set sysSuspend to TRUE
        if sysError <> null
           increment num
        end if
      until sysError is null

      if script of background id num <> null
         writeFile crlf & tag && background id num & crlf to fileName
         writeFile name of background id num & crlf to fileName
         writeFile script of background id num to fileName
      end if

      put objects of background id num into objs

   -- for all objects of this background

      step i from 1 to itemcount (objs)
         if script of item i of objs <> null
            writeFile crlf & tag && item i of objs & crlf to fileName
            writeFile object of item i of objs && \
                      name   of item i of objs &  crlf to fileName
            writeFile script of item i of objs to fileName
         end if

      -- if this object is a group

         if "group" is in item i of objs
            send dumpNested (item i of objs), fileName
         end if
      end step
      increment num
   end step

-- for all pages of this book

   step p from 1 to pagecount of this book
      if script of page p <> null
         writeFile crlf & tag && page p & crlf to fileName
         writeFile "Page:" && name of page p & crlf to fileName
         writeFile script of page p to filename
      end if

      put objects of page p into objs

   -- for all objects of this page

      step i from 1 to itemcount (objs)
         if script of item i of objs <> null
            writeFile crlf & tag && item i of objs & crlf to fileName
            writeFile object of item i of objs && \
                      name   of item i of objs &  crlf to fileName
            writeFile script of item i of objs to fileName
         end if

      -- if this object is a group

         if "group" is in item i of objs
            send dumpNested (item i of objs), fileName
         end if
      end step
   end step

   closeFile fileName
   set sysCursor to 1
end dumpScript

to handle dumpNested grp, fileName
   local tag
   put ">>-- script of" into tag
   put objects of grp into grps
   step g from 1 to itemcount (grps)
      if script of item g of grps <> null
         writeFile crlf & tag && item g of grps & crlf to fileName
         writeFile object of item g of grps && \
                   name   of item g of grps &  crlf to fileName
         writeFile script of item g of grps to fileName
      end if
      if "group" is in item g of grps
         send dumpNested (item g of grps) -- a little recursion here
      end if
   end step
end dumpNested

