Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-12-2012, 06:39 PM   PM User | #1
BrianEdL
New Coder

 
Join Date: Oct 2012
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
BrianEdL is an unknown quantity at this point
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;
               }
      }
}
BrianEdL is offline   Reply With Quote
Old 11-12-2012, 08:28 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 530 Times in 524 Posts
devnull69 will become famous soon enough
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 ");
...
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
BrianEdL (11-12-2012)
Old 11-12-2012, 08:57 PM   PM User | #3
BrianEdL
New Coder

 
Join Date: Oct 2012
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
BrianEdL is an unknown quantity at this point
THANK YOU! Works like magic.
BrianEdL is offline   Reply With Quote
Old 11-13-2012, 01:41 PM   PM User | #4
BrianEdL
New Coder

 
Join Date: Oct 2012
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
BrianEdL is an unknown quantity at this point
Quick question, when this script runs does it run in the order from top to bottom the top myReplace through to the bottom myReplace?
BrianEdL is offline   Reply With Quote
Old 12-04-2012, 09:23 PM   PM User | #5
BrianEdL
New Coder

 
Join Date: Oct 2012
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
BrianEdL is an unknown quantity at this point
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..
BrianEdL is offline   Reply With Quote
Old 12-05-2012, 02:21 PM   PM User | #6
BrianEdL
New Coder

 
Join Date: Oct 2012
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
BrianEdL is an unknown quantity at this point
This is to work in Illustrator CS5

I forgot to mention that this needs to work in Illustrator CS5.
BrianEdL is offline   Reply With Quote
Old 12-19-2012, 06:12 PM   PM User | #7
BrianEdL
New Coder

 
Join Date: Oct 2012
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
BrianEdL is an unknown quantity at this point
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;
}
}
}
}
BrianEdL is offline   Reply With Quote
Old 12-19-2012, 06:25 PM   PM User | #8
BrianEdL
New Coder

 
Join Date: Oct 2012
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
BrianEdL is an unknown quantity at this point
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..
BrianEdL is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:45 PM.


Advertisement
Log in to turn off these ads.