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

FREE to download
Please make a contribution

Many scripts are free to download thanks to the support of users. Help me keep developing new scripts by supporting my work. Click any one of the buttons below to make a contribution of any amount. Thank you.

× Stripe

Choose an amount to contribute.
Thank you for supporting my work.

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.

*/

//@target indesign

(function () {

    var title = "PDF Export Folder";

    // Script variables.
    var abort;
    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");
        if (f) {
            txtFolderInput.text = Folder.decode(f.fsName);
            folderInput = f;
        }
    };

    // 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 () {
        if (!txtFolderInput.text) {
            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) {
        try {
            process();
            alert(abort || "Done", title, false);
        } catch (e) {
            alert("An error has occurred.\nLine " + e.line + ": " + e.message, title, true);
        }
    }

    //====================================================================
    //               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) {
                abort = "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 {
            progress.close();
            // 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);
        }
    }

})();
× Stripe

Choose an amount to contribute.
Thank you for supporting my work.

Download
PDF Export Folder

License details included in download

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

Also available for hire to program custom solutions. 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.