How can I run an Illustrator javascript on all files in a directory?

久未见 提交于 2019-12-08 07:37:24

问题


I'm trying to use javascript to update an Illustrator file. I have a script that will modify an open file in the desired way (really just a text find/replace, but the text is encoded in the Illustrator data). The problem is I have hundreds of these files that I want modified in the same way in a directory. Is there a way for me to do this without having to open every single one of these files?

I figure either: 1. there's a way to modify the existing javascript to read from the directory and load the files in the background, process them, save, and close. 2. I can write a Node.js script that can wrap the existing Illustrator javascript, but I'm not sure how to get it to recognize the "application" object and read the file the same way as when it's opened in Illustrator.

Thanks for any help!

Edit: Here's the functioning script (that only .

function FindAndReplaceScript_AllOpenDocuments(){      
        for(var i=app.documents.length -1; i > -1;  i--){      
        app.documents[i].activate();  
        var aDoc = app.documents[i];  

        var searchString = /OLDTEXT/gi;     
        var replaceString = 'NEWTEXT';       

        var theTF = aDoc.textFrames;      
        if (theTF.length > 0) {      
            for (var j = 0 ; j <theTF.length; j++) {      
                var aTF = theTF[j];      
                var newString = aTF.contents.replace(searchString, replaceString);      
                if (newString != aTF.contents) {      
                    theTF[j].contents = newString;      
                }      
            }      
        }  
    }  
};      
FindAndReplaceScript_AllOpenDocuments();   
}

回答1:


Yes,there is a way to read from directory:

var dir = Folder.selectDialog("Where?");
var files = dir.getFiles("*.eps");

for(var f = 0; f < files.length; f++){
    var doc = app.open(files[f]);
    //Your processing
    doc.close(SaveOptions.SAVECHANGES);
}


来源:https://stackoverflow.com/questions/34920139/how-can-i-run-an-illustrator-javascript-on-all-files-in-a-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!