Skip to main content
Known Participant
October 14, 2023
Question

find and replace text with color

  • October 14, 2023
  • 2 replies
  • 354 views

HI, i'm writing an illustrator script to find replace some numbers to other number.

for example 511 to be changed to 410 like this.

for example 512 to be changed to 412 like this.

for example 513 to be changed to 413 like this.

......

for example 413 to be changed to 313 like this.

previous changed 413 should not affect...

here is my code.... plese help me to sortout....

replaceWord(/51/gim,"511")
replaceWord(/61/gim,"611")
replaceWord(/51/gim,"///")

var doc = app.activeDocument;
//replaceWord("INCORRECT", "CORRECT WORD");

var col = new RGBColor();
col.red = 255;
col.green = 0;
col.blue = 0;

var swatch = doc.swatches.add();
swatch.color = col;
swatch.name = "col";

function replaceWord(strFind, strReplace) {


var frame, words, word;
for (var i = 0; i < doc.textFrames.length; i++) {
frame = doc.textFrames[i].textRange;
words = frame.words;
for (var j = 0; j < words.length; j++) {
word = words[j];
//alert(word.contents);
alert(word.characterAttributes.fillColor.getByName);

if (word.fillColor != doc.swatches.getByName('col'))
{
new_string = word.contents.replace(strFind, strReplace)
if (new_string != word.contents) {
word.contents = new_string;
word.fillColor = swatch.color;
}
}

}
}
}

 

 

This topic has been closed for replies.

2 replies

femkeblanco
Legend
October 14, 2023
  • Variables used in a function should be declared either before the function call or within the function.
  • To compare colors, the components of the colors should be compared.
replaceWord(/511/gim, "410");

function replaceWord(strFind, strReplace) {
    var doc = app.activeDocument;
    var col = new RGBColor();
    col.red = 255;
    col.green = 0;
    col.blue = 0;
    var frame, words, word;
    for (var i = 0; i < doc.textFrames.length; i++) {
        frame = doc.textFrames[i].textRange;
        words = frame.words;
        for (var j = 0; j < words.length; j++) {
            word = words[j];
            if (word.fillColor.red != col.red || 
                word.fillColor.green != col.green || 
                word.fillColor.blue != col.blue) {
                new_string = word.contents.replace(strFind, strReplace)
                if (new_string != word.contents) {
                    word.contents = new_string;
                    word.fillColor = col;
                }
            }
        }
    }
}

 

M KarthikAuthor
Known Participant
October 15, 2023

Thank you for the timely hep!!!