9 Replies Latest reply on Sep 16, 2016 11:48 AM by Obi-wan Kenobi

    find/change script highlight found text

    jakec88782761 Level 1

      Hello,

       

      I've written this script and it works great. But what I'd like to do is go through each found word individually (like in the find/change dialog with the button 'find next').

       

      This is a snippet:

       

      app.findTextPreferences=app.changeTextPreferences=null;
      var what = app.findTextPreferences.findWhat="ensure";

      app.changeTextPreferences.changeTo="make sure";
      var found =app.activeDocument.changeText();
      if (found.length=1){
      alert(found.length+" instance of 'ensure' was changed");
      }else{
         alert(found.length+" instances of 'ensure' were changed");
        }

       

      I want the script to make each instance of "ensure" my active selection so I can see where they are before I change them.

       

      Thanks

        • 1. Re: find/change script highlight found text
          Skemicle Level 3

          I believe scripting find/change changes all instances in one action, making it very improbable if not impossible to select each instance individually before making the change.. I would use something like:

           

          var doc = app.activeDocument;

          var txts = doc.textFrames;

          var x = 0;

          for(c=0;c<txts.length;c++){

              words = txts[c].words;

              for(c=0;c<words.length;c++){

                  if(words[c].contents.toLowerCase() == "ensure"){

                      app.selection = words[c];

                      app.layoutWindows[0].zoomPercentage = app.layoutWindows[0].zoomPercentage;

                          if(confirm("Do you wish to change this instance?",undefined, "Find/Replace") == true){

                              words[c].contents = "make sure";

                              x++

                          }

                  }app.selection = null;

              }

          }alert(x + " changes were made");

           

           

           

           

          Though for some reason I am only to get this to run through only 1 text frame. Maybe you or someone else can fix that flaw =)

          • 2. Re: find/change script highlight found text
            Kai Rübsamen Level 4

            Hi Guys,

             

            try this one:

             

            app.findTextPreferences = app.changeTextPreferences = null;
            app.findTextPreferences.findWhat = "ensure";
            app.changeTextPreferences.changeTo = "make sure";
            
            var allFounds = app.activeDocument.findText();
            for (var i = 0; i < allFounds.length; i++) {
              var curFound = allFounds[i];
              curFound.select();
              app.layoutWindows[0].zoomPercentage = app.layoutWindows[0].zoomPercentage;
               if(confirm("Do you wish to change this instance?",undefined, "Find/Replace")) {
                 curFound.changeText();
              }
            }
            
            app.findTextPreferences = app.changeTextPreferences = null;
            

             

            Disadvantage of the confirm-dialog: There is no exit-button .

            • 3. Re: find/change script highlight found text
              jakec88782761 Level 1

              Thanks for the replies guys,

              Your scripts are great, but now I'm trying to add functionality to it so that you can skip to the next one without the dialog closing.

               

              To do this I'm using ScriptUI.

               

              I'm trying to add the function clickNext to the next button but it doesn't work.

               

              Do you know where I'm going wrong?

               

              var w = new Window('dialog','Plain English Find and Change');

              var check1 = w.add('checkbox',undefined,'ensure to make sure');
              var check2 = w.add('checkbox',undefined,'utilise to use');

              var find = w.add('button',undefined,'Find');
              var change = w.add('button',undefined,'Change');
              var next = w.add('button',undefined,'Next');
              var previous = w.add('button',undefined,'Previous');

               

                 
                

              var clickNext = function () {
              if (check1.value=true) {
              app.findTextPreferences = app.changeTextPreferences = null; 
              app.findTextPreferences.findWhat = "ensure"; 
              app.changeTextPreferences.changeTo = "make sure"; 
              var allFounds = app.activeDocument.findText();
                
              for (var i = 0; i < allFounds.length; i++) { 
                var curFound = allFounds[i]; 
                curFound.select(); 
                app.layoutWindows[0].zoomPercentage = app.layoutWindows[0].zoomPercentage;
                }

              }

              };
              next.onClick=clickNext();

              • 4. Re: find/change script highlight found text
                Stefan Rakete Level 2

                If you want to keep the window open you need a palette:

                 

                var w = new Window('palette','Plain English Find and Change');

                • 5. Re: find/change script highlight found text
                  Skemicle Level 3

                  I'm not sure if this is the best way to get to your desired result but i will put in my opinion anyway. =)

                  If you are wanting to loop through each instance of the word manually then there really is no need for a for() loop. Also, as

                   

                  #targetengine "session";

                  var dialog = new Window('palette {orientation:"column"}', "Find and Change")

                  with(dialog){

                      var change = add('button', undefined, "Change");

                      var next = add('button', undefined, "Next");

                      var prev = add('button', undefined, "Previous");

                  }app.findTextPreferences.findWhat = "found";

                  app.changeTextPreferences.changeTo = "changed";

                  var allFounds = app.activeDocument.findText();

                  var c=0;

                  app.selection = allFounds[c];

                  app.layoutWindows[0].zoomPercentage = app.layoutWindows[0].zoomPercentage;

                  dialog.show()

                  change.onClick = function(){allFounds[c].changeText(); c++; app.selection = allFounds[c]; app.layoutWindows[0].zoomPercentage = app.layoutWindows[0].zoomPercentage};

                  next.onClick = function(){c++; app.selection = allFounds[c]; app.layoutWindows[0].zoomPercentage = app.layoutWindows[0].zoomPercentage};

                  prev.onClick = function(){c--; app.selection = allFounds[c]; app.layoutWindows[0].zoomPercentage = app.layoutWindows[0].zoomPercentage};

                   

                  Though, I'm questioning as to why you need this as a script rather than going ahead and using the Find/Change dialog box?

                  • 6. Re: find/change script highlight found text
                    jakec88782761 Level 1

                    This looks good but I have the same problem that I had with my scripts-the buttons do not work.

                     

                    Any ideas?

                    • 7. Re: find/change script highlight found text
                      Obi-wan Kenobi Adobe Community Professional

                      Hi,

                       

                      I don't really understand what you mean!

                       

                      I often do this: Write, e.g., a regex: Find something and replace by some other thing!

                      So, I place my smart fingers on the keyboard and …:

                       

                      Ctrl-[num]1: jump to the first occurrence => if I want to replace => Ctrl-[num]2; if not, Ctrl-[num]1, … and so on!

                       

                      I'm not Mozart! … but I can play some cool music with my keyboard!

                       

                      (^/) 

                      • 8. Re: find/change script highlight found text
                        Skemicle Level 3

                        try adding this bit of code to set user interaction before the dialog.

                         

                        app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;

                        • 9. Re: find/change script highlight found text
                          Obi-wan Kenobi Adobe Community Professional

                          Thanks Jarek! 

                           

                          (^/)