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
Help me keep making new scripts by supporting my work. Click the PayPal button to contribute any amount you choose. Thank you. William Campbell

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 file name + — a suffix of characters appended to each output file name. The characters entered must be legal to use in file names. Any illegal characters are automatically removed. Having no suffix is allowed, in which case output file names exactly match input file names.

Replace existing output files — when enabled, existing output files are replaced. When disabled, the user is asked to confirm the replacement.

Source code

(download button below)

/*

PDF Export Folder
Copyright 2023 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 listPdfPreset;
    var txtFolderInput;

    // LANGUAGE EXTENSIONS

    if (!String.prototype.trim) {
        String.prototype.trim = function () {
            return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, "");
        };
    }

    // SETUP

    // 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.width = 450;
    progress.b = progress.add("progressbar");
    progress.b.preferredSize.width = 450;
    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.width = 300;
    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:");
    listPdfPreset = g.add("dropdownlist", undefined, pdfPresetNames);
    listPdfPreset.preferredSize.width = 240;
    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 2023 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.trim();
        // 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 (!listPdfPreset.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(listPdfPreset.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.increment();
                progress.display(File.decode(files[i].name));
                processFile(files[i]);
            }
        } 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);
        }
    }

})();
Help me keep making new scripts by supporting my work. Click the PayPal button to contribute any amount you choose. Thank you. William Campbell
Download
PDF Export Folder

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

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.

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 software products are the purchase of a non-exclusive license to use the software product and do not grant the purchaser any degree of ownership of the software code. Author of the intellectual property and copyright holder William Campbell retains 100% ownership of all code used in all software products regardless of the inspiration for the software product design or functionality.