Folders and Subfolders
Good evening everyone:
I'm having problems with my script in which I can't find the error. Any help is welcome.
The script reads a home directory. In this initial directory there are several folders where each folder can have more than one AI file to then convert to other formats (eg PNG).
The script works fine when there is only one AI file in each folder. When there is more than one AI file in the same folder, the script is lost...
#target illustrator
function main(){
inputPath = Folder.selectDialog ("Informe a pasta inicial que contem os arquivos AI...");
fileandfolderAr = scanSubFolders(inputPath , /\.(ai)$/i);
var aiFiles = fileandfolderAr[0];
var aiFolders = fileandfolderAr[1];
for (var i=0; i < aiFiles.length; i++ ){
var aiFile = File(aiFiles[i]);
filename = aiFile.name.split('.')[0];
outputPath = Folder(aiFolders[i+1]);
app.open(File(aiFiles[i]));
$.sleep(50);
doc = app.activeDocument;
//generating_formats
//PNG
var opt = getPng24Options(1);
savePng24(opt);
//JPG
//(...)
doc.close(SaveOptions.DONOTSAVECHANGES);
}
}
savePng24 = function ( options ) {
var destFile = new File( outputPath.fsName + '/' +filename + '.png' );
doc.exportFile(destFile, ExportType.PNG24 , options);
}
getPng24Options = function ( scaling ) {
var options = new ExportOptionsPNG24();
options.antiAliasing = true;
options.transparency = true;
options.artBoardClipping = true;
options.horizontalScale = scaling*100;
options.verticalScale = scaling*100;
return options;
}
function scanSubFolders (tFolder, myAI) {
var sFolders = [];
var allFiles = [];
sFolders[0] = tFolder;
for (var j = 0; j < sFolders.length; j++) {
var procFiles = sFolders[j].getFiles();
for (var i = 0; i < procFiles.length; i++) {
if (procFiles[i] instanceof File) {
if(myAI == undefined) {
allFiles.push(procFiles);
}
if (procFiles[i].fullName.search(myAI) != -1) {
allFiles.push(procFiles[i]);
}
}
else if (procFiles[i] instanceof Folder) {
sFolders.push(procFiles[i]);
scanSubFolders(procFiles[i], myAI);
}
}
}
return [allFiles, sFolders];
}
try{
main();
}catch(e){
alert("Error. Check the next message\n" + e.message);
}
