Skip to main content
Known Participant
March 9, 2023
Answered

PDF and JPG Export Script

  • March 9, 2023
  • 1 reply
  • 1581 views

Hi,

I'm new to scripting and I think I've bitten off more than I can chew and I was wondering if someone can help clean up the mess I've created.

 

I'm trying to write a script that will do the following:

 

  1. Ask me for an export location on my drive
  2. Ask me at what resolution I would like to export my artboards (for the jpeg exports). 
  3. Then, save any artboard that has the name LAYOUT in it to a single PDF with the [Smallest File Size] setting.  (So if there were three artboard that had LAYOUT in the artboard name, I'd end up with three separate PDFs, each with ONLY one of the designated artboards).
  4. Save any artboard that does NOT have LAYOUT in the artboard name as a jpeg at the full size of the artboard and at the resolution that was set in the step 2 dialogue.
  5. I would like all of the saved files to have the naming structure "NameOfCurrentDocument_ArtboardName.extension"

 

Here is the script I've been messing with.  It's making some of the exports, but coming up with weird file names, and the PDFs include all of the artboard and don't seem to be compressing down the the settings in [Smallest Files Size], and it's exporting the same artboard multiple jpegs instead of one artboard per jpeg.  I'm guessing I've messed up a loop somewhere...

 

Any help or guidance would be so very appreciated.

 

// Define the target folder for the exported files
var exportFolder = Folder.selectDialog("Select a folder for export");

if (exportFolder != null) {
    // Get the current document
    var doc = app.activeDocument;
    // Get the current file name
    var fileName = doc.name.split(".")[0];
    //returns the number of artboards in the document
    var numArtboards = doc.artboards.length;
    //set the restoultion of the export
    var resolution = prompt("Please enter the resolution for export (in pixels per inch):");

    // Loop through all artboards
    for (var i = 0; i < numArtboards; i++) {
        var artboard = doc.artboards[i];
        var artboardName = artboard.name;

        // Check if the artboard name includes "Layout"
        if (artboardName.indexOf("LAYOUT") >= 0) {

            // Save the artboard as PDF with the "Smallest File Size" preset

            function saveAsPDF() {
                var saveFile = new File(exportFolder + "/" + fileName + "_" + artboardName + ".pdf");


                var pdfOptions = new PDFSaveOptions();
                pdfOptions.artboardRange = artboard;
                pdfOptions.compatibility = PDFCompatibility.ACROBAT5;
                pdfOptions.generateThumbnails = true;
                pdfOptions.preserveEditability = false;
                pdfOptions.preset = "[Smallest File Size]";
                app.activeDocument.saveAs(saveFile, PDFSaveOptions);
            }
            saveAsPDF()


        } else {
            // Export the artboard as JPEG with the specified resolution


            if (resolution != null) {

                var activeAB = doc.artboards[doc.artboards.getActiveArtboardIndex()];
                var options = new ImageCaptureOptions();
                options.resolution = resolution;
                options.artBoardClipping = true;
                options.antiAliasing = true;
                options.matte = false;
                options.horizontalScale = 100;
                options.verticalScale = 100;
                options.transparency = true;



                var destFile = new File(exportFolder + "/" + fileName + " " + artboardName + ".jpg");
                doc.imageCapture(destFile, activeAB.artboardRect, options);
            }
        }
    }
}

 

This topic has been closed for replies.
Correct answer jduncan

You were close, you just needed a few minor changes. Also, I added a default resolution value which will prepopulate the input field and also be what is used if the user cancels the prompt or enters a non-numeric value.

 

I also added a check to the first line to make sure at least one document was open before running the script. You can add an alert there if you wanted to let users know why the script didn't do anything.

 

Let me know if this works for you? Cheers!

 

 

// Define the target folder for the exported files
if (app.documents.length > 0)
  var exportFolder = Folder.selectDialog("Select a folder for export");

if (exportFolder != null) {
  // Get the current document
  var doc = app.activeDocument;
  // Get the current file name
  var fileName = doc.name.split(".")[0];
  //set the resolution of the export
  var defaultResolution = 150;
  var resolution = prompt(
    "Please enter the resolution for export (in pixels per inch):",
    defaultResolution
  );
  resolution =
    resolution === null || isNaN(resolution) ? defaultResolution : resolution;

  // Set up PDF Save Options
  var pdfOptions = new PDFSaveOptions();
  pdfOptions.compatibility = PDFCompatibility.ACROBAT5;
  pdfOptions.preserveEditability = false;
  pdfOptions.pDFPreset = "[Smallest File Size]";

  // Set up JPEG Save Options
  var jpegOptions = new ImageCaptureOptions();
  jpegOptions.antiAliasing = true;
  jpegOptions.matte = false;
  jpegOptions.resolution = resolution;
  jpegOptions.transparency = true;

  // Iterate through artboards and save them depending on their name
  var ab, filePath;
  for (var i = 0; i < doc.artboards.length; i++) {
    ab = doc.artboards[i];
    // Check if artboard name contains "LAYOUT", if so export PDF
    if (doc.artboards[i].name.indexOf("LAYOUT") >= 0) {
      filePath = new File(exportFolder + "/" + ab.name + ".pdf");
      // specify exact artboard to save in file
      pdfOptions.artboardRange = i + 1 + "";
      doc.saveAs(filePath, pdfOptions);
    } else {
      filePath = new File(exportFolder + "/" + ab.name + ".jpg");
      doc.imageCapture(filePath, ab.artboardRect, jpegOptions);
    }
  }
}

 

 

1 reply

jduncan
Community Expert
jduncanCommunity ExpertCorrect answer
Community Expert
March 9, 2023

You were close, you just needed a few minor changes. Also, I added a default resolution value which will prepopulate the input field and also be what is used if the user cancels the prompt or enters a non-numeric value.

 

I also added a check to the first line to make sure at least one document was open before running the script. You can add an alert there if you wanted to let users know why the script didn't do anything.

 

Let me know if this works for you? Cheers!

 

 

// Define the target folder for the exported files
if (app.documents.length > 0)
  var exportFolder = Folder.selectDialog("Select a folder for export");

if (exportFolder != null) {
  // Get the current document
  var doc = app.activeDocument;
  // Get the current file name
  var fileName = doc.name.split(".")[0];
  //set the resolution of the export
  var defaultResolution = 150;
  var resolution = prompt(
    "Please enter the resolution for export (in pixels per inch):",
    defaultResolution
  );
  resolution =
    resolution === null || isNaN(resolution) ? defaultResolution : resolution;

  // Set up PDF Save Options
  var pdfOptions = new PDFSaveOptions();
  pdfOptions.compatibility = PDFCompatibility.ACROBAT5;
  pdfOptions.preserveEditability = false;
  pdfOptions.pDFPreset = "[Smallest File Size]";

  // Set up JPEG Save Options
  var jpegOptions = new ImageCaptureOptions();
  jpegOptions.antiAliasing = true;
  jpegOptions.matte = false;
  jpegOptions.resolution = resolution;
  jpegOptions.transparency = true;

  // Iterate through artboards and save them depending on their name
  var ab, filePath;
  for (var i = 0; i < doc.artboards.length; i++) {
    ab = doc.artboards[i];
    // Check if artboard name contains "LAYOUT", if so export PDF
    if (doc.artboards[i].name.indexOf("LAYOUT") >= 0) {
      filePath = new File(exportFolder + "/" + ab.name + ".pdf");
      // specify exact artboard to save in file
      pdfOptions.artboardRange = i + 1 + "";
      doc.saveAs(filePath, pdfOptions);
    } else {
      filePath = new File(exportFolder + "/" + ab.name + ".jpg");
      doc.imageCapture(filePath, ab.artboardRect, jpegOptions);
    }
  }
}

 

 

ericsoupAuthor
Known Participant
March 9, 2023

Thank you so much.  The script does work well!  The only thing I added was the fileName to the exported file names.  

 

// Define the target folder for the exported files
if (app.documents.length > 0)
    var exportFolder = Folder.selectDialog("Select a folder for export");

if (exportFolder != null) {
    // Get the current document
    var doc = app.activeDocument;
    // Get the current file name
    var fileName = doc.name.split(".")[0];
    //set the resolution of the export
    var defaultResolution = 150;
    var resolution = prompt(
        "Please enter the resolution for export (in pixels per inch):",
        defaultResolution
    );
    resolution =
        resolution === null || isNaN(resolution) ? defaultResolution : resolution;

    // Set up PDF Save Options
    var pdfOptions = new PDFSaveOptions();
    pdfOptions.compatibility = PDFCompatibility.ACROBAT5;
    pdfOptions.preserveEditability = false;
    pdfOptions.pDFPreset = "[Smallest File Size]";

    // Set up JPEG Save Options
    var jpegOptions = new ImageCaptureOptions();
    jpegOptions.antiAliasing = true;
    jpegOptions.matte = false;
    jpegOptions.resolution = resolution;
    jpegOptions.transparency = false;

    // Iterate through artboards and save them depending on their name
    var ab, filepath;
    for (var i = 0; i < doc.artboards.length; i++) {
        ab = doc.artboards[i];
        // Check if artboard name contains "LAYOUT", if so export PDF
        if (doc.artboards[i].name.indexOf("LAYOUT") >= 0) {
            filePath = new File(exportFolder + "/" + fileName + "_" + ab.name + ".pdf");
            // specify exact artboard to save in file
            pdfOptions.artboardRange = i + 1 + "";
            doc.saveAs(filePath, pdfOptions);
        } else {
            filePath = new File(exportFolder + "/" + fileName + "_" + ab.name + ".jpg");
            doc.imageCapture(filePath, ab.artboardRect, jpegOptions);
        }
    }
}

 

I also realized that there is no way to control the JPEG qualtiy when using imageCapture, which is a shame because the whole point of this was to make smaller file sizes at the correct resolution for our needs.  And unfortunately using ExportOptionsJPEG() only allows you to control the quality and not the resolution.  It seems like there is a workaround that can be done using app.doScript(), but I'm going to have to get a little deeper into the scripting before I can conquer that.  

 

Thanks for the help.

 

 

jduncan
Community Expert
Community Expert
March 9, 2023

You could record actions that save at different resolutions and qualities but and run them through your script using `app.doScript("ActionName", "ActionGroup");` but then you are limited to save locations.

 

My other thought is to use `ExportOptionsJPEG` and just scale the JPEG to meet your specified ppi. The only issue here is whoever is getting/using the JPEG would need to know to adjust the ppi to the same amount you specified when running the script. For example, since the export JPEG feature saves at 72 ppi you could save it scaled at 416.666667% (300/72x100).