16 Replies Latest reply on Aug 10, 2015 7:55 PM by Qwertyfly...

    improve or simplify this batch script

    julioc4816867

      sorry my bad english

      Hello, I've combined some scripts in this forum, to make an image processor as Photoshop, I wonder if this script is right or if you can simplify more

       

      This script convert AI to PSD, Pdf, Png, Jpg, AI v3 - cc2015, select resolution and included to subfolders (optional)

       

      Español

       

      hola, he combinado algunos scripts en este foro, para crear algo similar al procesador de photoshop, me gustaría saber si este script se puede mejorar o simplificar para que procese más rápido.

       

      este script procesa un Ai a Psd, Pdf, Png, Jpg y Ai v3 hasta la CC2015, además puede escoger la resolución de la exportación para PSD,PNG y JPG, también uno puede elegir que busque en subcarpetas

       

       

       

      var format = prompt('Choose a File Type Extension  :\nai, psd, pdf, jpg, png', "png", 'Batch');
      if (format == "ai" || format == "psd" || format == "pdf" || format == "jpg" || format == "png") {
        if (format == "jpg" || format == "png" || format == "psd") {
        var res = prompt('DPI resolution ', 300, 'Batch');
        }
        if (format == "ai") {
        alert("Adobe Ai versions :\n\nAdobe 3.0 = 3\n\nAdobe 8.0 = 8\n\nAdobe 9.0 = 9\n\nAdobe 10 = 10\n\nAdobe CS1 = 11\n\nAdobe CS2 = 12\n\nAdobe CS3 = 13\n\nAdobe CS4 = 14\n\nAdobe CS5 = 15\n\nAdobe CS6 = 16\n\nAdobe CC 2013,2014,2015 = 17")
        var cs = prompt('Type Ai version ', 17, 'Batch');
        }
      
        var folder = Folder.selectDialog("Select Source Folder...");
        var files;
        var resolution = res;
        var ver = cs;
      
      
        //exporting
        //var lFolder = new Folder(folder + "/" + format);
        //lFolder.create();
      
      
        if (folder != null) {
      
      
        var subcarpets = prompt('Include subfolders when searching? yes o no?', "no", 'Batch');
      
      
        if (subcarpets == "no" || subcarpets == "yes") {
        if (subcarpets == "no") {
        files = folder.getFiles("*.ai");
        }
        if (subcarpets == "yes") {
        files = find_files(folder, ['.ai']);
        }
      
      
        var fileCount = files.length; // count them
        var log = fileCount + ' Files Processed  to ' + format + '\n';
      
      
        if (fileCount > 0) {
        for (i = 0; i < fileCount; i++) {
        var idoc = app.open(files[i]);
        var tipe = files[i].name.substr(0, files[i].name.lastIndexOf('.')) || files[i].name;
      
      
        if (format == "ai") {
        exportAI();
        }
        if (format == "psd") {
        exportPSD();
        }
        if (format == "pdf") {
        exportPDF();
        }
        if (format == "jpg") {
        exportJPG();
        }
        if (format == "png") {
        exportPNG();
        }
        log += files[i] + " to " + tipe + "." + format + '\n';
      
      
        }
        alert(fileCount + ' file(s) processed to ' + format);
      
      
        txtlog = new File(folder + '/textlog.txt');
        var isopen = txtlog.open("w"); //open file for editing
        if (isopen) //test file is open
        {
        txtlog.seek(0, 0);
        txtlog.write(log);
        txtlog.close();
        }
      
      
        } else {
        alert("There are no Illustrator files in this folder.");
        }
        } else {
        alert("sorry, the option \"" + subcarpets + "\" no exist.");
        }
        }
      }
      
      
      /////////////////////////////////////subfolders - Peter Kharel/////////////////////////////////////////////////////////////////
      // recurse subfolders - Peter Kharel
      function find_files(dir, mask_array) {
        var arr = [];
        for (var i = 0; i < mask_array.length; i++) {
        arr = arr.concat(find_files_sub(dir, [], mask_array[i].toUpperCase()));
        }
        return arr;
      }
      
      
      function find_files_sub(dir, array, mask) {
        var f = Folder(dir).getFiles('*.*');
        for (var i = 0; i < f.length; i++) {
        if (f[i]instanceof Folder) {
        find_files_sub(f[i], array, mask);
        } else if (f[i].name.substr(-mask.length).toUpperCase() == mask) {
        array.push(f[i]);
        }
        }
        return array;
      }
      //////////////////////////////////////TIPOS DE ARCHIVOS/////////////////////////////////////////////////////////////////
      
      
      function exportPNG() {
      
      
        var pngExportOpts = new ExportOptionsPNG24();
        pngExportOpts.antiAliasing = true;
        pngExportOpts.artBoardClipping = true;
        pngExportOpts.horizontalScale = (resolution / 72) * 100;
        //pngExportOpts.matte = true;
        //pngExportOpts.matteColor = 0, 0, 0;
        pngExportOpts.saveAsHTML = false;
        pngExportOpts.transparency = true;
        pngExportOpts.verticalScale = (resolution / 72) * 100;
        idoc.exportFile(files[i], ExportType.PNG24, pngExportOpts);
        idoc.close(SaveOptions.DONOTSAVECHANGES);
        return pngExportOpts;
      }
      
      
      function exportJPG() {
      
      
        var jpgExportOpts = new ExportOptionsJPEG();
        jpgExportOpts.antiAliasing = true;
        jpgExportOpts.qualitySetting = 100;
        jpgExportOpts.horizontalScale = (resolution / 72) * 100;
        jpgExportOpts.verticalScale = (resolution / 72) * 100;
        jpgExportOpts.optimization = true;
        jpgExportOpts.artBoardClipping = true;
        idoc.exportFile(files[i], ExportType.JPEG, jpgExportOpts);
        idoc.close(SaveOptions.DONOTSAVECHANGES);
        return jpgExportOpts;
      }
      
      
      function exportAI() {
      
      
        var dest = new File(files[i] + "_v"+ver+".ai");
        var illustratorSaveOpts = new IllustratorSaveOptions();
        if (ver == 17) {
        illustratorSaveOpts.compatibility = Compatibility.ILLUSTRATOR17;
        }
        if (ver == 16) {
        illustratorSaveOpts.compatibility = Compatibility.ILLUSTRATOR16;
        }
        if (ver == 15) {
        illustratorSaveOpts.compatibility = Compatibility.ILLUSTRATOR15;
        }
        if (ver == 14) {
        illustratorSaveOpts.compatibility = Compatibility.ILLUSTRATOR14;
        }
        if (ver == 13) {
        illustratorSaveOpts.compatibility = Compatibility.ILLUSTRATOR13;
        }
        if (ver == 12) {
        illustratorSaveOpts.compatibility = Compatibility.ILLUSTRATOR12;
        }
        if (ver == 11) {
        illustratorSaveOpts.compatibility = Compatibility.ILLUSTRATOR11;
        }
        if (ver == 10) {
        illustratorSaveOpts.compatibility = Compatibility.ILLUSTRATOR10;
        }
        if (ver == 9) {
        illustratorSaveOpts.compatibility = Compatibility.ILLUSTRATOR9;
        }
        if (ver == 8) {
        illustratorSaveOpts.compatibility = Compatibility.ILLUSTRATOR8;
        }
        if (ver == 3) {
        illustratorSaveOpts.compatibility = Compatibility.ILLUSTRATOR3;
        }
        illustratorSaveOpts.embedLinkedFiles = true;
        illustratorSaveOpts.fontSubsetThreshold = 0.0
        illustratorSaveOpts.pdfCompatible = true
        illustratorSaveOpts.embedICCProfile = false
        idoc.saveAs(dest, illustratorSaveOpts);
        idoc.close(SaveOptions.DONOTSAVECHANGES);
        return illustratorSaveOpts;
      
      
      }
      
      
      function exportPDF() {
      
      
        var pdfSaveOpts = new PDFSaveOptions();
        pdfSaveOpts.acrobatLayers = false;
        pdfSaveOpts.colorBars = false;
        pdfSaveOpts.colorCompression = CompressionQuality.AUTOMATICJPEGHIGH;
        pdfSaveOpts.compressArt = true; //default
        pdfSaveOpts.embedICCProfile = true;
        pdfSaveOpts.enablePlainText = true;
        pdfSaveOpts.generateThumbnails = true; // default
        pdfSaveOpts.optimization = true;
        pdfSaveOpts.pageInformation = false;
        pdfSaveOpts.preserveEditability = false;
        idoc.saveAs(files[i], pdfSaveOpts);
        idoc.close(SaveOptions.DONOTSAVECHANGES);
        return pdfSaveOpts;
      
      
      }
      
      
      function exportPSD() {
      
      
        var psdExportOpts = new ExportOptionsPhotoshop();
        idoc.documentColorSpace == DocumentColorSpace.CMYK ? psdExportOpts.imageColorSpace = ImageColorSpace.CMYK : psdExportOpts.imageColorSpace = ImageColorSpace.RGB;
        psdExportOpts.antiAliasing = true;
        psdExportOpts.embedICCProfile = true;
        psdExportOpts.writeLayers = true;
        psdExportOpts.resolution = resolution;
        psdExportOpts.maximumEditability = true;
        idoc.exportFile(files[i], ExportType.PHOTOSHOP, psdExportOpts);
        idoc.close(SaveOptions.DONOTSAVECHANGES);
        return psdExportOpts
      
      
      }
      
      
      
      
      
        • 1. Re: improve or simplify this batch script
          W_J_T Level 4

          Hi,

           

          - Presently it sounds like its working?

          - If so, does it run inefficiently or something?

          - If not then what or why does it need improved or simplified?

          • 2. Re: improve or simplify this batch script
            julioc4816867 Level 1

            sorry my bad english

            hi, thanks for answering, I combined several scripts found in this forum actually works, but I wonder if the script is well written or organized, can simplify this script to work faster?

            • 3. Re: improve or simplify this batch script
              W_J_T Level 4

              Your English is good. So does it work slow now?

              • 4. Re: improve or simplify this batch script
                julioc4816867 Level 1

                I wanted to know if the script is well written , I have many "if", it affects the speed of the script?

                • 5. Re: improve or simplify this batch script
                  W_J_T Level 4

                  - Just at a glance, some of the conditional statements appear redundant, but maybe I am just misreading/observing things. Maybe clean those up better.

                  - Wrap the whole thing in a function.

                  - Also remove any unneeded alerts that were for debugging only.

                  - You could look into scripting a simple UI (using ScriptUI) to have a single window instead of multiple prompts as well.


                  EDIT: I was typing when you posted.

                  • 6. Re: improve or simplify this batch script
                    Qwertyfly... Level 4

                    The many prompts slow things down.

                    if statements are fairly quick.

                    and in this sort of thing the time is in the processing of the files, the if's make little difference.

                    W_J_T made a good point in wrapping it in a function. Keep the Global space Clean!!!

                    use of Case over Else If can help.

                    also the if if if if ect. in the exportAI() function is not needed, see below.

                     

                    I have done a rewrite for you.

                    I removed many if's, but think I added way more in.

                    added a GUI.

                    and some validation.

                     

                    your script is 235 lines and has much blank space.

                    I managed to slim it down to 364 lines !?

                    so in summary:

                    I may have improved and simplified the user experience.

                    but the script may be a little more complex and harder to follow.

                    let me know if you want anything explained.

                     

                    //----------------------------------------------------------------------------
                    //
                    //          Illustrator Image Processer
                    //
                    //----------------------------------------------------------------------------
                    //
                    //          Original Script by:
                    //              Julioc4816867
                    //
                    //          Rebuilt by:
                    //              Qwertyfly
                    //              tristan@qwertyfly.com
                    //
                    //          Version 1.0
                    //              27/7/15
                    //
                    //----------------------------------------------------------------------------
                    //          Version History
                    //
                    //          Version 1.0
                    //          - The main addition to this script is the GUI
                    //          - Also alot of validation and a bit of code cleanup
                    //          - Please let me know of any bugs found
                    //          - Please let me know of any feature requests
                    //          
                    //----------------------------------------------------------------------------
                    function main(){
                        // ---------------------------------recurse subfolders - Thanks to Peter Kharel  ----------------------------------------------
                        function find_files(dir, mask_array) {  
                            var arr = [];  
                            for (var i = 0; i < mask_array.length; i++) {  
                                arr = arr.concat(find_files_sub(dir, [], mask_array[i].toUpperCase()));  
                            }  
                            return arr;  
                        }  
                    
                    
                        function find_files_sub(dir, array, mask) {  
                            var f = Folder(dir).getFiles('*.*');  
                            for (var i = 0; i < f.length; i++) {  
                                if (f[i]instanceof Folder) {  
                                    find_files_sub(f[i], array, mask);  
                                }else if(f[i].name.substr(-mask.length).toUpperCase() == mask) {  
                                    array.push(f[i]);  
                                }  
                            }  
                            return array;  
                        } 
                        // ------------------------------------------------------------------------------------------------------------------------------------------
                        function exportAI(doc, newFile, version) {  
                            var illustratorSaveOpts = new IllustratorSaveOptions();  
                            illustratorSaveOpts.compatibility = Compatibility["ILLUSTRATOR" + version];
                            illustratorSaveOpts.compressed = true; // Version 10+
                            illustratorSaveOpts.pdfCompatible = true;  // Version 10+
                            illustratorSaveOpts.embedICCProfile = false;  // Version 9+
                            illustratorSaveOpts.embedLinkedFiles = true;  // Version 7+ 
                            illustratorSaveOpts.fontSubsetThreshold = 0.0;  
                            doc.saveAs(newFile, illustratorSaveOpts);  
                            doc.close(SaveOptions.DONOTSAVECHANGES);  
                        } 
                    
                    
                        function exportPDF(doc, newFile) {  
                            var pdfSaveOpts = new PDFSaveOptions();  
                            pdfSaveOpts.acrobatLayers = false;  
                            pdfSaveOpts.colorBars = false;  
                            pdfSaveOpts.colorCompression = CompressionQuality.AUTOMATICJPEGHIGH;  
                            pdfSaveOpts.compressArt = true; //default  
                            pdfSaveOpts.embedICCProfile = true;  
                            pdfSaveOpts.enablePlainText = true;  
                            pdfSaveOpts.generateThumbnails = true; // default  
                            pdfSaveOpts.optimization = true;  
                            pdfSaveOpts.pageInformation = false;  
                            pdfSaveOpts.preserveEditability = false;  
                            doc.saveAs(newFile, pdfSaveOpts);  
                            doc.close(SaveOptions.DONOTSAVECHANGES);  
                        }  
                    
                    
                        function exportPSD(doc, newFile, resolution) {
                            var psdExportOpts = new ExportOptionsPhotoshop();  
                            doc.documentColorSpace == DocumentColorSpace.CMYK ? psdExportOpts.imageColorSpace = ImageColorSpace.CMYK : psdExportOpts.imageColorSpace = ImageColorSpace.RGB;  
                            psdExportOpts.antiAliasing = true;
                            psdExportOpts.embedICCProfile = true;  
                            psdExportOpts.writeLayers = true;  
                            psdExportOpts.resolution = resolution;  
                            psdExportOpts.maximumEditability = true;
                            doc.exportFile(newFile, ExportType.PHOTOSHOP, psdExportOpts);
                            doc.close(SaveOptions.DONOTSAVECHANGES);
                        }
                    
                    
                        function exportJPG(doc, newFile, resolution) {  
                            var jpgExportOpts = new ExportOptionsJPEG();  
                            jpgExportOpts.antiAliasing = true;  
                            jpgExportOpts.qualitySetting = 100;  
                            jpgExportOpts.horizontalScale = (resolution / 72) * 100;  
                            jpgExportOpts.verticalScale = (resolution / 72) * 100;  
                            jpgExportOpts.optimization = true;  
                            jpgExportOpts.artBoardClipping = true;  
                            doc.exportFile(newFile, ExportType.JPEG, jpgExportOpts);  
                            doc.close(SaveOptions.DONOTSAVECHANGES);   
                        }  
                    
                    
                        function exportPNG(doc, newFile, resolution) {  
                            var pngExportOpts = new ExportOptionsPNG24();  
                            pngExportOpts.antiAliasing = true;  
                            pngExportOpts.artBoardClipping = true;  
                            pngExportOpts.horizontalScale = (resolution / 72) * 100;  
                            //pngExportOpts.matte = true;  
                            //pngExportOpts.matteColor = 0, 0, 0;  
                            pngExportOpts.saveAsHTML = false;  
                            pngExportOpts.transparency = true;  
                            pngExportOpts.verticalScale = (resolution / 72) * 100;  
                            doc.exportFile(newFile, ExportType.PNG24, pngExportOpts);  
                            doc.close(SaveOptions.DONOTSAVECHANGES);  
                        } 
                        // ------------------------------------------------------------------------------------------------------------------------------------------
                        function DateTime(){
                            var now = new Date();
                            return (now.getDate()<10?'0':'') + now.getDate() + '/'
                                        +(now.getMonth()<9?'0':'')+(now.getMonth()+1)+'/'
                                        +now.getFullYear() + ' @ ' 
                                        + ((now.getHours() % 12)?(now.getHours() % 12):12) + ':' 
                                        + (now.getMinutes()<10?'0':'') + now.getMinutes() + ':' 
                                        + now.getSeconds() + (now.getHours()>=12?' pm':' am');
                        }
                    
                    
                        function createLog(log, destination){
                            txtlog = new File(destination + '/textlog.txt');
                            var isopen = txtlog.open("w"); //open file for editing  
                            if (isopen) { //test file is open    
                                txtlog.seek(0, 0);  
                                txtlog.write(log);  
                                txtlog.close();  
                            }
                        }
                    
                    
                        function window() { 
                            var w = {};    
                            w = new Window("dialog",'Image Processor');
                            w.spacing = 5;
                            w.margins = 10;
                            w.alignChildren = "left";
                            //
                            w.g1 = w.add("group");
                            w.g1.alignment = ["fill","fill"];
                            w.g1.orientation = "row";
                            w.g1.stat = w.g1.add("statictext",undefined,'Select a File Type:');
                            w.g1.dd = w.g1.add("dropdownlist",undefined,['ai', 'pdf', 'psd', 'jpg', 'png']);
                            w.g1.dd.alignment = ["right","top"]; 
                            w.g1.dd.selection = 4;
                            w.g1.dd.onChange = function(){
                                if(w.g1.dd.selection.index > 1){
                                    w.g2.dd.enabled = w.g2.stat.enabled = true;
                                }else{
                                    w.g2.dd.enabled = w.g2.stat.enabled = false;
                                    if(w.g1.dd.selection.index == 0){
                                        w.g3.dd.enabled = w.g3.stat.enabled = true;
                                    }else{
                                        w.g3.dd.enabled = w.g3.stat.enabled = false;
                                    }
                                }
                            }
                            //
                            w.g2 = w.add("group");
                            w.g2.alignment = ["fill","fill"];
                            w.g2.orientation = "row";
                            w.g2.stat = w.g2.add("statictext",undefined,'Select a Resolution:');
                            w.g2.dd = w.g2.add("dropdownlist",undefined,['72 dpi', '150 dpi', '300 dpi', 'custom']);
                            w.g2.dd.alignment = ["right","top"];
                            w.g2.dd.selection = 2;
                            w.g2.dd.enabled = w.g2.stat.enabled = true;
                            w.g2.dd.onChange = function(){
                                if(w.g2.dd.selection == 3){
                                    w.g2.cus = w.g2.add("edittext",[240,0,300,23]);
                                    w.g2.cus.active = true;
                                    w.g2.cus.onDeactivate = function() { //may be good to make sure number is within a certain range
                                        if(w.g2.cus.text == ''){
                                            w.g2.dd.selection = 2; // if blank revert to default of 300 dpi
                                        }else{
                                            w.g2.cus.text = (w.g2.cus.text = w.g2.cus.text.replace(/[^\d.]/g, '')) + ' dpi';
                                        }
                                    }
                                }else{
                                    try{
                                        w.g2.remove(w.g2.cus);
                                        w.layout.layout (true);
                                    }catch (e){}
                                }
                            }
                            //
                            w.g3 = w.add("group");
                            w.g3.alignment = ["fill","fill"];
                            w.g3.orientation = "row";
                            w.g3.stat = w.g3.add("statictext",undefined,'Set AI version Compatability:');
                            var ver = [
                                'Illustrator CC', '-', 'Legacy Formats',
                                'Illustrator CS6', 'Illustrator CS5', 'Illustrator CS4', 'Illustrator CS3', 'Illustrator CS2', 'Illustrator CS',
                                'Illustrator 10', 'Illustrator 9', 'Illustrator 8', 'Illustrator 3'
                            ];
                            w.g3.dd = w.g3.add("dropdownlist",undefined,ver);
                            w.g3.dd.alignment = ["right","top"];
                            w.g3.dd.items[2].enabled = false;
                            w.g3.dd.selection = 0;
                            w.g3.dd.enabled = w.g3.stat.enabled = false;
                            w.g3.dd.onChange = function() {
                                if(w.g3.dd.selection == null){
                                    w.g3.dd.selection = 0;
                                }
                            }
                            //
                            w.g4 = w.add("group");
                            w.g4.alignment = ["fill","fill"];
                            w.g4.orientation = "row";
                            w.g4.stat = w.g4.add("statictext",undefined,'Source Folder:');
                            w.g4.edit = w.g4.add("edittext",[100,0,300,23],'');
                            w.g4.edit.alignment = ["right","centre"];
                            w.g4.but = w.g4.add("button",undefined,'Browse...');
                            w.g4.but.alignment = ["right","top"];
                            w.g4.but.onClick = function() {
                                w.g4.edit.text = Folder.selectDialog('Select Source Folder...')||'';
                                w.g6.edit.text = w.g6.edit.text || w.g4.edit.text + '/processed';
                                w.g7.go.enabled = Folder(w.g4.edit.text).exists;
                            }
                            w.g4.edit.onDeactivate = function() {
                                w.g7.go.enabled = Folder(w.g4.edit.text).exists;
                            }
                            //
                            w.g5 = w.add("group");
                            w.g5.alignment = ["fill","fill"];
                            w.g5.orientation = "row";
                            w.g5.chk = w.g5.add("checkbox",undefined,'\u00A0Include subfolders?');
                            w.g5.chk.alignment = ["right","top"];
                            //
                            w.g6 = w.add("group");
                            w.g6.alignment = ["fill","fill"];
                            w.g6.orientation = "row";
                            w.g6.stat = w.g6.add("statictext",undefined,'Destination:');
                            w.g6.edit = w.g6.add("edittext",[100,0,300,23],'');
                            w.g6.edit.enabled = false;
                            w.g6.edit.alignment = ["right","centre"];
                            w.g6.but = w.g6.add("button",undefined,'Browse...');
                            w.g6.but.alignment = ["right","top"];
                            w.g6.but.onClick = function() {
                                w.g6.edit.text = Folder.selectDialog('Select Destination Folder...')||'';
                            }
                            //
                            w.g7 = w.add("group");
                            w.g7.alignment = ["fill","fill"];
                            w.g7.orientation = "row";
                            w.g7.go = w.g7.add("button",undefined,'Start Processing');
                            w.g7.go.enabled = false;
                            w.g7.stop = w.g7.add("button",undefined,'Cancel');
                            w.g7.stop.onClick = function(){
                                exit = false;
                                w.close();
                            }
                            w.g7.go.onClick = function(){
                                settings.fileType = w.g1.dd.selection.text;
                                if(w.g1.dd.selection.index > 1){
                                    if(w.g2.dd.selection.text == 'custom') {
                                        settings.resolution =  w.g2.cus.text.replace(' dpi','');
                                    }else{
                                        settings.resolution =  w.g2.dd.selection.text.replace(' dpi','');
                                    }
                                }else{
                                    settings.resolution = 'Not Set';
                                }
                                if(w.g1.dd.selection.index === 0){
                                    var vers = [17,null,null,16,15,14,13,12,11,10,9,8,3];
                                    settings.version = vers[w.g3.dd.selection.index];
                                }else{
                                    settings.version = 'Not Set';
                                }
                                settings.source = Folder(w.g4.edit.text)||'Not Set';
                                settings.destination = Folder(w.g6.edit.text)||'Not Set';
                                settings.subfolders = w.g5.chk.value;
                                w.close();
                            }
                        w.show();
                        }
                        // ------------------------------------------------------------------------------------------------------------------------------------------
                        var  timer, exit = true, files, settings = {};
                        window();
                        timer = Date.now();
                        
                        // Exit if cancelled
                        if(exit===false){return;}
                        
                        // Get list of files to process
                        if (settings.subfolders) {
                            files = find_files(settings.source, ['.ai']);
                        }else{
                            files = settings.source.getFiles('*.ai');  
                        }
                        
                        // exit if no files found
                        if(files.length === 0){
                            alert('no files found to process in:\n' + settings.source);
                            return;
                        }
                    
                    
                        // warn before possibly overwriting files
                        if(Folder(settings.destination).exists && settings.destination.getFiles().length>0){
                                var ow = confirm('Destination folder exists.\n' 
                                    + 'Continuing may result in files being overwritten.\n' 
                                    + 'Are you sure you wish to continue?')
                                if(ow===false){
                                    return;
                                }
                        }
                    
                    
                        // Create new folder if needed
                        var destination = new Folder(settings.destination);
                        if (!destination.exists){
                            destination.create();
                        }
                        
                        // start log string
                        var log = 'Image Processer\n\n' + DateTime() + '\n' 
                              + files.length + ' Files found in selected directory' + (settings.subfolders === true?' and subfolders':'') + '\n'
                              + 'Converting to ".' + settings.fileType + '"' + (settings.resolution > 0?' at ' + settings.resolution + ' dpi':'') + '\n'
                              + (settings.fileType==='ai'?'with Illustrator compatability to Version ' + settings.version + '\n':'')
                              + 'From:    ' + settings.source + '\n'
                              + 'To:        ' + settings.destination + '\n\n';
                    
                    
                        // Process Files
                        var count = 0;
                        for(var i = 0; i<files.length; i++) {
                            var doc = app.open(files[i]);
                            var newDoc = new File(destination + '/' 
                                + (files[i].name.substr(0, files[i].name.lastIndexOf('.')) || files[i].name) 
                                + (settings.fileType=='ai'?'_v' + settings.version:'') + '.' + settings.fileType);
                            
                            switch(settings.fileType){
                                case 'ai':
                                    exportAI(doc, newDoc, settings.version);
                                    break;
                                case 'pdf':
                                    exportPDF(doc, newDoc);
                                    break;
                                case 'psd':
                                    exportPSD(doc, newDoc, settings.resolution);
                                    break;
                                case 'jpg':
                                    exportJPG(doc, newDoc, settings.resolution);
                                    break;
                                case 'png':
                                    exportPNG(doc, newDoc, settings.resolution)
                                    break;
                            }
                            count++
                            log = log + count + ')   ' + decodeURIComponent(files[i].name) + ' ------> ' + decodeURIComponent(newDoc.name) + '\n'
                        }
                        // finalise log file
                        log = log + '\nTotal of ' + count + (count>1?' files':' file') + ' Processed\nCompleted on the ' + DateTime();
                        createLog(log, destination);
                        var x = (Date.now()-timer);
                        var viewLog = confirm('conversion completed in:\n' 
                            + Math.floor(x/1000/60) + ' Minutes, '
                            + Math.floor(x/1000%60) + ' Seconds, and '
                            + x%1000 + ' Milliseconds\n\nWould you like to view the log file?');
                        if(viewLog){
                            File(destination + '/textlog.txt').execute();
                        }
                    }
                    main();
                    
                    • 7. Re: improve or simplify this batch script
                      julioc4816867 Level 1

                      oh! It is perfect, very clean and legible, thanks for helping me you're a very nice person, follow your advice , I will study more this code to keep adding more stuff

                      I try to find out how to add these things:

                       

                      in pdf, add password option, select preset config.

                      in ai add embed links on/off

                      in psd, add cmyk or Rgb option

                      add text box to add script code Function

                      • 8. Re: improve or simplify this batch script
                        Qwertyfly... Level 4

                        Update... Already!!!

                         

                        found a bug where x to close caused error.

                        added password option for PDFs

                         

                        //----------------------------------------------------------------------------
                        //
                        //          Illustrator Image Processer
                        //
                        //----------------------------------------------------------------------------
                        //
                        //          Original Script by:
                        //              Julioc4816867
                        //
                        //          Rebuilt by:
                        //              Qwertyfly
                        //              tristan@qwertyfly.com
                        //
                        //          Version 1.1
                        //              27/7/15
                        //
                        //----------------------------------------------------------------------------
                        //          Version History
                        //
                        //          Version 1.0
                        //          - The main addition to this script is the GUI
                        //          - Also alot of validation and a bit of code cleanup
                        //          - Please let me know of any bugs found
                        //          - Please let me know of any feature requests
                        //
                        //          Version 1.1
                        //          - Added Password option for PDFs
                        //          - fixed bug where x to close caused error
                        //          
                        //----------------------------------------------------------------------------
                        function main(){
                            // ---------------------------------recurse subfolders - Thanks to Peter Kharel  ----------------------------------------------
                            function find_files(dir, mask_array) {  
                                var arr = [];  
                                for (var i = 0; i < mask_array.length; i++) {  
                                    arr = arr.concat(find_files_sub(dir, [], mask_array[i].toUpperCase()));  
                                }  
                                return arr;  
                            }  
                        
                        
                            function find_files_sub(dir, array, mask) {  
                                var f = Folder(dir).getFiles('*.*');  
                                for (var i = 0; i < f.length; i++) {  
                                    if (f[i]instanceof Folder) {  
                                        find_files_sub(f[i], array, mask);  
                                    }else if(f[i].name.substr(-mask.length).toUpperCase() == mask) {  
                                        array.push(f[i]);  
                                    }  
                                }  
                                return array;  
                            } 
                            // ------------------------------------------------------------------------------------------------------------------------------------------
                            function exportAI(doc, newFile, version) {  
                                var illustratorSaveOpts = new IllustratorSaveOptions();  
                                illustratorSaveOpts.compatibility = Compatibility["ILLUSTRATOR" + version];
                                illustratorSaveOpts.compressed = true; // Version 10+
                                illustratorSaveOpts.pdfCompatible = true;  // Version 10+
                                illustratorSaveOpts.embedICCProfile = false;  // Version 9+
                                illustratorSaveOpts.embedLinkedFiles = true;  // Version 7+ 
                                illustratorSaveOpts.fontSubsetThreshold = 0.0;  
                                doc.saveAs(newFile, illustratorSaveOpts);  
                                doc.close(SaveOptions.DONOTSAVECHANGES);  
                            } 
                        
                        
                            function exportPDF(doc, newFile, pass) {  
                                var pdfSaveOpts = new PDFSaveOptions();  
                                pdfSaveOpts.acrobatLayers = false;  
                                pdfSaveOpts.colorBars = false;  
                                pdfSaveOpts.colorCompression = CompressionQuality.AUTOMATICJPEGHIGH;  
                                pdfSaveOpts.compressArt = true; //default  
                                pdfSaveOpts.embedICCProfile = true;  
                                pdfSaveOpts.enablePlainText = true;  
                                pdfSaveOpts.generateThumbnails = true; // default  
                                pdfSaveOpts.optimization = true;  
                                pdfSaveOpts.pageInformation = false;  
                                pdfSaveOpts.preserveEditability = false;  
                                pdfSaveOpts.requirePermissionPassword = (pass?true:false);  
                                if(pass){pdfSaveOpts.permissionPassword = pass;}
                                pdfSaveOpts.pDFAllowPrinting = PDFPrintAllowedEnum.PRINT128LOWRESOLUTION;  
                                doc.saveAs(newFile, pdfSaveOpts);  
                                doc.close(SaveOptions.DONOTSAVECHANGES);  
                            }  
                        
                        
                            function exportPSD(doc, newFile, resolution) {
                                var psdExportOpts = new ExportOptionsPhotoshop();  
                                doc.documentColorSpace == DocumentColorSpace.CMYK ? psdExportOpts.imageColorSpace = ImageColorSpace.CMYK : psdExportOpts.imageColorSpace = ImageColorSpace.RGB;  
                                psdExportOpts.antiAliasing = true;
                                psdExportOpts.embedICCProfile = true;  
                                psdExportOpts.writeLayers = true;  
                                psdExportOpts.resolution = resolution;  
                                psdExportOpts.maximumEditability = true;
                                doc.exportFile(newFile, ExportType.PHOTOSHOP, psdExportOpts);
                                doc.close(SaveOptions.DONOTSAVECHANGES);
                            }
                        
                        
                            function exportJPG(doc, newFile, resolution) {  
                                var jpgExportOpts = new ExportOptionsJPEG();  
                                jpgExportOpts.antiAliasing = true;  
                                jpgExportOpts.qualitySetting = 100;  
                                jpgExportOpts.horizontalScale = (resolution / 72) * 100;  
                                jpgExportOpts.verticalScale = (resolution / 72) * 100;  
                                jpgExportOpts.optimization = true;  
                                jpgExportOpts.artBoardClipping = true;  
                                doc.exportFile(newFile, ExportType.JPEG, jpgExportOpts);  
                                doc.close(SaveOptions.DONOTSAVECHANGES);   
                            }  
                        
                        
                            function exportPNG(doc, newFile, resolution) {  
                                var pngExportOpts = new ExportOptionsPNG24();  
                                pngExportOpts.antiAliasing = true;  
                                pngExportOpts.artBoardClipping = true;  
                                pngExportOpts.horizontalScale = (resolution / 72) * 100;  
                                //pngExportOpts.matte = true;  
                                //pngExportOpts.matteColor = 0, 0, 0;  
                                pngExportOpts.saveAsHTML = false;  
                                pngExportOpts.transparency = true;  
                                pngExportOpts.verticalScale = (resolution / 72) * 100;  
                                doc.exportFile(newFile, ExportType.PNG24, pngExportOpts);  
                                doc.close(SaveOptions.DONOTSAVECHANGES);  
                            } 
                            // ------------------------------------------------------------------------------------------------------------------------------------------
                            function DateTime(){
                                var now = new Date();
                                return (now.getDate()<10?'0':'') + now.getDate() + '/'
                                            +(now.getMonth()<9?'0':'')+(now.getMonth()+1)+'/'
                                            +now.getFullYear() + ' @ ' 
                                            + ((now.getHours() % 12)?(now.getHours() % 12):12) + ':' 
                                            + (now.getMinutes()<10?'0':'') + now.getMinutes() + ':' 
                                            + now.getSeconds() + (now.getHours()>=12?' pm':' am');
                            }
                        
                        
                            function createLog(log, destination){
                                txtlog = new File(destination + '/textlog.txt');
                                var isopen = txtlog.open("w"); //open file for editing  
                                if (isopen) { //test file is open    
                                    txtlog.seek(0, 0);  
                                    txtlog.write(log);  
                                    txtlog.close();  
                                }
                            }
                        
                        
                            function window() { 
                                var w = {};    
                                w = new Window("dialog",'Image Processor');
                                w.spacing = 5;
                                w.margins = 10;
                                w.alignChildren = "left";
                                //
                                w.g1 = w.add("group");
                                w.g1.alignment = ["fill","fill"];
                                w.g1.orientation = "row";
                                w.g1.stat = w.g1.add("statictext",undefined,'Select a File Type:');
                                w.g1.dd = w.g1.add("dropdownlist",undefined,['ai', 'pdf', 'psd', 'jpg', 'png']);
                                w.g1.dd.alignment = ["right","top"]; 
                                w.g1.dd.selection = 4;
                                w.g1.dd.onChange = function(){
                                    if(w.g1.dd.selection.index > 1){
                                        w.g2.dd.enabled = w.g2.stat.enabled = true;
                                    }else{
                                        w.g2.dd.enabled = w.g2.stat.enabled = false;
                                        if(w.g1.dd.selection.index == 0){
                                            w.g3.dd.enabled = w.g3.stat.enabled = true;
                                        }else{
                                            w.g3.dd.enabled = w.g3.stat.enabled = false;
                                        }
                                        if(w.g1.dd.selection.index == 1){
                                            w.g1.passtat = w.g1.add("statictext",[180,0,240,23],'Password:');
                                            w.g1.pass = w.g1.add("edittext",[240,0,320,23],'');
                                        }else{
                                            try{
                                                w.g1.remove(w.g1.passtat);
                                                w.g1.remove(w.g1.pass);
                                                w.layout.layout (true);
                                            }catch (e){} 
                                        }
                                    }
                                }
                                //
                                w.g2 = w.add("group");
                                w.g2.alignment = ["fill","fill"];
                                w.g2.orientation = "row";
                                w.g2.stat = w.g2.add("statictext",undefined,'Select a Resolution:');
                                w.g2.dd = w.g2.add("dropdownlist",undefined,['72 dpi', '150 dpi', '300 dpi', 'custom']);
                                w.g2.dd.alignment = ["right","top"];
                                w.g2.dd.selection = 2;
                                w.g2.dd.enabled = w.g2.stat.enabled = true;
                                w.g2.dd.onChange = function(){
                                    if(w.g2.dd.selection == 3){
                                        w.g2.cus = w.g2.add("edittext",[240,0,300,23]);
                                        w.g2.cus.active = true;
                                        w.g2.cus.onDeactivate = function() { //may be good to make sure number is within a certain range
                                            if(w.g2.cus.text == ''){
                                                w.g2.dd.selection = 2; // if blank revert to default of 300 dpi
                                            }else{
                                                w.g2.cus.text = (w.g2.cus.text = w.g2.cus.text.replace(/[^\d.]/g, '')) + ' dpi';
                                            }
                                        }
                                    }else{
                                        try{
                                            w.g2.remove(w.g2.cus);
                                            w.layout.layout (true);
                                        }catch (e){}
                                    }
                                }
                                //
                                w.g3 = w.add("group");
                                w.g3.alignment = ["fill","fill"];
                                w.g3.orientation = "row";
                                w.g3.stat = w.g3.add("statictext",undefined,'Set AI version Compatability:');
                                var ver = [
                                    'Illustrator CC', '-', 'Legacy Formats',
                                    'Illustrator CS6', 'Illustrator CS5', 'Illustrator CS4', 'Illustrator CS3', 'Illustrator CS2', 'Illustrator CS',
                                    'Illustrator 10', 'Illustrator 9', 'Illustrator 8', 'Illustrator 3'
                                ];
                                w.g3.dd = w.g3.add("dropdownlist",undefined,ver);
                                w.g3.dd.alignment = ["right","top"];
                                w.g3.dd.items[2].enabled = false;
                                w.g3.dd.selection = 0;
                                w.g3.dd.enabled = w.g3.stat.enabled = false;
                                w.g3.dd.onChange = function() {
                                    if(w.g3.dd.selection == null){
                                        w.g3.dd.selection = 0;
                                    }
                                }
                                //
                                w.g4 = w.add("group");
                                w.g4.alignment = ["fill","fill"];
                                w.g4.orientation = "row";
                                w.g4.stat = w.g4.add("statictext",undefined,'Source Folder:');
                                w.g4.edit = w.g4.add("edittext",[100,0,300,23],'');
                                w.g4.edit.alignment = ["right","centre"];
                                w.g4.but = w.g4.add("button",undefined,'Browse...');
                                w.g4.but.alignment = ["right","top"];
                                w.g4.but.onClick = function() {
                                    w.g4.edit.text = Folder.selectDialog('Select Source Folder...')||'';
                                    w.g6.edit.text = w.g6.edit.text || w.g4.edit.text + '/processed';
                                    w.g7.go.enabled = Folder(w.g4.edit.text).exists;
                                }
                                w.g4.edit.onDeactivate = function() {
                                    w.g7.go.enabled = Folder(w.g4.edit.text).exists;
                                }
                                //
                                w.g5 = w.add("group");
                                w.g5.alignment = ["fill","fill"];
                                w.g5.orientation = "row";
                                w.g5.chk = w.g5.add("checkbox",undefined,'\u00A0Include subfolders?');
                                w.g5.chk.alignment = ["right","top"];
                                //
                                w.g6 = w.add("group");
                                w.g6.alignment = ["fill","fill"];
                                w.g6.orientation = "row";
                                w.g6.stat = w.g6.add("statictext",undefined,'Destination:');
                                w.g6.edit = w.g6.add("edittext",[100,0,300,23],'');
                                w.g6.edit.enabled = false;
                                w.g6.edit.alignment = ["right","centre"];
                                w.g6.but = w.g6.add("button",undefined,'Browse...');
                                w.g6.but.alignment = ["right","top"];
                                w.g6.but.onClick = function() {
                                    w.g6.edit.text = Folder.selectDialog('Select Destination Folder...')||'';
                                }
                                //
                                w.g7 = w.add("group");
                                w.g7.alignment = ["fill","fill"];
                                w.g7.orientation = "row";
                                w.g7.go = w.g7.add("button",undefined,'Start Processing');
                                w.g7.go.enabled = false;
                                w.g7.stop = w.g7.add("button",undefined,'Cancel');
                                w.g7.stop.onClick = function(){
                                    w.close();
                                }
                                w.g7.go.onClick = function(){
                                    settings.fileType = w.g1.dd.selection.text;
                                    if(w.g1.dd.selection.index > 1){
                                        if(w.g2.dd.selection.text == 'custom') {
                                            settings.resolution =  w.g2.cus.text.replace(' dpi','');
                                        }else{
                                            settings.resolution =  w.g2.dd.selection.text.replace(' dpi','');
                                        }
                                    }else{
                                        settings.resolution = 'Not Set';
                                    }
                                    if(w.g1.dd.selection.index === 0){
                                        var vers = [17,null,null,16,15,14,13,12,11,10,9,8,3];
                                        settings.version = vers[w.g3.dd.selection.index];
                                    }else{
                                        settings.version = 'Not Set';
                                    }
                                    settings.source = Folder(w.g4.edit.text)||'Not Set';
                                    settings.destination = Folder(w.g6.edit.text)||'Not Set';
                                    settings.subfolders = w.g5.chk.value;
                                    settings.password = w.g1.pass.text;
                                    exit = true;
                                    w.close();
                                }
                            w.show();
                            }
                            // ------------------------------------------------------------------------------------------------------------------------------------------
                            var  timer, exit = false, files, settings = {};
                            window();
                            timer = Date.now();
                            
                            // Exit if cancelled
                            if(exit===false){return;}
                            
                            // Get list of files to process
                            if (settings.subfolders) {
                                files = find_files(settings.source, ['.ai']);
                            }else{
                                files = settings.source.getFiles('*.ai');  
                            }
                            
                            // exit if no files found
                            if(files.length === 0){
                                alert('no files found to process in:\n' + settings.source);
                                return;
                            }
                        
                        
                            // warn before possibly overwriting files
                            if(Folder(settings.destination).exists && settings.destination.getFiles().length>0){
                                    var ow = confirm('Destination folder exists.\n' 
                                        + 'Continuing may result in files being overwritten.\n' 
                                        + 'Are you sure you wish to continue?')
                                    if(ow===false){
                                        return;
                                    }
                            }
                        
                        
                            // Create new folder if needed
                            var destination = new Folder(settings.destination);
                            if (!destination.exists){
                                destination.create();
                            }
                            
                            // start log string
                            var log = 'Image Processer\n\n' + DateTime() + '\n' 
                                  + files.length + ' Files found in selected directory' + (settings.subfolders === true?' and subfolders':'') + '\n'
                                  + 'Converting to ".' + settings.fileType + '"' + (settings.resolution > 0?' at ' + settings.resolution + ' dpi':'') + '\n'
                                  + (settings.password?'Password: ' + settings.password + '\n':'')
                                  + (settings.fileType==='ai'?'with Illustrator compatability to Version ' + settings.version + '\n':'')
                                  + 'From:    ' + settings.source + '\n'
                                  + 'To:        ' + settings.destination + '\n\n';
                        
                        
                            // Process Files
                            var count = 0;
                            for(var i = 0; i<files.length; i++) {
                                var doc = app.open(files[i]);
                                var newDoc = new File(destination + '/' 
                                    + (files[i].name.substr(0, files[i].name.lastIndexOf('.')) || files[i].name) 
                                    + (settings.fileType=='ai'?'_v' + settings.version:'') + '.' + settings.fileType);
                                
                                switch(settings.fileType){
                                    case 'ai':
                                        exportAI(doc, newDoc, settings.version);
                                        break;
                                    case 'pdf':
                                        exportPDF(doc, newDoc, settings.password);
                                        break;
                                    case 'psd':
                                        exportPSD(doc, newDoc, settings.resolution);
                                        break;
                                    case 'jpg':
                                        exportJPG(doc, newDoc, settings.resolution);
                                        break;
                                    case 'png':
                                        exportPNG(doc, newDoc, settings.resolution)
                                        break;
                                }
                                count++
                                log = log + count + ')   ' + decodeURIComponent(files[i].name) + ' ------> ' + decodeURIComponent(newDoc.name) + '\n'
                            }
                            // finalise log file
                            log = log + '\nTotal of ' + count + (count>1?' files':' file') + ' Processed\nCompleted on the ' + DateTime();
                            createLog(log, destination);
                            var x = (Date.now()-timer);
                            var viewLog = confirm('conversion completed in:\n' 
                                + Math.floor(x/1000/60) + ' Minutes, '
                                + Math.floor(x/1000%60) + ' Seconds, and '
                                + x%1000 + ' Milliseconds\n\nWould you like to view the log file?');
                            if(viewLog){
                                File(destination + '/textlog.txt').execute();
                            }
                        }
                        main();
                        
                        • 9. Re: improve or simplify this batch script
                          W_J_T Level 4

                          Really nice job Qwertyfly, definitely above and beyond.

                          Qwertyfly... wrote:  I have done a rewrite for you. ... Your script is 235 lines and has much blank space. ... I managed to slim it down to 364 lines !?

                          Ha, ha.

                          • 10. Re: improve or simplify this batch script
                            julioc4816867 Level 1

                            Thanks, you are outstanding , this helped me a lot

                            • 12. Re: improve or simplify this batch script
                              julioc4816867 Level 1

                              I have a problem with my illustrator. In the version 1.1 the button "start processing"  does'nt works. But the version 1.0 its fine. Could you help me, please.

                              • 13. Re: improve or simplify this batch script
                                Qwertyfly... Level 4

                                the start Processing button is disabled at the start.

                                it will enable when the source folder is tested and exists.

                                if your typing the path in then you need to deselect the field before it will activate.

                                and it Wont activate if the folder does not exist.

                                 

                                does it work if you use the Browse... button?

                                • 14. Re: improve or simplify this batch script
                                  julioc4816867 Level 1

                                  Browse button works, I recorded my problem, please, you could see the video

                                   

                                  http://www.mediafire.com/download/j37wjnhwkjx7qni/Problem.zip

                                  • 15. Re: improve or simplify this batch script
                                    Qwertyfly... Level 4

                                    small issue.

                                    I did not pick up on it because:

                                    Illustrator uses the same javascript engine for the life of the session.

                                    so all variables sit in memory until illustrator is restarted.

                                    so I tested pdf output which created the w.g1.pass object.

                                    because this then exists until restart when I tested PNG there was no issue passing that to the settings.password object.

                                    but if illustrator is restarted and png is run then the w.g1.pass object is never created.

                                    this then causes the fail when trying to pass a non existent object to the settings object.

                                    fix is to test if pdf and only set a password in the settings object if processing to pdf.

                                     

                                    replace this:

                                    settings.password = w.g1.pass.text;
                                    

                                     

                                    with this:

                                     if(w.g1.dd.selection.index == 1){
                                       settings.password = w.g1.pass.text;
                                     }
                                    

                                     

                                     

                                     

                                    side note:

                                    if your scripting a bit the the ESTK is a great tool. (ExtendScript ToolKit)

                                    this will really help in locating why and where a script fails.

                                    here is a screen shot of it stopping with this issue:

                                    note ant the bottom it states undefined is not an object, that's because the w.g1.pass.text was never created.

                                    un.JPG

                                    • 16. Re: improve or simplify this batch script
                                      Qwertyfly... Level 4
                                      //----------------------------------------------------------------------------
                                      //
                                      //          Illustrator Image Processer
                                      //
                                      //----------------------------------------------------------------------------
                                      //
                                      //          Original Script by:
                                      //              Julioc4816867
                                      //
                                      //          Rebuilt by:
                                      //              Qwertyfly
                                      //              tristan@qwertyfly.com
                                      //
                                      //          Version 1.2
                                      //              11/8/15
                                      //
                                      //----------------------------------------------------------------------------
                                      //          Version History
                                      //
                                      //          Version 1.0
                                      //          - The main addition to this script is the GUI
                                      //          - Also alot of validation and a bit of code cleanup
                                      //          - Please let me know of any bugs found
                                      //          - Please let me know of any feature requests
                                      //
                                      //          Version 1.1
                                      //          - Added Password option for PDFs
                                      //          - fixed bug where x to close caused error
                                      //
                                      //          Version 1.2
                                      //          - Fixed bug introduced with password option when not processing to pdf
                                      //          
                                      //----------------------------------------------------------------------------
                                      function main(){
                                          // ---------------------------------recurse subfolders - Thanks to Peter Kharel  ----------------------------------------------
                                          function find_files(dir, mask_array) {  
                                              var arr = [];  
                                              for (var i = 0; i < mask_array.length; i++) {  
                                                  arr = arr.concat(find_files_sub(dir, [], mask_array[i].toUpperCase()));  
                                              }  
                                              return arr;  
                                          }  
                                      
                                      
                                          function find_files_sub(dir, array, mask) {  
                                              var f = Folder(dir).getFiles('*.*');  
                                              for (var i = 0; i < f.length; i++) {  
                                                  if (f[i]instanceof Folder) {  
                                                      find_files_sub(f[i], array, mask);  
                                                  }else if(f[i].name.substr(-mask.length).toUpperCase() == mask) {  
                                                      array.push(f[i]);  
                                                  }  
                                              }  
                                              return array;  
                                          } 
                                          // ------------------------------------------------------------------------------------------------------------------------------------------
                                          function exportAI(doc, newFile, version) {  
                                              var illustratorSaveOpts = new IllustratorSaveOptions();  
                                              illustratorSaveOpts.compatibility = Compatibility["ILLUSTRATOR" + version];
                                              illustratorSaveOpts.compressed = true; // Version 10+
                                              illustratorSaveOpts.pdfCompatible = true;  // Version 10+
                                              illustratorSaveOpts.embedICCProfile = false;  // Version 9+
                                              illustratorSaveOpts.embedLinkedFiles = true;  // Version 7+ 
                                              illustratorSaveOpts.fontSubsetThreshold = 0.0;  
                                              doc.saveAs(newFile, illustratorSaveOpts);  
                                              doc.close(SaveOptions.DONOTSAVECHANGES);  
                                          } 
                                      
                                      
                                          function exportPDF(doc, newFile, pass) {  
                                              var pdfSaveOpts = new PDFSaveOptions();  
                                              pdfSaveOpts.acrobatLayers = false;  
                                              pdfSaveOpts.colorBars = false;  
                                              pdfSaveOpts.colorCompression = CompressionQuality.AUTOMATICJPEGHIGH;  
                                              pdfSaveOpts.compressArt = true; //default  
                                              pdfSaveOpts.embedICCProfile = true;  
                                              pdfSaveOpts.enablePlainText = true;  
                                              pdfSaveOpts.generateThumbnails = true; // default  
                                              pdfSaveOpts.optimization = true;  
                                              pdfSaveOpts.pageInformation = false;  
                                              pdfSaveOpts.preserveEditability = false;  
                                              pdfSaveOpts.requirePermissionPassword = (pass?true:false);  
                                              if(pass){pdfSaveOpts.permissionPassword = pass;}
                                              pdfSaveOpts.pDFAllowPrinting = PDFPrintAllowedEnum.PRINT128LOWRESOLUTION;  
                                              doc.saveAs(newFile, pdfSaveOpts);  
                                              doc.close(SaveOptions.DONOTSAVECHANGES);  
                                          }  
                                      
                                      
                                          function exportPSD(doc, newFile, resolution) {
                                              var psdExportOpts = new ExportOptionsPhotoshop();  
                                              doc.documentColorSpace == DocumentColorSpace.CMYK ? psdExportOpts.imageColorSpace = ImageColorSpace.CMYK : psdExportOpts.imageColorSpace = ImageColorSpace.RGB;  
                                              psdExportOpts.antiAliasing = true;
                                              psdExportOpts.embedICCProfile = true;  
                                              psdExportOpts.writeLayers = true;  
                                              psdExportOpts.resolution = resolution;  
                                              psdExportOpts.maximumEditability = true;
                                              doc.exportFile(newFile, ExportType.PHOTOSHOP, psdExportOpts);
                                              doc.close(SaveOptions.DONOTSAVECHANGES);
                                          }
                                      
                                      
                                          function exportJPG(doc, newFile, resolution) {  
                                              var jpgExportOpts = new ExportOptionsJPEG();  
                                              jpgExportOpts.antiAliasing = true;  
                                              jpgExportOpts.qualitySetting = 100;  
                                              jpgExportOpts.horizontalScale = (resolution / 72) * 100;  
                                              jpgExportOpts.verticalScale = (resolution / 72) * 100;  
                                              jpgExportOpts.optimization = true;  
                                              jpgExportOpts.artBoardClipping = true;  
                                              doc.exportFile(newFile, ExportType.JPEG, jpgExportOpts);  
                                              doc.close(SaveOptions.DONOTSAVECHANGES);   
                                          }  
                                      
                                      
                                          function exportPNG(doc, newFile, resolution) {  
                                              var pngExportOpts = new ExportOptionsPNG24();  
                                              pngExportOpts.antiAliasing = true;  
                                              pngExportOpts.artBoardClipping = true;  
                                              pngExportOpts.horizontalScale = (resolution / 72) * 100;  
                                              //pngExportOpts.matte = true;  
                                              //pngExportOpts.matteColor = 0, 0, 0;  
                                              pngExportOpts.saveAsHTML = false;  
                                              pngExportOpts.transparency = true;  
                                              pngExportOpts.verticalScale = (resolution / 72) * 100;  
                                              doc.exportFile(newFile, ExportType.PNG24, pngExportOpts);  
                                              doc.close(SaveOptions.DONOTSAVECHANGES);  
                                          } 
                                          // ------------------------------------------------------------------------------------------------------------------------------------------
                                          function DateTime(){
                                              var now = new Date();
                                              return (now.getDate()<10?'0':'') + now.getDate() + '/'
                                                          +(now.getMonth()<9?'0':'')+(now.getMonth()+1)+'/'
                                                          +now.getFullYear() + ' @ ' 
                                                          + ((now.getHours() % 12)?(now.getHours() % 12):12) + ':' 
                                                          + (now.getMinutes()<10?'0':'') + now.getMinutes() + ':' 
                                                          + now.getSeconds() + (now.getHours()>=12?' pm':' am');
                                          }
                                      
                                      
                                          function createLog(log, destination){
                                              txtlog = new File(destination + '/textlog.txt');
                                              var isopen = txtlog.open("w"); //open file for editing  
                                              if (isopen) { //test file is open    
                                                  txtlog.seek(0, 0);  
                                                  txtlog.write(log);  
                                                  txtlog.close();  
                                              }
                                          }
                                      
                                      
                                          function window() { 
                                              var w = {};    
                                              w = new Window("dialog",'Image Processor');
                                              w.spacing = 5;
                                              w.margins = 10;
                                              w.alignChildren = "left";
                                              //
                                              w.g1 = w.add("group");
                                              w.g1.alignment = ["fill","fill"];
                                              w.g1.orientation = "row";
                                              w.g1.stat = w.g1.add("statictext",undefined,'Select a File Type:');
                                              w.g1.dd = w.g1.add("dropdownlist",undefined,['ai', 'pdf', 'psd', 'jpg', 'png']);
                                              w.g1.dd.alignment = ["right","top"]; 
                                              w.g1.dd.selection = 4;
                                              w.g1.dd.onChange = function(){
                                                  if(w.g1.dd.selection.index > 1){
                                                      w.g2.dd.enabled = w.g2.stat.enabled = true;
                                                  }else{
                                                      w.g2.dd.enabled = w.g2.stat.enabled = false;
                                                      if(w.g1.dd.selection.index == 0){
                                                          w.g3.dd.enabled = w.g3.stat.enabled = true;
                                                      }else{
                                                          w.g3.dd.enabled = w.g3.stat.enabled = false;
                                                      }
                                                      if(w.g1.dd.selection.index == 1){
                                                          w.g1.passtat = w.g1.add("statictext",[180,0,240,23],'Password:');
                                                          w.g1.pass = w.g1.add("edittext",[240,0,320,23],'');
                                                      }else{
                                                          try{
                                                              w.g1.remove(w.g1.passtat);
                                                              w.g1.remove(w.g1.pass);
                                                              w.layout.layout (true);
                                                          }catch (e){} 
                                                      }
                                                  }
                                              }
                                              //
                                              w.g2 = w.add("group");
                                              w.g2.alignment = ["fill","fill"];
                                              w.g2.orientation = "row";
                                              w.g2.stat = w.g2.add("statictext",undefined,'Select a Resolution:');
                                              w.g2.dd = w.g2.add("dropdownlist",undefined,['72 dpi', '150 dpi', '300 dpi', 'custom']);
                                              w.g2.dd.alignment = ["right","top"];
                                              w.g2.dd.selection = 2;
                                              w.g2.dd.enabled = w.g2.stat.enabled = true;
                                              w.g2.dd.onChange = function(){
                                                  if(w.g2.dd.selection == 3){
                                                      w.g2.cus = w.g2.add("edittext",[240,0,300,23]);
                                                      w.g2.cus.active = true;
                                                      w.g2.cus.onDeactivate = function() { //may be good to make sure number is within a certain range
                                                          if(w.g2.cus.text == ''){
                                                              w.g2.dd.selection = 2; // if blank revert to default of 300 dpi
                                                          }else{
                                                              w.g2.cus.text = (w.g2.cus.text = w.g2.cus.text.replace(/[^\d.]/g, '')) + ' dpi';
                                                          }
                                                      }
                                                  }else{
                                                      try{
                                                          w.g2.remove(w.g2.cus);
                                                          w.layout.layout (true);
                                                      }catch (e){}
                                                  }
                                              }
                                              //
                                              w.g3 = w.add("group");
                                              w.g3.alignment = ["fill","fill"];
                                              w.g3.orientation = "row";
                                              w.g3.stat = w.g3.add("statictext",undefined,'Set AI version Compatability:');
                                              var ver = [
                                                  'Illustrator CC', '-', 'Legacy Formats',
                                                  'Illustrator CS6', 'Illustrator CS5', 'Illustrator CS4', 'Illustrator CS3', 'Illustrator CS2', 'Illustrator CS',
                                                  'Illustrator 10', 'Illustrator 9', 'Illustrator 8', 'Illustrator 3'
                                              ];
                                              w.g3.dd = w.g3.add("dropdownlist",undefined,ver);
                                              w.g3.dd.alignment = ["right","top"];
                                              w.g3.dd.items[2].enabled = false;
                                              w.g3.dd.selection = 0;
                                              w.g3.dd.enabled = w.g3.stat.enabled = false;
                                              w.g3.dd.onChange = function() {
                                                  if(w.g3.dd.selection == null){
                                                      w.g3.dd.selection = 0;
                                                  }
                                              }
                                              //
                                              w.g4 = w.add("group");
                                              w.g4.alignment = ["fill","fill"];
                                              w.g4.orientation = "row";
                                              w.g4.stat = w.g4.add("statictext",undefined,'Source Folder:');
                                              w.g4.edit = w.g4.add("edittext",[100,0,300,23],'');
                                              w.g4.edit.alignment = ["right","centre"];
                                              w.g4.but = w.g4.add("button",undefined,'Browse...');
                                              w.g4.but.alignment = ["right","top"];
                                              w.g4.but.onClick = function() {
                                                  w.g4.edit.text = Folder.selectDialog('Select Source Folder...')||'';
                                                  w.g6.edit.text = w.g6.edit.text || w.g4.edit.text + '/processed';
                                                  w.g7.go.enabled = Folder(w.g4.edit.text).exists;
                                              }
                                              w.g4.edit.onDeactivate = function() {
                                                  w.g7.go.enabled = Folder(w.g4.edit.text).exists;
                                              }
                                              //
                                              w.g5 = w.add("group");
                                              w.g5.alignment = ["fill","fill"];
                                              w.g5.orientation = "row";
                                              w.g5.chk = w.g5.add("checkbox",undefined,'\u00A0Include subfolders?');
                                              w.g5.chk.alignment = ["right","top"];
                                              //
                                              w.g6 = w.add("group");
                                              w.g6.alignment = ["fill","fill"];
                                              w.g6.orientation = "row";
                                              w.g6.stat = w.g6.add("statictext",undefined,'Destination:');
                                              w.g6.edit = w.g6.add("edittext",[100,0,300,23],'');
                                              w.g6.edit.enabled = false;
                                              w.g6.edit.alignment = ["right","centre"];
                                              w.g6.but = w.g6.add("button",undefined,'Browse...');
                                              w.g6.but.alignment = ["right","top"];
                                              w.g6.but.onClick = function() {
                                                  w.g6.edit.text = Folder.selectDialog('Select Destination Folder...')||'';
                                              }
                                              //
                                              w.g7 = w.add("group");
                                              w.g7.alignment = ["fill","fill"];
                                              w.g7.orientation = "row";
                                              w.g7.go = w.g7.add("button",undefined,'Start Processing');
                                              w.g7.go.enabled = false;
                                              w.g7.stop = w.g7.add("button",undefined,'Cancel');
                                              w.g7.stop.onClick = function(){
                                                  w.close();
                                              }
                                              w.g7.go.onClick = function(){
                                                  settings.fileType = w.g1.dd.selection.text;
                                                  if(w.g1.dd.selection.index > 1){
                                                      if(w.g2.dd.selection.text == 'custom') {
                                                          settings.resolution =  w.g2.cus.text.replace(' dpi','');
                                                      }else{
                                                          settings.resolution =  w.g2.dd.selection.text.replace(' dpi','');
                                                      }
                                                  }else{
                                                      settings.resolution = 'Not Set';
                                                  }
                                                  if(w.g1.dd.selection.index === 0){
                                                      var vers = [17,null,null,16,15,14,13,12,11,10,9,8,3];
                                                      settings.version = vers[w.g3.dd.selection.index];
                                                  }else{
                                                      settings.version = 'Not Set';
                                                  }
                                                  settings.source = Folder(w.g4.edit.text)||'Not Set';
                                                  settings.destination = Folder(w.g6.edit.text)||'Not Set';
                                                  settings.subfolders = w.g5.chk.value;
                                                  if(w.g1.dd.selection.index == 1){  
                                                      settings.password = w.g1.pass.text;  
                                                  } 
                                                  exit = true;
                                                  w.close();
                                              }
                                          w.show();
                                          }
                                          // ------------------------------------------------------------------------------------------------------------------------------------------
                                          var  timer, exit = false, files, settings = {};
                                          window();
                                          timer = Date.now();
                                          
                                          // Exit if cancelled
                                          if(exit===false){return;}
                                          
                                          // Get list of files to process
                                          if (settings.subfolders) {
                                              files = find_files(settings.source, ['.ai']);
                                          }else{
                                              files = settings.source.getFiles('*.ai');  
                                          }
                                          
                                          // exit if no files found
                                          if(files.length === 0){
                                              alert('no files found to process in:\n' + settings.source);
                                              return;
                                          }
                                      
                                      
                                          // warn before possibly overwriting files
                                          if(Folder(settings.destination).exists && settings.destination.getFiles().length>0){
                                                  var ow = confirm('Destination folder exists.\n' 
                                                      + 'Continuing may result in files being overwritten.\n' 
                                                      + 'Are you sure you wish to continue?')
                                                  if(ow===false){
                                                      return;
                                                  }
                                          }
                                      
                                      
                                          // Create new folder if needed
                                          var destination = new Folder(settings.destination);
                                          if (!destination.exists){
                                              destination.create();
                                          }
                                          
                                          // start log string
                                          var log = 'Image Processer\n\n' + DateTime() + '\n' 
                                                + files.length + ' Files found in selected directory' + (settings.subfolders === true?' and subfolders':'') + '\n'
                                                + 'Converting to ".' + settings.fileType + '"' + (settings.resolution > 0?' at ' + settings.resolution + ' dpi':'') + '\n'
                                                + (settings.password?'Password: ' + settings.password + '\n':'')
                                                + (settings.fileType==='ai'?'with Illustrator compatability to Version ' + settings.version + '\n':'')
                                                + 'From:    ' + settings.source + '\n'
                                                + 'To:        ' + settings.destination + '\n\n';
                                      
                                      
                                          // Process Files
                                          var count = 0;
                                          for(var i = 0; i<files.length; i++) {
                                              var doc = app.open(files[i]);
                                              var newDoc = new File(destination + '/' 
                                                  + (files[i].name.substr(0, files[i].name.lastIndexOf('.')) || files[i].name) 
                                                  + (settings.fileType=='ai'?'_v' + settings.version:'') + '.' + settings.fileType);
                                              
                                              switch(settings.fileType){
                                                  case 'ai':
                                                      exportAI(doc, newDoc, settings.version);
                                                      break;
                                                  case 'pdf':
                                                      exportPDF(doc, newDoc, settings.password);
                                                      break;
                                                  case 'psd':
                                                      exportPSD(doc, newDoc, settings.resolution);
                                                      break;
                                                  case 'jpg':
                                                      exportJPG(doc, newDoc, settings.resolution);
                                                      break;
                                                  case 'png':
                                                      exportPNG(doc, newDoc, settings.resolution)
                                                      break;
                                              }
                                              count++
                                              log = log + count + ')   ' + decodeURIComponent(files[i].name) + ' ------> ' + decodeURIComponent(newDoc.name) + '\n'
                                          }
                                          // finalise log file
                                          log = log + '\nTotal of ' + count + (count>1?' files':' file') + ' Processed\nCompleted on the ' + DateTime();
                                          createLog(log, destination);
                                          var x = (Date.now()-timer);
                                          var viewLog = confirm('conversion completed in:\n' 
                                              + Math.floor(x/1000/60) + ' Minutes, '
                                              + Math.floor(x/1000%60) + ' Seconds, and '
                                              + x%1000 + ' Milliseconds\n\nWould you like to view the log file?');
                                          if(viewLog){
                                              File(destination + '/textlog.txt').execute();
                                          }
                                      }
                                      main();