2 Replies Latest reply on Mar 23, 2016 10:51 AM by K.Daube

    Timing problem ?

    K.Daube Level 1

      Test, test, test...

      In my script at the place where I find a string and replace it by a paragraph contens, it turns out that the very first operation is not performed.

      I inserted an alert to find out whether the item can really be found - and voilà the function is OK.

      Then I replace the alert by a Console command - and it still works.

      Removing this line resuls in loosing the first find/replace.

      What's going on here?

      The following is just part of the function:

      ...
        findParams = GetFindParams (findString);        // Get the find parameters for finding string, case sensitive
      //FA_errno = Constants.FE_Success;                // errno global, to be used to track the progress of the find and replace
        tr = targetDoc.Find(tr.beg, findParams);        // and do an initial find to get started.
      // find and replace loop as long as we keep finding
      Console ("textrange selected ?");
        while(FA_errno === Constants.FE_Success && loopCounter++ < 2*loopMax) { 
          targetDoc.TextSelection = tr;                 // set up the text range to clear the original text
          targetDoc.Clear(0);                           // clear it
          app.ActiveDoc = sourceDoc;                    // switch to sourceDoc
          sourceDoc.TextSelection = trReplace;          // to be able to get the replacement text
          sourceDoc.Copy(0);
          app.ActiveDoc = targetDoc;                    // switch to targetDoc
          targetDoc.Paste(0);                           // and paste the replacement text
          ReApplyFontAndSize (targetDoc, tr);             // re-apply the current para format
      ...
      
        • 1. Re: Timing problem ?
          Klaus Göbel Level 3

          Hi Klaus,

           

          the problem is:

          Console ("textrange selected ?");

           


          That leads to FA_errno = 0, so if an error occured before it is set to 0 and this line

          while(FA_errno === Constants.FE_Success && loopCounter++ < 2*loopMax) {  


          doesn't get the error.



          • 2. Re: Timing problem ?
            K.Daube Level 1

            Klaus, your remark confirm my findings:

            Having replaced the assignments

            FA_errno = Constants.FE_Success;
            
            

            with

            InitFA_errno ();
            
            

            led to a correct behaviour of the script. The original logic is from Russ Ward at https://forums.adobe.com/thread/895626. BTW the small function is:

            function InitFA_errno() {
            // Reset FA_errno as it is write protected
            // see https://forums.adobe.com/thread/962910
              app.GetNamedMenu("!MakerMainMenu");            //If this fails, you've got bigger problems
            }
            
            

             

            Before I had the idea to replace the assignment of the FA_errno I tried the 'various methods offered on the net' for setting a sleep timer. These all created a hanging FM, although cpu usage was very moderate.

            So now the comple function FindAndReplacePara is this

             

            function FindAndReplacePara (targetDoc, findString, sourceDoc, trReplace, loopMax) {
            // ----------------------------------------------- Replace string by textrange (part of pgf)
            // FindText, ConsiderCase; Function returns replacemtCounter
            // Since we have collected only in doc.MainFlowInDoc, we replace only in this flow
            // loopMax used as emergency back door - twice the # of initially found citations
            // We do not have an initial TextSelection, hence start directly at the first para
            // Source: script by Russ Ward in https://https://forums.adobe.com/thread/895626; 2081392
            //        and Rick Quatro
              var tr = new TextRange();
              var restoreTR, frame = 0, loopCounter = 0, replacementCounter = 0;
              var findParams = new PropVals();
              var firstPgf = targetDoc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
            
              tr.beg.obj = tr.end.obj = firstPgf;            //  set up the starting text range as the very beginning
              tr.beg.offset = tr.end.offset = 0;              // of the flow. We'll move straight from beginning to end.
              trSaved = tr                                    // to come back after work
            //Set up find parameters. We want to configure it to look for a string and perhaps be case sensitive.
            //We don't need the find to wrap because we are controlling the flow from beginning to end.
              findParams = GetFindParams (findString);        // Get the find parameters for finding string, case sensitive
            
            //  FA_errno = Constants.FE_Success;              // errno global, to be used to track the progress of the find and replace
              InitFA_errno ();                                // reset - it is write protected
              tr = targetDoc.Find(tr.beg, findParams);        // and do an initial find to get started.
                                                              // find and replace loop as long as we keep finding
              while(FA_errno === Constants.FE_Success && loopCounter++ < 2*loopMax) { 
                targetDoc.TextSelection = tr;                // set up the text range to clear the original text
                targetDoc.Clear(0);                          // clear it
                app.ActiveDoc = sourceDoc;                    // switch to sourceDoc
                sourceDoc.TextSelection = trReplace;          // to be able to get the replacement text
                sourceDoc.Copy(0);
                app.ActiveDoc = targetDoc;                    // switch to targetDoc
                targetDoc.Paste(0);                          // and paste the replacement text
                ReApplyFontAndSize (targetDoc, tr);            // re-apply the current para format
            
            //  FA_errno = Constants.FE_Success;              // ...  find the next instance. We'll reset FA_errno again just in case
                InitFA_errno ();                              // reset - it is write protected
                tr = targetDoc.Find(tr.beg, findParams);      // something screwy happened while we were replacing text.
              }
              targetDoc.ScrollToText(trSaved);                // we're done. Restore the document to it's original area of display
              replacementCounter = loopCounter;
              return replacementCounter;
            } // --- end FindAndReplacePara