Skip to main content
Participating Frequently
August 11, 2021
Question

Formatted text find and replace.

  • August 11, 2021
  • 4 replies
  • 1843 views

I am looking for a script that will enable me to find and replace text. I print sticky lables for folders and books and i want to be able to change the name on the all the lables at once with a script that will keep the font and all the styles that are in the target file.

Any help?

This topic has been closed for replies.

4 replies

pixxxelschubser
Community Expert
Community Expert
August 15, 2021

As @femkeblanco said: you need the "Palette" dialog type and it will be best to use Bridgetalk to include the script(s).

pixxxelschubser
Community Expert
Community Expert
August 14, 2021

Create an action that starts your script. Give the action a shortcut.

pixxxelschubser
Community Expert
Community Expert
August 12, 2021

Something like this?

RegExp search and replace, keep original text formatting 

 

I didn't tested your code yet.

 

But why you didn't play with my code snippet and use something like this:

// regex_changeContentsOfWordOrString_RemainFormatting.jsx
// regards pixxxelschubser

var s = /arguments/gi;
var replacer = "other string", result;
var atf = activeDocument.textFrames[0];

while (result = s.exec(atf.contents)) {
    try {
        aCon = atf.characters[result.index];
        aCon.length = result[0].length;
        aCon.contents = replacer;
        } catch (e) {};
    }

 

Try it and have fun

 

By @pixxxelschubser

 

femkeblanco
Legend
August 11, 2021
var w = new Window("dialog");
    w.text = "Replace";
var input = w.add("edittext");
    input.characters = 20;
    input.active = true;
var statictext1 = w.add("statictext");
    statictext1.text = "with";
var output = w.add("edittext");
    output.characters = 20;
var button1 = w.add("button", undefined, "OK");
    button1.onClick = function () {
        var frames = app.activeDocument.textFrames;
        var input1 = new RegExp(input.text, "g");
        for (var i = 0; i < frames.length; i++) {
            var substitute = frames[i].contents.replace(input1, output.text);
            frames[i].contents = substitute;
        }
        w.close();
    };
w.show();
YannoAuthor
Participating Frequently
August 11, 2021

Hi, Few questions

1. Does it work on all versions of Illustrator? 

2.  It only replace 1 line. Is there a script that will replace 2 lines? first name on 1 line and last name on the second.

3.  I need a script that will replace on selected text only and not the all file.

femkeblanco
Legend
August 11, 2021

1.  I see no reason why it shouldn't. 

2.  Can you show what you mean?  Are we talking about two words separated by a carriage return or a new line, two words anywhere on two lines, two text frames, et cetera?  Could you not just run the script twice, once for each word? 

3.  Replace these two lines

            var substitute = frames[i].contents.replace(input1, output.text);
            frames[i].contents = substitute;

with this

            if (frames[i].selected == true) {
                var substitute = frames[i].contents.replace(input1, output.text);
                frames[i].contents = substitute;
            }