Skip to main content
May 28, 2011
Answered

Illustrator Find And Replace

  • May 28, 2011
  • 4 replies
  • 40495 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,

Thanks again for your swift response even thought it's not the answer I hoped for!!

As you've suggested as a process I suppose I would need to:

Create an array of every text frame of the active doc.

Repeat with every text frame and create an array of every word in that text frame.

Repeat with every word and put and If statement in to catch a search string match.

Replace the matching word with replace string.

Quite straight forward for me in Applescript but another adventure in JavaScript

Thanks,

Nik