5 Replies Latest reply on Dec 2, 2015 8:45 AM by philipl99145132

    Script Making Unwatted Formatting Changes

    NovakDamien

      I picked up the following search and replace script online which works as inteded, except that it removes any italics I have and makes all of my text bold. Is there something I need to add/remove/replace to make it stop doing this?

       

      Edit: It's doing even more in some files, including changing the size of my text. Also, it is only making these changes to any text box that has a string to be replaced.

       

      function myReplace(search_string, replace_string) {
          var active_doc = app.activeDocument;
          var text_frames = active_doc.textFrames;
          if (text_frames.length > 0)
          {
              for (var i = 0 ; i < text_frames.length; i++)
              {
                  var this_text_frame = text_frames[i];
                  var new_string = this_text_frame.contents.replace(search_string, replace_string);
                  if (new_string != this_text_frame.contents)
                  {
                      this_text_frame.contents = new_string;
                      }
                  }
              }
          }
      myReplace(/1\/8/gi, "\u215B" );
      
        • 1. Re: Script Making Unwatted Formatting Changes
          Muppet Mark Level 5

          That script replaces the whole string contents of the frame… I've not used it so I can't say for definate but would expect the replaced formatting to be based on the first character of the frame…?

          1 person found this helpful
          • 2. Re: Script Making Unwatted Formatting Changes
            CarlosCanto Adobe Community Professional & MVP

            if the Font Set to be changed does not have the character "\u215B", then yes, it will do wacky things to the text frame contents, what options does the system have?

             

            Try it with Arial, it all should be fine.

            • 3. Re: Script Making Unwatted Formatting Changes
              NovakDamien Level 1

              Thanks MuppetMark, I did some testing and this certainly seems to be the case. Would you possibly be able to point me towards a script that replaces only the found text, or otherwise preserves formatting? I've unfortunately never worked with JavaScript before, so I'd have no idea where to begin or what to look for in the script.

              • 4. Re: Script Making Unwatted Formatting Changes
                Muppet Mark Level 5

                I did try to make one myself a while back… And got side tracked with other script projects… Rather than deal with the text at textFame.contents level the script would need to go down textRanges where you can deal with words or characters… I don't know of an existing script…

                • 5. Re: Script Making Unwatted Formatting Changes
                  philipl99145132

                  Faced with this situation i wrote this script, no guarantees its bulletproof or fast.

                   

                  // https://www.adobe.com/content/dam/Adobe/en/devnet/pdf/illustrator/scripting/cs6/Illustrator-Scripting-Reference-JavaScript.pdf
                  var wrong = 'replace';
                  var correct = 'with';
                  var active_doc = app.activeDocument;
                  var text_frames = active_doc.textFrames;
                  var buffer = '';
                  if (text_frames.length > 0) {
                      for (var i = 0; i < text_frames.length; i++) {
                          buffer = '';
                          var sortThese = [];
                          for (var j = 0; j < text_frames[i].textRanges.length; j++) {
                              buffer = buffer + text_frames[i].textRanges[j].contents;
                              if (buffer.length > wrong.length) {
                                  buffer = buffer.slice(wrong.length * -1);
                              }
                              if (buffer === wrong) {
                                  var position = 0;
                                  for (var k = j - (wrong.length - 1); k <= j; k++) {
                                      if (typeof correct[position] !== 'undefined') {
                                          text_frames[i].textRanges[k].contents = correct[
                                              position];
                                      } else if (wrong.length > correct.length) {
                                          sortThese.push(k);
                                      }
                                      position++;
                                  }
                                  if (correct.length > wrong.length) {
                                      sortThese.push(k - 1);
                                  }
                              }
                          }
                          if (wrong.length > correct.length) {
                              for (var j = sortThese.length - 1; j >= 0; j--) {
                                  text_frames[i].textRanges[sortThese[j]].remove();
                              }
                          } else if (correct.length > wrong.length) {
                              for (var j = sortThese.length - 1; j >= 0; j--) {
                                  for (var k = correct.length; k >= (wrong.length + 1); k--) {
                                      text_frames[i].textRanges[sortThese[j]].duplicate(
                                          text_frames[i].textRanges[sortThese[j]],
                                          ElementPlacement.PLACEAFTER).contents = correct[k -
                                          1];
                                  }
                              }
                          }
                      }
                  }