4 Replies Latest reply on Mar 22, 2016 6:45 AM by foltman

    find/change conditional text problem

    foltman Level 1

      Hi!

      For couple of hours I'm trying to solve this case: I want to apply conditions (conditional text) to all paragraphs according to applied style name. So for example text with paragraph style named "BODY" should have "BODY" conditional text etc. I've figured out mechanism, but I can't resolve problem with passing it to find/change.

      Here is my code:

       

      (function() {
          if (app.documents.length > 0) {
              processDocument(app.documents[0]);
          }
          function processDocument(aDoc) {
              var paraStyles = aDoc.allParagraphStyles.sort();
              for (var j = 0; paraStyles.length > j; j++) {
                  var currentStyle = paraStyles[j].name;
                  if(aDoc.conditions.item (currentStyle) == null){
                      var varConditions = aDoc.conditions.add({name: currentStyle});
                  }
                  doFindChange(currentStyle);
              }
          }
      }())
      function doFindChange(currentStyle) {
          app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
          
          app.findGrepPreferences.appliedParagraphStyle = currentStyle;
          app.changeGrepPreferences.appliedConditions = currentStyle;
          
          app.activeDocument.changeGrep();
      }
      

       

      I know where is the mistake, but I have no idea how to change it.

      Could anyone help me?

       

      Thanks in advance!

        • 1. Re: find/change conditional text problem
          TᴀW Adobe Community Professional & MVP

          You've got a few problems in the code:

           

          1. Pass the actual paragraph style (not just its name) to findGrepPreferences (your currentStyle variable is a string; although this may work, it's better [especially for para styles in groups] to pass the actual paragraph style itself).

          2. Ditto for the appliedCondition: give changeGrepPreferences the condition itself, not just its name.

          3. It seems that changeGrepPreferences.appliedConditions is expecting an array of conditions, so give it an array even if it has just one member.

           

          Ariel

          • 2. Re: find/change conditional text problem
            foltman Level 1

            Yeah!

            Ariel, I've changed it like you said and it works. Thank you!

             

            My new code: (I know it is little bit messy, now I will clean it out).

             

            var  q = [];

            (function() {

                if (app.documents.length > 0) {

                    processDocument(app.documents[0]);

                }

                function processDocument(aDoc) {

                    var paraStyles = aDoc.allParagraphStyles.sort();

                    for (var j = 0; paraStyles.length > j; j++) {          

                        var currentStyle = paraStyles[j];

                        var currentStyleName = paraStyles[j].name;          

                        if(aDoc.conditions.item (currentStyleName) == null){

                            var varConditions = aDoc.conditions.add({name: currentStyleName});

                        }      

                        q.push({

                            strChangeTo: {appliedConditions: [currentStyleName]}

                        });      

                        doFindChange(currentStyle, q[j].strChangeTo);

                    }

                }

            }())

            function doFindChange(currentStyle, conditonName) {

                app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;  

                app.findGrepPreferences.appliedParagraphStyle = currentStyle;

                app.changeGrepPreferences.properties = conditonName;  

                app.activeDocument.changeGrep();  

                app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

            }

            • 3. Re: find/change conditional text problem
              TᴀW Adobe Community Professional & MVP

              Great, glad to have helped.

               

              You've made it more complicated perhaps than it needs to be (building and passing objects, using changeGrepPreferences.properties) -- but perhaps you're preparing the groundwork for a more complicated script?

               

              Re. point (3) above, I was thinking of just:

              app.changeGrepPreferences.appliedConditions = [myCondition];

               

              Ariel

              • 4. Re: find/change conditional text problem
                foltman Level 1

                Ariel, indeed I'm planning make something more complicated with this, but for now it should be simple.

                So finally (hope) I've made it like this:

                 

                (function() {

                    if (app.documents.length > 0) {

                        processDocument(app.documents[0]);

                    }

                    function processDocument(aDoc) {

                        var paraStyles = aDoc.allParagraphStyles.sort();

                        for (var i = 0; paraStyles.length > i; i++) {

                         

                            var currentStyle = paraStyles[i];

                            var myCondition = paraStyles[i].name;

                         

                            if(aDoc.conditions.item (myCondition) == null){

                                var varConditions = aDoc.conditions.add({name: myCondition});

                            }

                 

                            app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

                 

                            app.findGrepPreferences.appliedParagraphStyle = currentStyle;

                            app.changeGrepPreferences.appliedConditions = [myCondition];

                 

                            app.activeDocument.changeGrep();

                 

                            app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

                 

                        }

                    }

                }())

                 

                It works fine. So again - thanks for all tips and advices.