Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Combine Find & Replace Code for Illustrator

    Would it be easy & if so, could anyone show me how I would combine all of these lines of code into a simpler & maybe faster working script. Instead of just repeating it over & over. Thanks for looking!

    Code:
    var active_doc = app.activeDocument;
     
    var search_string = /_/gi; // g for global search, remove i to make a case sensitive search
    var replace_string = " ";
     
    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;
                   }
          }
    }
    
    var active_doc = app.activeDocument;
     
    var search_string = /ASY - /gi; // g for global search, remove i to make a case sensitive search
    var replace_string = "ASY ";
     
    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;
                   }
          }
    }
    
    var active_doc = app.activeDocument;
     
    var search_string = /ASY /gi; // g for global search, remove i to make a case sensitive search
    var replace_string = "ASY - ";
     
    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;
                   }
          }
    }
    
    var active_doc = app.activeDocument;
     
    var search_string = /ASY-/gi; // g for global search, remove i to make a case sensitive search
    var replace_string = "ASY - ";
     
    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;
                   }
          }
    }
    
    var active_doc = app.activeDocument;
     
    var search_string = /ASSY - /gi; // g for global search, remove i to make a case sensitive search
    var replace_string = "ASSY ";
     
    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;
                   }
          }
    }
    
    var active_doc = app.activeDocument;
     
    var search_string = /ASSY /gi; // g for global search, remove i to make a case sensitive search
    var replace_string = "ASY - ";
     
    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;
                   }
          }
    }
    
    var active_doc = app.activeDocument;
     
    var search_string = /ASSY-/gi; // g for global search, remove i to make a case sensitive search
    var replace_string = "ASY - ";
     
    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;
                   }
          }
    }
    
    var active_doc = app.activeDocument;
     
    var search_string = /DS-C1/gi; // g for global search, remove i to make a case sensitive search
    var replace_string = "";
     
    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;
                   }
          }
    }
    
    var active_doc = app.activeDocument;
     
    var search_string = /DSC1/gi; // g for global search, remove i to make a case sensitive search
    var replace_string = "";
     
    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;
                   }
          }
    }
    
    var active_doc = app.activeDocument;
     
    var search_string = /MCA-/gi; // g for global search, remove i to make a case sensitive search
    var replace_string = "-";
     
    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;
                   }
          }
    }
    
    var active_doc = app.activeDocument;
     
    var search_string = /MCA/gi; // g for global search, remove i to make a case sensitive search
    var replace_string = "";
     
    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;
                   }
          }
    }
    
    var active_doc = app.activeDocument;
     
    var search_string = /PAF-/gi; // g for global search, remove i to make a case sensitive search
    var replace_string = "";
     
    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;
                   }
          }
    }
    
    var active_doc = app.activeDocument;
     
    var search_string = /PAF/gi; // g for global search, remove i to make a case sensitive search
    var replace_string = "";
     
    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;
                   }
          }
    }
    
    var active_doc = app.activeDocument;
     
    var search_string = /FDR-/gi; // g for global search, remove i to make a case sensitive search
    var replace_string = "";
     
    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;
                   }
          }
    }
    
    var active_doc = app.activeDocument;
     
    var search_string = /FDR/gi; // g for global search, remove i to make a case sensitive search
    var replace_string = "";
     
    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;
                   }
          }
    }
    
    var active_doc = app.activeDocument;
     
    var search_string = / (VIEW)/gi; // g for global search, remove i to make a case sensitive search
    var replace_string = "";
     
    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;
                   }
          }
    }

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Functions are the key to avoid code repetition
    Code:
    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(/_/gi, " ");
    myReplace(/ASY - /gi, "ASY ");
    ...

  • Users who have thanked devnull69 for this post:

    BrianEdL (11-12-2012)

  • #3
    New Coder
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts
    THANK YOU! Works like magic.

  • #4
    New Coder
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quick question, when this script runs does it run in the order from top to bottom the top myReplace through to the bottom myReplace?

  • #5
    New Coder
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I've been working hard on learning Regular Expressions & here is what I have so far.
    Code:
    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(/([NW]\d{6})-S[_-]/i, "$1-S??? ");
    myReplace(/_/gi, " ");
    myReplace(/(\d{6})-?\d{7}\/\d\d?\d?-/i, "-$1-");
    myReplace(/-INS-\d\d\/\d\d?-/i, "* ");
    myReplace(/ ASSEMBLY/gi, " ASY");
    myReplace(/ ***?Y+$| ***?Y - | ***?Y -| ***?Y | ***?Y- | ***?Y-/gi, " ASY - ");
    myReplace(/MCA-|DSC1-|DSC1|DS-C1-|DS-C1/i, "-");
    myReplace(/^DS-|^DI-|^PH-|MCA|^PAF-|^PAF|^FDR-|^FDR/i, "");
    myReplace(/ ?\(VIEW\)| ?\(VIE[W?)?]| ?\(VIE+$| ?\(VI+$| ?\(V+$| ?\(+$/i, "");
    myReplace(/([NW]\d{6})-S\/.-/i, "$1-S??? ");
    myReplace(/([NW]\d{6})-S\?\?\? SC /i, "$1-S??? SCREW ");
    myReplace(/--/gi, "-");
    Question: is their a way to apply, to a character, a specific Horizontal Scale (140%), Vertical Scale (120%), & Baseline Shift (-3pt)...

    I need to apply these to the * in the line myReplace(/-INS-\d\d\/\d\d?-/i, "* ");
    Last edited by BrianEdL; 12-04-2012 at 09:40 PM.

  • #6
    New Coder
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    This is to work in Illustrator CS5

    I forgot to mention that this needs to work in Illustrator CS5.

  • #7
    New Coder
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts
    This is what I came up with

    if ( app.documents.length > 0 && app.activeDocument.textFrames.length > 0 ) {
    // Set the value of the word to look for
    searchWord1 = "*";
    //searchWord2 = "The";

    // Iterate through all words in the document
    // the words that match searchWord
    for ( i = 0; i < app.activeDocument.textFrames.length; i++ ) {
    textArt = activeDocument.textFrames[i];
    for ( j = 0; j < textArt.characters.length; j++) {
    word = textArt.characters[j];
    if ( word.contents == searchWord1 ) {

    word.verticalScale = 120;
    word.horizontalScale = 140;
    word.baselineShift = -3;
    }
    }
    }
    }

  • #8
    New Coder
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I have added many parts after learning about regular expressions. Does it look ok? I know there are several beginner mistakes but it does do what I need for the most part.

    I DON'T KNOW WHY BUT WHEN I PUT THIS SCRIPT IN THE FORUM IT REPLACES THE A S S? TO ***? I THINK BECAUSE IT THINKS THAT IT IS A BAD WORD ***?

    Code:
    // Version 3
    
    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(/^PRG.*/i, "");
    myReplace(/.*EBOM.*/i, "");
    myReplace(/^PH.*0000.*/i, "");
    myReplace(/^PH.*00\/.*/i, "");
    //  N or W  &  6 #'s   &   -S_    EX. N123456-S_  REPLACE with:  N123456-S???    (THIS NEEDS TO BE ABOVE  _ REPLACED BY SPACE)
    myReplace(/([NW]\d{6}-S)_/i, "$1??? ");
    
    myReplace(/_/gi, " ");
    //  6 #'s   &  - or no -  &  7 #'s   &  1 to 3 #'s   &  -    EX: 123456-1234567/123-  REPLACE with:  -123456-
    myReplace(/(\d{6})-?\d{7}\/\d\d?\d?-/i, "-$1-");
    myReplace(/(\d{6})-?\d{7}-\/\d\d?\d?-/i, "-$1-");
    
    myReplace(/([NW]\d{6}-S)-INS-\d\d\/\d\d?-/i, "$1??? ");
    
    myReplace(/-INS-\d\d\/\d\d?-/i, "* ");
    
    //  -  That is only followed by one more -  &  -  &   2 to 3 #'s   &   /   &   1 to 3 #'s   &   -      EX:   -7NPSJ_RH-001/9-   REPLACE with  * & Space
    myReplace(/-[^-]*-\d\d\d?\/\d\d?\d?-/i, "* ");
    
    myReplace(/ ?ASSEMBLY/gi, " ASY");
    myReplace(/ ***?Y+$| ***?Y - | ***?Y -| ***?Y | ***?Y- | ***?Y-/gi, " ASY - ");
    
    myReplace(/(MCA-|DS-?C1-?)/i, "-");
    
    myReplace(/^DS-|^DI-|^PH-|MCA|^PAF-|^PAF|^FDR-|^FDR/i, "");
    
    myReplace(/VIEW ([a-z])/i, "TTEMPP $1");
    
    myReplace(/ ?\(?V?I?EW\)| ?\(?VIE[W)]?|^W\)| ?\(VI+$|^ ?\(VI| ?\(V+$|^ ?\(V| ?\(+$|^ ?\)/i, "");
    
    myReplace(/TTEMPP ([a-z])/i, "VIEW $1");
    
    myReplace(/([NW]\d{6}-S)-/i, "$1??? ");
    myReplace(/([NW]\d{6}-S)\/.-/i, "$1??? ");
    
    //   Needs to be in this order
    myReplace(/ AND /i, "&");
    myReplace(/WASHER/i, "WSHR");
    myReplace(/BOLT/i, "BLT");
    myReplace(/STUD/i, "STU");
    myReplace(/([SCREW|SC|NUT|BLT|STU])&WSHR/i, "$1 & WSHR");
    myReplace(/\?\?\? SCREW &/i, "??? SC &");
    myReplace(/\?\?\? SC [^&]/i, "??? SCREW ");
    myReplace(/(\?\?\? SC & WSHR).*/i, "$1");
    myReplace(/(\?\?\? SCREW).*/i, "$1");
    myReplace(/(\?\?\? NUT & WSHR).*/i, "$1");
    myReplace(/\?\?\? NUT [^&].*/i, "??? NUT");
    myReplace(/(\?\?\? BLT & WSHR).*/i, "$1");
    myReplace(/\?\?\? BLT [^&].*/i, "??? BLT");
    myReplace(/(\?\?\? STU & WSHR).*/i, "$1");
    myReplace(/\?\?\? STU [^&].*/i, "??? STU");
    
    myReplace(/--/gi, "-");
    
    
    if ( app.documents.length > 0 && app.activeDocument.textFrames.length > 0 ) {
    // Set the value of the word to look for
    searchWord1 = "*";
    //searchWord2 = "The";
    
    // Iterate through all words in the document
    // the words that match searchWord
    for ( i = 0; i < app.activeDocument.textFrames.length; i++ ) {
    textArt = activeDocument.textFrames[i];
    for ( j = 0; j < textArt.characters.length; j++) {
    word = textArt.characters[j];
    if ( word.contents == searchWord1 ) {
    
    word.verticalScale = 120;
    word.horizontalScale = 140;
    word.baselineShift = -3;
    }
    }
    }
    }

    Any ideas why sometimes when the script is called to run it will launch ExtendScript Toolkit & just stall? I think it maybe when there is a lot of text to go through. Not sure that is the case every time. See Pic.

    Last edited by BrianEdL; 12-19-2012 at 07:04 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •