PDF Export Folder

Script for Adobe InDesign

The purpose of the script is twofold:

1. Export a folder of InDesign documents to PDF as simply as possible. Other scripts perform the same and more, and with more options. Users are encouraged to use those scripts when further capability is needed. The point of this script was to accomplish one specific task without excess.

2. Act as a template to create other scripts that process InDesign documents, whatever the process might be. The script can perform other tasks by altering the processFile() function, called for each InDesign document found in the selected folder. Also further options are easily added to the interface.

  • Option to include subfolders
  • Export using any defined PDF Preset
  • Option to add suffix to output file names
  • Option to replace existing output files.
  • Adapt open source to customize or create other scripts
Download
PDF Export Folder
Many scripts are free to download thanks to the support of users. Help me keep developing new scripts by supporting my work. Click the PayPal button to contribute any amount. Thank you.

How to use the script

The interface has two sections: Process and Options. Set desired options then click the OK button to begin. A progress bar is displayed as documents are processed.

Section 1: Process

Folder — select a folder that contains InDesign documents. Each will be exported to a PDF in the same folder. Only files with the .indd extension are processed.

Include subfolders — if enabled, documents in all subfolders are also processed.

Section 2: Options

PDF preset — the PDF preset used to export the InDesign files.

Original name + — an optional suffix of characters appended to output file names, prior to the file extension. The characters chosen must be legal to use in file names.

Replace existing output files — when enabled, existing output files are replaced without user intervention. When disabled, the user is prompted to confirm the replacement of each existing output file.

Source code

(download button below)

/*

PDF Export Folder
Copyright 2022 William Campbell
All Rights Reserved
https://www.marspremedia.com/contact

Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

*/

(function () {

    var title = "PDF Export Folder";

    if (!/indesign/i.test(app.name)) {
        alert("Script for InDesign", title, false);
        return;
    }

    // Script variables.
    var doneMessage;
    var error;
    var folderInput;
    var pdfPreset;
    var pdfPresetNames;
    var progress;

    // Reusable UI variables.
    var g; // group
    var p; // panel
    var w; // window

    // Permanent UI variables.
    var btnCancel;
    var btnOk;
    var btnFolderInput;
    var cbReplaceOutput;
    var cbSubfolders;
    var inpSuffix;
    var listPdfPresets;
    var txtFolderInput;

    // Load application PDF presets.
    pdfPresetNames = app.pdfExportPresets.everyItem().name;
    pdfPresetNames.sort();

    // CREATE PROGRESS WINDOW

    progress = new Window("palette", "Progress", undefined, {
        "closeButton": false
    });
    progress.t = progress.add("statictext");
    progress.t.preferredSize = [450, -1];
    progress.b = progress.add("progressbar");
    progress.b.preferredSize = [450, -1];
    progress.display = function (message) {
        message && (this.t.text = message);
        this.show();
        this.update();
    };
    progress.increment = function () {
        this.b.value++;
    };
    progress.set = function (steps) {
        this.b.value = 0;
        this.b.minvalue = 0;
        this.b.maxvalue = steps;
    };

    // CREATE USER INTERFACE

    w = new Window("dialog", title);
    w.alignChildren = "fill";

    // Panel 'Process'
    p = w.add("panel", undefined, "Process");
    p.alignChildren = "left";
    p.margins = [18, 24, 18, 12];
    g = p.add("group");
    btnFolderInput = g.add("button", undefined, "Folder...");
    txtFolderInput = g.add("statictext", undefined, "", {
        truncate: "middle"
    });
    txtFolderInput.preferredSize = [300, -1];
    g = p.add("group");
    g.margins = [36, 6, 0, 0];
    cbSubfolders = g.add("checkbox", undefined, "Include subfolders");

    // Panel 'Options'
    p = w.add("panel", undefined, "Options");
    p.alignChildren = "left";
    p.margins = [18, 18, 18, 12];
    g = p.add("group");
    g.add("statictext", undefined, "PDF Preset:");
    listPdfPresets = g.add("dropdownlist", undefined, pdfPresetNames);
    listPdfPresets.preferredSize = [240, -1];
    g = p.add("group");
    g.add("statictext", undefined, "Original file name  +");
    inpSuffix = g.add("edittext");
    inpSuffix.characters = 18;
    g = p.add("group");
    g.margins = [0, 6, 0, 0];
    cbReplaceOutput = g.add("checkbox", undefined, "Replace existing output files");

    // Action Buttons
    g = w.add("group");
    g.alignment = "center";
    btnOk = g.add("button", undefined, "OK");
    btnCancel = g.add("button", undefined, "Cancel");

    // Panel Copyright
    p = w.add("panel");
    p.add("statictext", undefined, "Copyright 2022 William Campbell");

    // UI ELEMENT EVENT HANDLERS

    // Panel 'Process'
    btnFolderInput.onClick = function () {
        var f = Folder.selectDialog("Select folder to process", txtFolderInput.text);
        if (f) {
            txtFolderInput.text = Folder.decode(f.fullName);
        }
    };

    // Panel 'Options'
    inpSuffix.onChange = function () {
        // Trim.
        this.text = this.text.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, "");
        // Remove illegal characters.
        var s = this.text.replace(/[\/\\:*?"<>|]/g, "");
        if (s != this.text) {
            this.text = s;
            alert("Illegal characters removed", " ", false);
        }
    };

    // Action Buttons
    btnOk.onClick = function () {
        folderInput = new Folder(txtFolderInput.text);
        if (!(folderInput && folderInput.exists)) {
            alert("Select folder to process", " ", false);
            return;
        }
        if (!listPdfPresets.selection) {
            alert("Select a PDF Preset", " ", false);
            return;
        }
        w.close(1);
    };
    btnCancel.onClick = function () {
        w.close(0);
    };

    // DISPLAY THE DIALOG

    if (w.show() == 1) {
        doneMessage = "";
        try {
            process();
            doneMessage = doneMessage || "Done";
        } catch (e) {
            error = error || e;
            doneMessage = "An error has occurred.\nLine " + error.line + ": " + error.message;
        }
        progress.close();
        doneMessage && alert(doneMessage, title, error);
    }

    //====================================================================
    //               END PROGRAM EXECUTION, BEGIN FUNCTIONS
    //====================================================================

    function getFiles(folder, subfolders, extensions) {
        // folder = folder object, not folder name.
        // subfolders = true to include subfolders.
        // extensions = string, extensions to include.
        // Combine multiple extensions with RegExp OR i.e. jpg|psd|tif
        // extensions case-insensitive.
        // extensions undefined = any.
        // Ignores hidden files and folders.
        var f;
        var files;
        var i;
        var pattern;
        var results = [];
        if (extensions) {
            pattern = new RegExp("\." + extensions + "$", "i");
        } else {
            // Any extension.
            pattern = new RegExp(".*");
        }
        files = folder.getFiles();
        for (i = 0; i < files.length; i++) {
            f = files[i];
            if (!f.hidden) {
                if (f instanceof Folder && subfolders) {
                    // Recursive (function calls itself).
                    results = results.concat(getFiles(f, subfolders, extensions));
                } else if (f instanceof File && pattern.test(f.name)) {
                    results.push(f);
                }
            }
        }
        return results;
    }

    function process() {
        var files;
        var i;
        // Preserve preferences.
        var preserve = {
            pdfExportPreferencesViewPDF: app.pdfExportPreferences.viewPDF
        };
        // Get PDF Preset to use.
        pdfPreset = app.pdfExportPresets.item(String(listPdfPresets.selection));
        // Don't view PDFs after export.
        app.pdfExportPreferences.viewPDF = false;
        progress.display("Reading folder...");
        try {
            files = getFiles(folderInput, cbSubfolders.value, "indd");
            if (!files.length) {
                doneMessage = "No InDesign files found in selected folder";
                return;
            }
            progress.set(files.length);
            // Loop through files array.
            for (i = 0; i < files.length; i++) {
                progress.display(File.decode(files[i].name));
                processFile(files[i]);
                progress.increment();
            }
        } finally {
            // Restore preferences.
            app.pdfExportPreferences.viewPDF = preserve.pdfExportPreferencesViewPDF;
            // Restore PDF export preferences to all pages.
            // (must set twice, once text another enumerated value).
            app.pdfExportPreferences.pageRange = "All Pages";
            app.pdfExportPreferences.pageRange = PageRange.ALL_PAGES;
        }
    }

    function processFile(file) {
        var doc;
        var filePdf;
        var name;
        var replace;
        doc = app.open(file);
        try {
            filePdf = new File(file.fullName.replace(/\.indd$/i, "") + inpSuffix.text + ".pdf");
            name = File.decode(filePdf.name);
            replace = true;
            if (!cbReplaceOutput.value && filePdf.exists) {
                replace = confirm("File exists. Replace?\n" + name, true, title);
            }
            if (replace) {
                app.pdfExportPreferences.pageRange = PageRange.ALL_PAGES;
                doc.exportFile(ExportFormat.PDF_TYPE, filePdf, false, pdfPreset);
            }
        } finally {
            doc.close(SaveOptions.NO);
        }
    }

})();
Many scripts are free to download thanks to the support of users. Help me keep developing new scripts by supporting my work. Click the PayPal button to contribute any amount. Thank you.
Download
PDF Export Folder

For help installing scripts, see How to Install and Use Scripts in Adobe Creative Cloud Applications.

Custom solutions based on any script, or completely new ideas, are possible at reasonable cost. Contact William for more information.

IMPORTANT: by downloading any of the scripts on this page you agree that the software is provided without any warranty, express or implied. USE AT YOUR OWN RISK. Always make backups of important data.

IMPORTANT: fees paid for products purchased from this site, or for programming custom solutions, are the purchase of a non-exclusive license to use the software and do not grant the purchaser any degree of ownership of the software. Author of the intellectual property and copyright holder William Campbell retains 100% ownership of all code used in all products and custom solutions.

IMPORTANT: scripts are developed for the latest Adobe Creative Cloud applications. Many scripts work in CC 2018 and later, even some as far back as CS6, but may not perform as expected, or run at all, when used in versions prior to 2018. Photoshop features Select Subject and Preserve Details 2.0 definitely fail prior to CC 2018 (version 19) as the features do not exist in earlier versions. For best results use the latest versions of Adobe Creative Cloud applications.