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

Illustrator Find And Replace

Guest
May 28, 2011 May 28, 2011

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

37.4K

Translate

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

correct answers 1 Correct answer

Community Expert , May 29, 2011 May 29, 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

...

Votes

Translate

Translate
Adobe
New Here ,
Apr 10, 2015 Apr 10, 2015

Copy link to clipboard

Copied

Hi Carlos! I'm trying to use your code with a folder full of documents, but Illustrator keeps crashing on me. Can you please help?

// Illustrator Find And Replace

// Original code from:    https://forums.adobe.com/thread/858021

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

var replace_string = "lalala"; 

var sourceFolder, files, fileType, sourceDoc;

// Select the source folder.

sourceFolder = Folder.selectDialog( 'Select folder with Illustrator files', '~' );

// If a valid folder is selected

if ( sourceFolder != null )

{

    files = new Array();

    fileType = '*.ai';

   

    // Get all files matching the pattern

    files = sourceFolder.getFiles( fileType );

   

    if ( files.length > 0 )

    {     

        for ( i = 0; i < files.length; i++ )

        {

            sourceDoc = app.open(files); // returns the document object

           

           

            //REPLACE CODE BEGIN          

            var text_frames = sourceDoc.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; 

                    } 

                } 

            }                                    

            //REPALCE CODE END

            sourceDoc.save();

            // Close current without saving

            sourceDoc.close(SaveOptions.DONOTSAVECHANGES);         

         }

     }

    else

    {

        alert( 'No matching files found' );

    }

}

Votes

Translate

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 ,
Nov 19, 2019 Nov 19, 2019

Copy link to clipboard

Copied

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

Votes

Translate

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 Expert ,
Nov 23, 2019 Nov 23, 2019

Copy link to clipboard

Copied

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

selectNumbers.PNG

 

// 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();

 

 

Votes

Translate

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
New Here ,
Nov 28, 2021 Nov 28, 2021

Copy link to clipboard

Copied

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:

 

Screen Shot 2021-11-28 at 12.34.34 PM.png

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

Votes

Translate

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
Guide ,
Nov 28, 2021 Nov 28, 2021

Copy link to clipboard

Copied

LATEST

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. 

Votes

Translate

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