Extract the text out of Illustrator file any API or script?

I’m working on a project that has 1000 files with the same typo ex. the file with the word “dogs” that has to be turned into “dog”. these 1000 files has the same typo. Is there anyway that I can write a script to do that in Illustrator? or some API that I can extract out text from the file then edit it (change from “dogs” into “dog”) then save it back because I don’t want to open 1000 files and do it 1000 times.

Answer

This script will find keyword “Dogs” and replace it with “Dog” within all open documents

    #target illustrator
    var count;
var folderRef = Folder.selectDialog ("choose Folder");
var prefix = prompt("Prefix name","");
var fileRef = folderRef + "/" + prefix;
for (count=0;count<9999;count++){
    var temp;
    if(count<10){
        temp = "000";
        }else if(count>=10 && count<100){
        temp = "00";
        }else if(count>=100 && count<1000){
        temp = "0";
        }else{
            temp = "";
            }
    var result = fileRef + " " + temp + count;
    var f = new File(result + ".ai");
    if ( f.exists ) {
        app.open( new File(f) );
for(var i = 0; i < app.documents.length; i++){    
    for(var j = 0; j < app.documents[i].textFrames.length; j++){    
        var str = app.documents[i].textFrames[j].contents;    
        var rep = str.replace ("Dogs", "Dog");
        app.documents[i].textFrames[j].contents = rep;
//justification lines
       var z = app.documents[i].textFrames[j].paragraphs[0].paragraphAttributes;
       z.justification = Justification.CENTER; 
          } 
    }
app.activeDocument.close(SaveOptions.SAVECHANGES);   //comment this lineby putting "//" before it if you wanna keep your file open after edited
    }

}

so if you want to replace word from all open document you can use this; save this code as ReplaceTest(orWhatever).jsx then put it in your script folder,and restart your Illustrator . you will find your script at File-Scripts.

enter image description here

Comment your requirement below for further improvements in code 🙂 you can see how i achieved it; don’t forget to comment close command if you wanna keep your file open

enter image description here

Attribution
Source : Link , Question Author : user136301 , Answer Author : Mr.Online

Leave a Comment