1 Reply Latest reply on Jun 6, 2012 11:40 AM by Muppet Mark

    Export to PNG24 creates duplicate empty .png files

    Ian Fraiser

      I've modified the SaveDocsAsSVG.jsx file that comes with AI CS6 for PNG-24s. Pretty straight-forward, but along with my PNG-24s, the script is also creating duplicate empty .png files. Any ideas why this is happening? Thanks in advance!

       

       

      try {
                if (app.documents.length > 0 ) {
      
      
                          // Get the folder to save the files into
                          var destFolder = null;
                          destFolder = Folder.selectDialog( 'Select folder for PNG-24 files.', '~' );
      
      
                          if (destFolder != null) {
                                    var options, i, sourceDoc, targetFile; 
        
                              // Get the PNG options to be used.
                                    options = this.getOptions();
                              // You can tune these by changing the code in the getOptions() function.
                     
                                    for ( i = 0; i < app.documents.length; i++ ) {
                                              sourceDoc = app.documents[i]; // returns the document object
        
                                              // Get the file to save the document as png into
                                              targetFile = this.getTargetFile(sourceDoc.name, '.png', destFolder);
        
                                              // Save as PNG
                                              sourceDoc.exportFile(targetFile, ExportType.PNG24, options);
                                    }
                                    alert( 'Files are saved as PNG-24' );
                          }
                }
                else{
                          throw new Error('There are no documents open.');
                }
      }
      catch(e) {
                alert( e.message, "Script Alert", true);
      }
      
      
      /** Returns the options to be used for the generated files.
                @return ExportOptionsPNG object
      */
      function getOptions()
      {
                // Create the required options object
                var options = new ExportOptionsPNG24();
                // See ExportOptionsPNG24 in the JavaScript Reference for available options
        
                // Set the options you want below:
      
      
          options.transparency = true;
          options.artBoardClipping = false;
        
                return options;
      }
      
      
      /** Returns the file to save or export the document into.
                @param docName the name of the document
                @param ext the extension the file extension to be applied
                @param destFolder the output folder
                @return File object
      */
      function getTargetFile(docName, ext, destFolder) {
                var newName = "";
      
      
                // if name has no dot (and hence no extension),
                // just append the extension
                if (docName.indexOf('.') < 0) {
                          newName = docName + ext;
                } else {
                          var dot = docName.lastIndexOf('.');
                          newName += docName.substring(0, dot);
                          newName += ext;
                }
        
                // Create the file object to save to
                var myFile = new File( destFolder + '/' + newName );
        
                // Preflight access rights
                if (myFile.open("w")) {
                          myFile.close();
                }
                else {
                          throw new Error('Access is denied');
                }
                return myFile;
      }