//----------------------------------------------------------------------------
//   FrameScript for replacing text in all documents of a book.
//
//   Requirements: FrameMaker 5.5.6, FrameScript 1.2 or later.
//
//   Most of this script is copyright by Elmsoft (producers of FrameScript).
//   This version is published with Elmsoft's permission; it was written
//   (and tested) with the Macintosh beta version of FrameScript.
//   I used one of the example scripts included with FrameScript as a
//   template to call FrameMaker's fcodes for replacing text in each 
//   document in a book. So a few lines are copyright by me...
//
//   The fcodes were transferred directly from an AppleScript that does
//   the same thing on a Macintosh version of FrameMaker 5.5.6.
//
//   The same limitations apply as with the AppleScript: fcodes simulate
//   user input (key presses); for the case of replacing text, the script 
//   can't get any return code from set search dialog, so the Cancel button
//   will not work! Therefore a dialog is added that asks if the user is
//   sure to proceed (no undo is possible after a global replace process).
//   FrameMaker will still ask for each document whether to go ahead with
//   replacing text; click on Cancel to skip this document. Replacing will
//   continue with the next file in the book. You will also see a dialog
//   if FrameMaker hasn't found any occurrences in the current document;
//   simply press the OK button (the only one in that dialog) to continue.
//
//   Advantage when compared to the original script: replacing by clipboard 
//   or by applying character format is possible, just as in the standard
//   find and replace dialog in FrameMaker. Disadvantage: user has to dismiss
//   lots of dialogs...
//   Note that this version doesn't save any of the changed documents; it
//   will simply leave all documents of this book open.
//
//
//   Author: Markus Frischknecht  mfrischknecht@access.ch
//   Version 1.0  August 1999
//
//   Use at your own risk!
//
//----------------------------------------------------------------------------

WRITE console '*****************STARTING Book Find and Replace*******************'

IF fslVersionMajor < 1 or fslVersionMinor < 2
   MSGBOX 'Requires FrameScript Version 1.2 or greater';
   LEAVESUB;
ENDIF

IF ActiveBook = 0
   MsgBox 'No Active Book'
   LeaveSub;
ENDIF

SET BookObj = ActiveBook;

SET ChgFromStr = '';
SET ChgToStr = '';

SET FirstBook = 1;

LOOP ForEach(BookComponent) In(BookObj) LoopVar(bookcomp)
   RUN isDocOpenAlready filename(bookcomp.Name) returns DocObj(dobj);
   IF dobj = 0
        SET errorcode = 0;
        OPEN Document File(bookcomp.Name) FileIsOldVersion FontNotFoundInDoc NewVar(dobj);
        IF errorcode not 0
           DISPLAY  'Error opening component-'+bookcomp.Name+'  Msg-'+ErrorMsg;
        ENDIF

   ENDIF


	IF FirstBook = 1
		// open first doc of book
		// set search and replace text

		Execute Fc KbdSetSearch;
		SET FirstBook = 0;
		
		// Ask if continue with replace, using MsgBox expr [Mode(modetype)] [Button(buttonpushedvar)];
		
		MsgBox 'Replace in all documents of the book '+BookObj.Name+'?' Mode(OkCancel)
			Button(btnvar);
		If btnvar = CancelButton
				CLOSE Document DocObject (bookcomp.Name) //close this doc again
			    LEAVESUB;	// stop script
		EndIf

	ENDIF	// FirstBook


   IF dobj not = 0
      SET docchgcount = 0;
        // fcode for replace in whole file; asks for confirmation!
		Execute Fc KbdRGlobal;

      WRITE console 'Changes in doc ('+dobj.Name+') ='+docchgcount;
   ENDIF
ENDLOOP

MSGBOX 'Finished Replacing In Book.'

write console '*****************ENDING*******************'


//----------------------------------------------------------------------------
//   This subroutine checks the list of open documents to see if the 
//   specified document is already open.
//    
//   Format:
//       Run IsDocAlreadyOpen Filename(testfilename) returns DocObj(retdocvar)
//
//    If it is open, it returns the document object
//    and returns zero if it is not open.
//----------------------------------------------------------------------------
SUB isDocOpenAlready using filename DocObj

   GET String FromString (filename) Uppercase NewVar(tfilename);  // Upper case the string
   SET DocObj = 0;
   LOOP foreach(Doc) In(Session) LoopVar(testdocobj)
      GET String FromString (testdocobj.Name) Uppercase NewVar(tname); // Upper case the string
      IF tname = tfilename
         SET DocObj = testdocobj;
         LEAVELOOP;
      ENDIF
   ENDLOOP

ENDSUB
//--------------------------------------------------------------------------------