...

View Full Version : Combine Find & Replace Code for Illustrator



BrianEdL
11-12-2012, 06:39 PM
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!


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;
}
}
}

devnull69
11-12-2012, 08:28 PM
Functions are the key to avoid code repetition


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 ");
...

BrianEdL
11-12-2012, 08:57 PM
THANK YOU! Works like magic.

BrianEdL
11-13-2012, 01:41 PM
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
12-04-2012, 09:23 PM
I've been working hard on learning Regular Expressions & here is what I have so far.

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, "* ");

BrianEdL
12-05-2012, 02:21 PM
I forgot to mention that this needs to work in Illustrator CS5.

BrianEdL
12-19-2012, 06:12 PM
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
12-19-2012, 06:25 PM
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 ***?


// 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.

http://i.imgur.com/3YNE2.jpg



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum