Skip to main content
Participant
December 5, 2022
Answered

Folders and Subfolders

  • December 5, 2022
  • 1 reply
  • 541 views

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);
        }

 

 

This topic has been closed for replies.
Correct answer Mike Bro

Hello @Alexandre - Alenac2001.

 

Give this a shot...

 

#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];

	for (var i=0; i < aiFiles.length; i++ ){
            var aiFile = File(aiFiles[i]);
            filename = aiFile.name.split('.')[0];	

			app.open(File(aiFiles[i]));

       while (app.documents.length > 0) { 
            doc = app.activeDocument;
            outputPath = doc.path;
		
            //generating_formats
			//PNG
			var opt = getPng24Options(1);
              savePng24(opt);
			
			//JPG
			//(...)
			
            doc.close(SaveOptions.DONOTSAVECHANGES);
       } 
    }
    alert("Done!")
}

  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, scanMask){
    var subFolders = new Array();
    var allFiles = new Array();
    subFolders[0] = tFolder;
    for (var j = 0; j < subFolders.length; j++){    
        var procFiles = subFolders[j].getFiles();
        for (var i = 0; i< procFiles.length; i++){
            if (procFiles[i] instanceof File ){
                if(scanMask == undefined) allFiles.push(procFiles[i]);
                if (procFiles[i].fullName.search(scanMask) != -1) allFiles.push(procFiles[i]);
        }else if (procFiles[i] instanceof Folder){
            subFolders.push(procFiles[i]);
            scanSubFolders(procFiles[i], scanMask);
         }
      }
   }
   return [allFiles,subFolders];
};

try{
    main();
    }catch(e){
        alert("Error. Check the next message\n" + e.message);
        }

 

Regards,

Mike

1 reply

Mike BroCorrect answer
Legend
December 5, 2022

Hello @Alexandre - Alenac2001.

 

Give this a shot...

 

#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];

	for (var i=0; i < aiFiles.length; i++ ){
            var aiFile = File(aiFiles[i]);
            filename = aiFile.name.split('.')[0];	

			app.open(File(aiFiles[i]));

       while (app.documents.length > 0) { 
            doc = app.activeDocument;
            outputPath = doc.path;
		
            //generating_formats
			//PNG
			var opt = getPng24Options(1);
              savePng24(opt);
			
			//JPG
			//(...)
			
            doc.close(SaveOptions.DONOTSAVECHANGES);
       } 
    }
    alert("Done!")
}

  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, scanMask){
    var subFolders = new Array();
    var allFiles = new Array();
    subFolders[0] = tFolder;
    for (var j = 0; j < subFolders.length; j++){    
        var procFiles = subFolders[j].getFiles();
        for (var i = 0; i< procFiles.length; i++){
            if (procFiles[i] instanceof File ){
                if(scanMask == undefined) allFiles.push(procFiles[i]);
                if (procFiles[i].fullName.search(scanMask) != -1) allFiles.push(procFiles[i]);
        }else if (procFiles[i] instanceof Folder){
            subFolders.push(procFiles[i]);
            scanSubFolders(procFiles[i], scanMask);
         }
      }
   }
   return [allFiles,subFolders];
};

try{
    main();
    }catch(e){
        alert("Error. Check the next message\n" + e.message);
        }

 

Regards,

Mike

Participant
December 6, 2022

Mike: Your change worked perfectly. Thanks a lot for the help!