• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
11

InDesign Export Script

Community Beginner ,
Feb 22, 2024 Feb 22, 2024

Copy link to clipboard

Copied

Hey guys,

 

i have an InDesign Script which exports all opened Documents at ones as PDFs and JPGs. You can also choose PDF-Presets, to seperate PDFs in different folders. The folders the folders will be created, when you run the script and the different PDFs will be placed in those folders. The folder Structure:

Bildschirmfoto 2024-02-22 um 10.02.51.png

 

My problem: For example I have 5 opened documents in InDesign which i want to export, my script exports all documents perfectly exept the first one. The first PDF only has one page. The example documents have 5 pages.

 

Here is my InDesign Structure before I run the Script:

Bildschirmfoto 2024-02-22 um 10.06.23.png

 

As you can see, I have 5 opened documents which all have 5 pages.

 

This is the Output i get in the Print folder and also in the Web folder:

 

Bildschirmfoto 2024-02-22 um 10.10.28.png

 

Here you can see, that the first pdf is a single page pdf. In the other document are 5 pages included.

 

Code:

// InDesign JavaScript

// Globale Variablen für die Exporteinstellungen
var globalExportSettings = null;

// Funktion für den Export als PDF mit einem bestimmten Preset, benutzerdefinierter Dateibezeichnung und Unterordnern
function exportPDFWithPresetAndNameAndFolders(presetName, outputPath, subfolder) {
    // Erstelle Unterverzeichnis, falls es nicht existiert
    var subfolderPath = outputPath + "/" + subfolder;
    var subfolderObj = new Folder(subfolderPath);
    if (!subfolderObj.exists) {
        subfolderObj.create();
    }

    var myPDFExportPreset = app.pdfExportPresets.itemByName(presetName);
    var exportedFile = new File(subfolderPath + "/" + app.activeDocument.name.replace(/\.[^\.]+$/, "") + "_" + myPDFExportPreset.name + "_" + subfolder + ".pdf");
    app.activeDocument.exportFile(ExportFormat.pdfType, exportedFile, false, myPDFExportPreset);

    // Rückgabe des exportierten Dateipfads für spätere Verwendung (falls benötigt)
    return exportedFile;
}

// Funktion für den Export als PDF mit einem bestimmten Preset und benutzerdefinierter Dateibezeichnung
function exportPDFWithPresetAndName(presetName, outputPath) {
    // Exportiere PDF in den Ordner "Web"
    exportPDFWithPresetAndNameAndFolders(presetName, outputPath, "Web");
}

// Funktion für den Export als PDF mit einem bestimmten Preset und benutzerdefinierter Dateibezeichnung
function exportPDFWithPresetAndNameForPrint(presetName, outputPath) {
    // Exportiere PDF in den Ordner "Print"
    exportPDFWithPresetAndNameAndFolders(presetName, outputPath, "Print");
}

// Funktion für den Export als JPG mit benutzerdefinierten Einstellungen und benutzerdefinierter Dateibezeichnung
function exportPagesAsJPG(outputPath) {
    var doc = app.activeDocument;

    // Erstelle den JPGs-Ordner, falls er nicht existiert
    var jpgFolder = new Folder(outputPath + "/JPGs");
    if (!jpgFolder.exists) {
        jpgFolder.create();
    }

    for (var p = 0; p < doc.pages.length; p++) {
        var myPageName = doc.pages[p].name;

        // Set JPEG export preferences
        var jpegExportSettings = {
            antiAlias: true,
            embedColorProfile: true,
            exportResolution: 72,
            jpegColorSpace: JpegColorSpaceEnum.rgb,
            jpegExportRange: ExportRangeOrAllPages.exportRange,
            jpegQuality: JPEGOptionsQuality.maximum,
            jpegRenderingStyle: JPEGOptionsFormat.baselineEncoding,
            useDocumentBleeds: false,
            simulateOverprint: false,
            pageString: myPageName,
        };

        app.jpegExportPreferences.properties = jpegExportSettings;

        // Exportiere JPGs in den Ordner "JPGs"
        doc.exportFile(ExportFormat.jpg, File(jpgFolder + "/" + "JPG_" + app.activeDocument.name.replace(/\.[^\.]+$/, "") + "_" + myPageName + ".jpg"), false);
    }
}

// Funktion zum Erstellen eines Dialogfensters für PDF-Presets und JPG-Einstellungen
function createExportDialog() {
    var pdfPresetDropdown1 = null;
    var pdfPresetDropdown2 = null;
    var outputPath = null;

    try {
        // Dialogfenster erstellen
        var dialog = app.dialogs.add({
            name: "Exporteinstellungen",
            canCancel: true
        });

        // Dialogspalte erstellen
        var column = dialog.dialogColumns.add();

        // Wenn es globale Exporteinstellungen gibt, verwende diese
        if (globalExportSettings) {
            pdfPresetDropdown1 = column.dropdowns.add({
                stringList: [globalExportSettings.pdfPreset1],
                selectedIndex: 0,
                minWidth: 200,
                maxWidth: 400
            });

            pdfPresetDropdown2 = column.dropdowns.add({
                stringList: [globalExportSettings.pdfPreset2],
                selectedIndex: 0,
                minWidth: 200,
                maxWidth: 400
            });

            outputPath = Folder(globalExportSettings.outputPath);
        } else {
            // Dropdown für erstes PDF-Preset erstellen
            pdfPresetDropdown1 = column.dropdowns.add({
                stringList: app.pdfExportPresets.everyItem().name,
                selectedIndex: 0,
                minWidth: 200,
                maxWidth: 400
            });

            // Dropdown für zweites PDF-Preset erstellen
            pdfPresetDropdown2 = column.dropdowns.add({
                stringList: app.pdfExportPresets.everyItem().name,
                selectedIndex: 0,
                minWidth: 200,
                maxWidth: 400
            });

            // Dialog anzeigen und Benutzereingaben sammeln
            if (dialog.show() == true) {
                outputPath = Folder.selectDialog("Wählen Sie das Ausgabeverzeichnis für die Dateien:");
            } else {
                return null;
            }
        }

        return {
            pdfPreset1: pdfPresetDropdown1.stringList[pdfPresetDropdown1.selectedIndex],
            pdfPreset2: pdfPresetDropdown2.stringList[pdfPresetDropdown2.selectedIndex],
            outputPath: outputPath ? outputPath.fsName : null
        };
    } catch (e) {
        // Fehlerbehandlung
        alert("Fehler beim Erstellen des Dialogfensters: " + e);
    } finally {
        // Dialog schließen, wenn er geöffnet wurde
        if (dialog && dialog instanceof Dialog) {
            dialog.destroy();
        }
    }
}

// Funktion für den Export als PDF mit einem bestimmten Preset und benutzerdefinierter Dateibezeichnung
function exportPDFWithPresetAndName(presetName, outputPath) {
    // Exportiere PDF in den Ordner "Web"
    exportPDFWithPresetAndNameAndFolders(presetName, outputPath, "Web");
}

// Funktion für den Export als PDF mit einem bestimmten Preset und benutzerdefinierter Dateibezeichnung
function exportPDFWithPresetAndNameForPrint(presetName, outputPath) {
    // Exportiere PDF in den Ordner "Print"
    exportPDFWithPresetAndNameAndFolders(presetName, outputPath, "Print");
}

// Funktion zum Exportieren für jedes geöffnete Dokument
function exportForAllDocuments() {
    var documents = app.documents;

    if (documents.length > 0) {
        var originalActiveDocument = app.activeDocument; // Das aktive Dokument speichern

        for (var d = 0; d < documents.length; d++) {
            var currentDocument = documents[0];

            // Benutzereingabe für PDF-Presets und JPG-Export (nur beim ersten Mal abfragen)
            var exportSettings = (d === 0) ? createExportDialog() : globalExportSettings;

            if (exportSettings) {
                try {
                    app.activeDocument = documents[1]; // Aktuelles Dokument setzen

                    // Setze die globalen Exporteinstellungen für jedes Dokument separat
                    globalExportSettings = {
                        pdfPreset1: exportSettings.pdfPreset1,
                        pdfPreset2: exportSettings.pdfPreset2,
                        outputPath: exportSettings.outputPath
                    };

                    // Exportiere erste PDF mit ausgewähltem Preset und Ausgabeverzeichnis
                    exportPDFWithPresetAndName(globalExportSettings.pdfPreset1, globalExportSettings.outputPath);

                    // Exportiere zweite PDF mit ausgewähltem Preset und Ausgabeverzeichnis für "Print"
                    exportPDFWithPresetAndNameForPrint(globalExportSettings.pdfPreset2, globalExportSettings.outputPath);

                    // Exportiere JPGs mit benutzerdefinierten Einstellungen und Ausgabeverzeichnis
                    exportPagesAsJPG(globalExportSettings.outputPath);
                } catch (e) {
                    alert("Fehler beim Exportieren des Dokuments: " + currentDocument.name + "\n" + e);
                }
            }
        }

        app.activeDocument = originalActiveDocument; // Das ursprüngliche aktive Dokument wiederherstellen

        alert("Export für alle geöffneten Dokumente abgeschlossen.");
    } else {
        alert("Es sind keine Dokumente geöffnet.");
    }
}

// Führe das Skript für alle geöffneten Dokumente aus
exportForAllDocuments();

 

Please help me. Thanks! 🙂

- Luca

TOPICS
Bug , Import and export , Scripting , Sync and storage

Views

132

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 23, 2024 Feb 23, 2024

Copy link to clipboard

Copied

That's rather a lot of code to review. . .

Maybe add this line somewhere at the beginning of the script:

app.pdfExportPreferences.pageRange = PageRange.ALL_PAGES;

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Feb 23, 2024 Feb 23, 2024

Copy link to clipboard

Copied

LATEST
function exportForAllDocuments() {
    var documents = app.documents;

 

better use app.documents.getElements() – by setting the active document in the loop you change the order within app.documents. Also pass the current document as argument rather than via active document. You should then be using  currentDocument = documents[d] in the loop, also instead of that documents[1].

 

Then move the dialog out of the loop rather than invoke it only for d==0 .

Hmm, this "globalExportSettings" copy of your dialog results "exportSettings" looks weird, without targetengine the variable is lost for the next run of the script, while with targetengine it would cause no choice but the previous values in the dialog (even if these preset names don't exist any more). If I get it right it is invariant in either case, so also does not belong into the loop.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines