6 Replies Latest reply on Mar 6, 2015 4:22 PM by Zantcor

    Advanced "Save as PDF" script that saves 2 PDF presets with 2 different names.

    spaceheat

      Hi Everyone,

       

      I am looking to improve a save as pdf workflow and was hoping to get some direction. Here is the background...

       

      I routinely have to save numerous files as 2 separate PDFs with different settings (a high res printable version and a low res email version). Each file has to be renamed as follows...

       

      Original filename = MikesPDF.ai

      High Res PDF Filename = MikesPDF_HR.pdf

      Low Res PDF Filename = MikesPDF_LR.pdf

       

      I was able to alter the default "SaveAsPDF" script to save the files to my desired settings and to add the suffix to the name. So with these scripts here is how the workflow operates...

       

      1. Open all files I wish to save as pdfs

      2. Select script to save files as high res pdfs

      3. Illustrator asks me to choose a destination.

      4. Illustrator adds the appropriate suffix and saves each file as a pdf to my desired setting. ("Save As" not "Save a Copy").

      5. Now all of the open windows are the new pdfs, not the original ai files.

      6. Now I have to close each window. For some reason Illustrator asks me if I want to save each document when I tell it to close window, even though the file was just saved. I tell it to not save and everything seems to be fine.

      7. Reopen all the files I just saved as high res pdfs.

      8. Repeat the entire process except I run the script specifically designed for the low res pdfs.

       

      What I would like to do is to combine these two processes so that there will be one script that saves both pdfs. From what I understand, the script can't support "Save A Copy" so the workflow would go as follows...

       

      1. Open all files I wish to save as pdfs

      2. Select single script to save files as both high res and low res pdfs

      3. Illustrator asks me to choose a destination.

      4. Illustrator saves each file as a High Res PDF and adds the the "_HR" suffix.

      5. Illustrator then re-saves the open windows as a Low Res PDF and replaces the "_HR" suffix with "_LR".

       

      Here is the code for the High Res script, The Low Res script is pretty much the same except for a different preset name and different suffix. Any pointer that anyone could give me would be most appreciated. I am pretty much a noob to this stuff so please keep that in mind.

       

      Thanks!

      Mike

       

      ---------------------------CODE----------------------------

       

      /** Saves every document open in Illustrator

        as a PDF file in a user specified folder.

      */

       

       

      // Main Code [Execution of script begins here]

       

       

      try {

        // uncomment to suppress Illustrator warning dialogs

        // app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

       

       

        if (app.documents.length > 0 ) {

       

       

        // Get the folder to save the files into

        var destFolder = null;

        destFolder = Folder.selectDialog( 'Select folder for PDF files.', '~' );

       

       

        if (destFolder != null) {

        var options, i, sourceDoc, targetFile;

       

        // Get the PDF 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 pdf into

        targetFile = this.getTargetFile(sourceDoc.name, '.pdf', destFolder);

       

        // Save as pdf

        sourceDoc.saveAs( targetFile, options );

        }

        alert( 'Documents saved as PDF' );

        }

        }

        else{

        throw new Error('There are no document open!');

        }

      }

      catch(e) {

        alert( e.message, "Script Alert", true);

      }

       

       

      /** Returns the options to be used for the generated files. --------------------CHANGE PDF PRESET BELOW, var NamePreset = ----------------

        @return PDFSaveOptions object

      */

      function getOptions()

      {var NamePreset = 'Proof High Res PDF';

        // Create the required options object

        var options = new PDFSaveOptions();

           options.pDFPreset="High Res PDF";

        // See PDFSaveOptions in the JavaScript Reference for available options

       

        // Set the options you want below:

       

       

        // For example, uncomment to set the compatibility of the generated pdf to Acrobat 7 (PDF 1.6)

        // options.compatibility = PDFCompatibility.ACROBAT7;

       

        // For example, uncomment to view the pdfs in Acrobat after conversion

        // options.viewAfterSaving = true;

       

        return options;

      }

       

       

      /** Returns the file to save or export the document into.----------------CHANGE FILE SUFFIX ON LINE BELOW, var newName = ------------------

        @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 = "_HR";

       

       

        // 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;

        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;

      }

        • 1. Re: Advanced "Save as PDF" script that saves 2 PDF presets with 2 different names.
          bob138

          If it is OK for the new PDFs to be in the same folder as the original ai file then it's pretty easy and requires no extra input.  Sorry if this isn't what you were looking for.

           

          #target illustrator

          #targetengine "main"

           

          for (x=0;x<app.documents.length;x++)

          {

               var workingDoc = app.documents[x];

               var workingDocFile = workingDoc.fullName;

           

          // populate these with your settings

               var lowResOpts = new PDFSaveOptions();

               var highResOpts = new PDFSaveOptions();

           

               var lowResFile = new File(workingDocFile.toString().replace(".ai","_LR.pdf"));

               workingDoc.saveAs(lowResFile,lowResOpts);

           

               var highResFile = new File(workingDocFile.toString().replace(".ai","_HR.pdf"));

               workingDoc.saveAs(highResFile,highResOpts);

           

               workingDoc.close(SaveOptions.DONOTSAVECHANGES);

           

          }

          • 2. Re: Advanced "Save as PDF" script that saves 2 PDF presets with 2 different names.
            spaceheat Level 1

            Thank you for the reply Bob!

             

            Am I correct in assuming that these few lines of code replace all of the lines that I posted above?

             

            Also, When populating the PDFSaveOptions lines, would the end result look like this? FYI, LowRes preset name = "Proof Low Res PDF - CMYK". HighRes preset name = "Proof High Res PDF"

             

             

            #target illustrator

            #targetengine "main"

             

            for (x=0;x<app.documents.length;x++)

            {

                 var workingDoc = app.documents[x];

                 var workingDocFile = workingDoc.fullName;

             

            // populate these with your settings

                 var lowResOpts = new PDFSaveOptions(Proof Low Res PDF - CMYK);

                 var highResOpts = new PDFSaveOptions(Proof High Res PDF);

             

                 var lowResFile = new File(workingDocFile.toString().replace(".ai","_LR.pdf"));

                 workingDoc.saveAs(lowResFile,lowResOpts);

             

                 var highResFile = new File(workingDocFile.toString().replace(".ai","_HR.pdf"));

                 workingDoc.saveAs(highResFile,highResOpts);

             

                 workingDoc.close(SaveOptions.DONOTSAVECHANGES);

             

            }

            • 3. Re: Advanced "Save as PDF" script that saves 2 PDF presets with 2 different names.
              bob138 Level 1

              lowResOpts and highResOpts are objects that have properties that you can set like this:

               

              lowResOpts.colorDownsampling = 50;

              lowResOpts.compressArt = true;

               

              Full properties can be found in the illustrator javascript reference PDF file under "PDFSaveOptions"

               

              Bob

              • 4. Re: Advanced "Save as PDF" script that saves 2 PDF presets with 2 different names.
                spaceheat Level 1

                How about these lines that refer to simple presets... Would there be a way to incorporate these into the script instead of individually setting each option line?

                 

                function getOptions()

                {var NamePreset = 'Proof High Res PDF';

                  // Create the required options object

                  var options = new PDFSaveOptions();

                     options.pDFPreset="High Res PDF";

                • 5. Re: Advanced "Save as PDF" script that saves 2 PDF presets with 2 different names.
                  bob138 Level 1

                  It looks like that might work.  I've never worked with that property so I can't tell you anything about it but it's worth testing out.

                   

                  Bob

                  • 6. Re: Advanced "Save as PDF" script that saves 2 PDF presets with 2 different names.
                    Zantcor Level 1

                    You could condense everything down to something similar to the following couldn't you?

                     

                    
                    
                    
                    var destFolder = Folder.selectDialog( 'Select folder for PDF files.', '~' );
                    
                    for (i=0;i < app.documents.length; i++)
                    {
                    
                    var filename = app.activedocument
                    
                    function saveLowQualityPDF () //  saves the file to the server as a .PDF document.
                    { 
                        var thisDoc = app.activeDocument;
                        if ( app.documents.length > 0 )
                        {
                            var highQualitySavePlace = new File (destFolder + "/" + filename + "_Highquality");
                            highsaveOpts = new PDFSaveOptions();
                            highsaveOpts.compatibility = PDFCompatibility.ACROBAT6;
                            highsaveOpts.generateThumbnails = true;
                            highsaveOpts.preserveEditability = true;
                            thisDoc.saveAs( highQualitySavePlace, highsaveOpts );
                        }//end if
                    }//end save high quality
                    
                    function saveHighQualityPDF () //  saves the file to the server as a .PDF document.
                    { 
                        var thisDoc = app.activeDocument;
                        if ( app.documents.length > 0 )
                        {
                            var lowQualitySavePlace = new File (destFolder + "/" + filename + "_Lowquality");
                            lowsaveOpts = new PDFSaveOptions();
                            lowsaveOpts.compatibility = PDFCompatibility.ACROBAT6;
                            lowsaveOpts.generateThumbnails = true;
                            lowsaveOpts.preserveEditability = true;
                            lowsaveOpts.colorDownsampling = 50;
                            lowsaveOtps.compressArt = true;       
                            thisDoc.saveAs( lowQualitySavePlace, lowsaveOpts );
                        }//end if
                    }//end save low quality
                    
                    }//end for loop