Skip to main content
May 28, 2011
Answered

Illustrator Find And Replace

  • May 28, 2011
  • 4 replies
  • 40490 views

Hi there,

Can anybody tell me if it's possible to JavaScript Illustrators Find And Replace text function?

I know it's possible in InDesign but I can't find anything for Illustrator.

Many Thanks,

Nik

This topic has been closed for replies.
Correct answer CarlosCanto

Hi Nik, no need to dig down to words, we can search the whole textFrame.contents. Here's a working sample

var active_doc = app.activeDocument;

var search_string = /blah/gi; // g for global search, remove i to make a case sensitive search

var replace_string = "lalala";

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;

           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;

               }

      }

}

4 replies

Known Participant
November 28, 2021

I'm new to scripting and I can't get the simplest possible code to work.  I found this thread and I copied one lines, which was "var docRef = app.activeDocument;  ".  It won't compile in the mac script editor.  See this screenshot:

 

I just can't write the simplest possible code.  Please help. 

femkeblanco
Legend
November 28, 2021

Save the file as a .jsx file.  Then, while your document is open in Illustrator, go to File > Scripts > Other Script.  Find your script and open it.  This will run the script. 

 

Note that var docRef = app.activeDocument is just a reference to the document, so running a script with just this line won't do anything. 

CarlosCanto
Community Expert
Community Expert
November 23, 2019

Hi born2wrestle, here you go

select one or more text frames to process selection, or don't select anything to process all frames in the active document

 

// script.name = highlight_numbers,jsx
// script.description = highlights all numbers in either the selected frames or the whole doucument if nothing is selected.
// script.requirement = Illustrator must be open before running the script
// script.parent = CarlosCanto // 11/23/19
// script.support = canto29 at gmail d0t c0m;

function main () {
    var idoc = app.activeDocument;
    
    if (idoc.selection.length == 0) {
        var tframes = idoc.textFrames;
    }
    else {
        var tframes = [];
        var tframe;
        
        for (var a=0; a<idoc.selection.length; a++) {
            tframe = idoc.selection[0];
            if (tframe.typename == 'TextFrame') {
                tframes.push(tframe);
            }
        }
    }

    var range, framecontent;
    
    var reg = /\d+/g;  
 
    var rangesToSelect = [];
    
    for (var b=0; b<tframes.length; b++) {
        tframe = tframes[b];

        framecontent = tframe.contents; 
        
        var result;
        while (result = reg.exec(framecontent)) {  // this always returns 1 element
            //$.writeln(result[0] + " | " + result.index);  
            //$.writeln(result[0].length);
            
            range = tframe.characters[result.index];
            range.length = result[0].length;
            rangesToSelect.push(range);
        } 
    }
    idoc.selection = null;
    idoc.selection = rangesToSelect;
}

main();

 

 

Participant
June 18, 2024

Hi Carlos

i want to select all "*"

because i want change color and formating to super script

i have more than 100 artboards

is this possible

 

 

 

CarlosCanto
Community Expert
Community Expert
June 20, 2024

Hi raviraj, what do you mean? do you want a script that only selects all text items and stops there?

Participating Frequently
November 19, 2019

Hi Carlos,

 

Is it possible to have a similar script that would find numeric values in a text frame?  Example "Smith12" to find and select only "12".  I don't want to replace it with anything just a selection of the number.  Any help would be great. Thank you

CarlosCanto
Community Expert
Community Expert
May 29, 2011

there's no such feature but it is doable, you have to loop thru all TextFrames and work each of their contents.

May 29, 2011

Hi Carlos,

I'm afraid I'm a little stumped and wander if you or anybody else can help?

I have written the code to search for a matching string:

var active_doc = app.activeDocument

var search_string = "blah"

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;

          var this_text_frame_word_count = this_text_frame.words.length;

          //alert(this_text_frame_word_count);

          for (var j = 0 ; j < this_text_frame_word_count; j++)

          {

                        var this_word = this_text_frame.words.contents;

                        //alert(this_word);

                        if ( this_word == search_string)

                        {

                            alert(this_word+" matches "+search_string);

                            }

                        }

          }

      }

But I'm afraid I'm stuck with how I now replace the string. Could somebody please point me in the right direction.

Thanks again,

Nik

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
May 30, 2011

Hi Nik, no need to dig down to words, we can search the whole textFrame.contents. Here's a working sample

var active_doc = app.activeDocument;

var search_string = /blah/gi; // g for global search, remove i to make a case sensitive search

var replace_string = "lalala";

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;

           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;

               }

      }

}