Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
1

find and replace text with color

Community Beginner ,
Oct 14, 2023 Oct 14, 2023

Copy link to clipboard

Copied

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

}
}
}

 

 

TOPICS
Bug , Feature request , How-to , Performance , Scripting , Type

Views

243
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Guide ,
Oct 14, 2023 Oct 14, 2023

Copy link to clipboard

Copied

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

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 14, 2023 Oct 14, 2023

Copy link to clipboard

Copied

LATEST

Thank you for the timely hep!!!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines