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

Script for Export Multiple Artboard to JPG, PNG...etc

Explorer ,
Mar 03, 2023 Mar 03, 2023

Copy link to clipboard

Copied

Hi, 

I tried to look for script to export multiple artboards to PNG, JPG, SVG and PDF: starting with JPEG and not sure where is the issues? 

 

// artboards to files

var document = app.activeDocument;
var boards = document.artboards;

for(var i = 0; i < boards.length; i++) {
document.artboards.setActiveArtboardIndex(i);
var exportOptions = new ExportOptionsJPEG();
var aiDoc = new File("~/Desktop/test-export/artboard"+(i+1).toString());
exportOptions.exportMultipleArtboards = true;
exportOptions.artboardRange = (i+1).toString();
document.exportFile(ExportType.JPEG, exportOptions);
}

thanks

TOPICS
Scripting

Views

871

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

correct answers 2 Correct answers

Community Expert , Mar 04, 2023 Mar 04, 2023

Hi @iampurple, I've written a script that shows one way to export each artboard in various formats, using Export For Screens. I hope that helps your understanding. Let me know how it goes.

- Mark

 

/**
 * Export For Screens in various formats.
 * @author m1b
 * @discussion https://community.adobe.com/t5/illustrator-discussions/script-for-export-multiple-artboard-to-jpg-png-etc/m-p/13624982
 */
(function () {

    var exportPathJPG = '~/Desktop/EXPORTS/JPG/',
        exportPathPNG = '~/Desktop/EX
...

Votes

Translate

Translate
Explorer , Mar 07, 2023 Mar 07, 2023

Thanks @m1b !!

 

I combined the script for Export Screen (JPG, PNG & SVG (removed PDF) ) and Save As Option (added AI format), there is wrong on here, but not sure why. Would you please help to take a look where is

// Check if a document is open
if (app.documents.length == 0) {
alert("No open document.");
return;
}? 

/**
 * Export For Screens in various formats.
 * @author m1b
 * @discussion https://community.adobe.com/t5/illustrator-discussions/script-for-export-multiple-artboard-to-jpg-png-etc/m-p/136
...

Votes

Translate

Translate
Community Expert ,
Mar 04, 2023 Mar 04, 2023

Copy link to clipboard

Copied

Hi @iampurple, I've written a script that shows one way to export each artboard in various formats, using Export For Screens. I hope that helps your understanding. Let me know how it goes.

- Mark

 

/**
 * Export For Screens in various formats.
 * @author m1b
 * @discussion https://community.adobe.com/t5/illustrator-discussions/script-for-export-multiple-artboard-to-jpg-png-etc/m-p/13624982
 */
(function () {

    var exportPathJPG = '~/Desktop/EXPORTS/JPG/',
        exportPathPNG = '~/Desktop/EXPORTS/PNG/',
        exportPathSVG = '~/Desktop/EXPORTS/SVG/',
        exportPathPDF = '~/Desktop/EXPORTS/PDF/';

    // Check if a document is open
    if (app.documents.length == 0) {
        alert("No open document.");
        return;
    }

    var previousInteractionLevel = app.userInteractionLevel;
    app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

    var doc = app.activeDocument;

    // do the exporting
    exportJPG(doc, File(exportPathJPG));
    exportPNG(doc, File(exportPathPNG));
    exportPDF(doc, File(exportPathPDF));
    exportSVG(doc, File(exportPathSVG));

    // clean up
    app.userInteractionLevel = previousInteractionLevel;

    // display a confirmation message
    alert("Export complete.");

    /**
     * Export all artboards as PNG.
     * @param {Document} doc - an Illustrator Document.
     * @param {Folder} [exportFolder] - the folder to export to (default: document's folder).
     */
    function exportPNG(doc, exportFolder) {

        var options = new ExportForScreensOptionsPNG24();
        options.antiAliasing = AntiAliasingMethod.ARTOPTIMIZED;
        options.scaleType = ExportForScreensScaleType.SCALEBYFACTOR;
        options.scaleTypeValue = 1;
        options.transparency = true;

        var itemToExport = new ExportForScreensItemToExport();
        itemToExport.artboards = 'all';
        itemToExport.document = false;
        exportFolder = exportFolder || doc.path;
        doc.exportForScreens(exportFolder, ExportForScreensType.SE_PNG24, options, itemToExport);

    };

    /**
     * Export all artboards as JPG.
     * @param {Document} doc - an Illustrator Document.
     * @param {Folder} [exportFolder] - the folder to export to (default: document's folder).
     */
    function exportJPG(doc, exportFolder) {

        var options = new ExportForScreensOptionsJPEG();
        options.antiAliasing = AntiAliasingMethod.ARTOPTIMIZED;
        options.compressionMethod = JPEGCompressionMethodType.BASELINESTANDARD;
        options.embedICCProfile = false;
        options.scaleType = ExportForScreensScaleType.SCALEBYFACTOR;
        options.scaleTypeValue = 1;

        var itemToExport = new ExportForScreensItemToExport();
        itemToExport.artboards = 'all';
        itemToExport.document = false;
        exportFolder = exportFolder || doc.path;
        doc.exportForScreens(exportFolder, ExportForScreensType.SE_JPEG100, options, itemToExport);

    };

    /**
     * Export all artboards as PDF.
     * @param {Document} doc - an Illustrator Document.
     * @param {Folder} [exportFolder] - the folder to export to (default: document's folder).
     */
    function exportPDF(doc, exportFolder) {

        var options = new ExportForScreensPDFOptions();
        options.pdfPreset = '[Press Quality]';

        var itemToExport = new ExportForScreensItemToExport();
        itemToExport.artboards = 'all';
        itemToExport.document = false;
        doc.exportForScreens(exportFolder, ExportForScreensType.SE_PDF, options, itemToExport);

    };

    /**
     * Export all artboards as SVG.
     * @param {Document} doc - an Illustrator Document.
     * @param {Folder} [exportFolder] - the folder to export to (default: document's folder).
     */
    function exportSVG(doc, exportFolder) {

        var options = new ExportForScreensOptionsWebOptimizedSVG();
        options.coordinatePrecision = 3;
        options.cssProperties = SVGCSSPropertyLocation.ENTITIES;
        options.fontType = SVGFontType.OUTLINEFONT;
        options.rasterImageLocation = RasterImageLocation.EMBED;
        options.svgId = SVGIdType.SVGIDREGULAR;
        options.svgMinify = false;
        options.svgResponsive = true;

        var itemToExport = new ExportForScreensItemToExport();
        itemToExport.artboards = 'all';
        itemToExport.document = false;
        doc.exportForScreens(exportFolder, ExportForScreensType.SE_SVG, options, itemToExport);

    };

})();

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
Explorer ,
Mar 06, 2023 Mar 06, 2023

Copy link to clipboard

Copied

Hi @m1b
Thank you so much!! It's perfect.

 

I have another question, how do you make the artboard name when save as EPS, AI and PDF? Example, I name the artboard, "artwork-1", but when scripting and it automatic added the artboard1_artwork-1?

 

var document = app.activeDocument;
var boards = document.artboards;

// loop through all artboards, make each one active and render before moving onto the next
for(var i = 0; i < boards.length; i++) {
document.artboards.setActiveArtboardIndex(i);
var saveOptions = new IllustratorSaveOptions();
var aiDoc = new File("~/Desktop/test-export/artboard"+(i+1).toString());
saveOptions.saveMultipleArtboards = true;
saveOptions.artboardRange = (i+1).toString();
document.saveAs(aiDoc, saveOptions);
}

for(var i = 0; i < boards.length; i++) {
document.artboards.setActiveArtboardIndex(i);
var saveOptions = new EPSSaveOptions();
var aiDoc = new File("~/Desktop/test-export/artboard"+(i+1).toString());
saveOptions.saveMultipleArtboards = true;
saveOptions.artboardRange = (i+1).toString();
document.saveAs(aiDoc, saveOptions);
}

for(var i = 0; i < boards.length; i++) {
document.artboards.setActiveArtboardIndex(i);
var saveOptions = new PDFSaveOptions();
var aiDoc = new File("~/Desktop/test-export/artboard"+(i+1).toString());
saveOptions.saveMultipleArtboards = true;
saveOptions.artboardRange = (i+1).toString();
document.saveAs(aiDoc, saveOptions);
}

 

Thanks

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
Community Expert ,
Mar 06, 2023 Mar 06, 2023

Copy link to clipboard

Copied

Hi @iampurple, using saveAs is different to ExportForScreens, as you know. You are basically correct with how to do it, and the problem you notice is that it doesn't save the file(s) with the correct artboard names.

Here is a modified version of your script that I hope fixes the issue. I am using @jduncan's technique from this post, which I have incorporated into a function saveActiveArtboardAs. See what you think.

- Mark

 

/**
 * Exports all artboards of active document
 * as .ai, .eps and .pdf.
 */
(function () {

    var exportPath = '~/Desktop/test-export/';

    var doc = app.activeDocument;
    var boards = doc.artboards;
    var allSaveOptions = [
        getIllustratorSaveOptions(),
        getEPSSaveOptions(),
        getPDFSaveOptions(),
    ];

    var previousInteractionLevel = app.userInteractionLevel;
    app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

    // loop through all artboards, make each one active and render before moving onto the next
    for (var i = 0; i < boards.length; i++) {
        doc.artboards.setActiveArtboardIndex(i);

        for (var j = 0; j < allSaveOptions.length; j++) {
            var saveOptions = allSaveOptions[j];
            saveOptions.artboardRange = i + 1;
            saveActiveArtboardAs(doc, exportPath, boards[i].name, saveOptions);
        }
    }

    app.userInteractionLevel = previousInteractionLevel;

})();

/**
 * Performs saveAs for the active artboard.
 * Using renaming technique by jduncan.
 * @author jduncan and m1b
 * @version 2023-03-07
 * @param {Document} doc - an Illustrator Document.
 * @param {String} exportPath - the export folder path.
 * @param {String} fileName - the desired exported filename without file extension.
 * @param {IllustratorSaveOptions|EPSSaveOptions|PDFSaveOptions} saveOptions - the saveOptions for Document.saveAs().
 */
function saveActiveArtboardAs(doc, exportPath, fileName, saveOptions) {

    // The `baseName` variable is just so I know how to exactly recreate the path for renaming.
    var baseName = 'EXPORTING';

    var fileExtension, exportedFileName;

    // When exporting specific artboards, the api only allows specifying the first part of
    // the file name, then it appends `_Artboard Name` and add the extension which result in:
    //`{baseName}_Artboard X.{extension}`
    var tempFile = new File(exportPath + "/" + baseName);

    if (saveOptions.typename == 'IllustratorSaveOptions') {
        fileExtension = '.ai';
        exportedFileName = baseName + "_" + fileName + fileExtension;
    }
    else if (saveOptions.typename == 'EPSSaveOptions') {
        fileExtension = '.eps';
        exportedFileName = baseName + "_" + fileName + fileExtension;
    }
    else if (saveOptions.typename == 'PDFSaveOptions') {
        fileExtension = '.pdf';
        exportedFileName = baseName + fileExtension;
    }

    // To be able to correctly name the exported file(s), I am recreating the known
    // naming convention with a new `file` object to that path
    var actualExportedFile = new File(exportPath + "/" + exportedFileName);

    // Since the export exported file path and the preferred file path are different the built-in
    // file overwrite protection will not prompt you so and the `rename` method would not
    // overwrite the existing file. So, here I do the checking and prompt if needed.
    var testFile = new File(exportPath + "/" + fileName + fileExtension);
    if (testFile.exists) {
        if (
            !confirm(
                "File already exists!\nOverwrite " + fileName + fileExtension + "?",
                "noAsDflt",
                "File Already Exists"
            )
        ) {
            // If you choose not to overwrite I just remove the exported artboard
            actualExportedFile.remove();
            return;
        } else {
            // Otherwise, I remove the file at that path so the rename works
            testFile.remove();
        }
    }

    // do the save as
    doc.saveAs(tempFile, saveOptions);

    // set this to the correct name
    if (actualExportedFile.exists)
        actualExportedFile.rename(fileName + fileExtension);

};

/**
 * Example IllustratorSaveOptions
 * @returns {IllustratorSaveOptions}
 */
function getIllustratorSaveOptions() {
    var saveOptions = new IllustratorSaveOptions();
    saveOptions.compatibility = Compatibility.ILLUSTRATOR24;
    saveOptions.compressed = true;
    saveOptions.embedICCProfile = true;
    saveOptions.embedLinkedFiles = false;
    saveOptions.flattenOutput = OutputFlattening.PRESERVEAPPEARANCE;
    saveOptions.fontSubsetThreshold = 100;
    saveOptions.pdfCompatible = true;
    saveOptions.saveMultipleArtboards = true;
    return saveOptions;
};

/**
 * Example EPSSaveOptions
 * @returns {EPSSaveOptions}
 */
function getEPSSaveOptions() {
    var saveOptions = new EPSSaveOptions();
    saveOptions.cmykPostScript = true;
    saveOptions.compatibility = Compatibility.ILLUSTRATOR24;
    saveOptions.compatibleGradientPrinting = false;
    saveOptions.embedAllFonts = true;
    saveOptions.embedLinkedFiles = true;
    saveOptions.flattenOutput = OutputFlattening.PRESERVEAPPEARANCE;
    saveOptions.includeDocumentThumbnails = true;
    saveOptions.overprint = PDFOverprint.PRESERVEPDFOVERPRINT;
    saveOptions.postScript = EPSPostScriptLevelEnum.LEVEL2;
    saveOptions.preview = EPSPreview.COLORMACINTOSH;
    saveOptions.saveMultipleArtboards = true;
    return saveOptions;
};

/**
 * Example PDFSaveOptions
 * @returns {PDFSaveOptions}
 */
function getPDFSaveOptions() {
    var saveOptions = new PDFSaveOptions();
    saveOptions.pDFPreset = '[Press Quality]';
    saveOptions.saveMultipleArtboards = true;
    saveOptions.viewAfterSaving = false;
    return saveOptions;
};

 

P.S. when you post code to the forum here, please use the </> button and paste it into a code block. It helps because it formats the script much better. 🙂

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
Explorer ,
Mar 07, 2023 Mar 07, 2023

Copy link to clipboard

Copied

Hi @m1b 

Thank you a lot, appreciate it! Also thanks for @jduncan's posted.

Thank you for the tip on posting code.

 

Do you know where to learn JS for adobe? I want to learn more.

 

Thanksss

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
Community Expert ,
Mar 07, 2023 Mar 07, 2023

Copy link to clipboard

Copied

Hi @iampurple, I'm sure where is best to learn ExtendScript. ExtendScript is Javascript ES3 (Javascript from 1999) with a few differences. If you cannot find tutorials for ExtendScript, you can do any Javascript tutorial, but look for ES3 tutorials, otherwise you will have to adjust for features of the newer Javascript that isn't in ExtendScript.

 

But in general, just learn any Javascript course suitable to your programming level, and then search for people's existing scripts to examine and experiment with. And ask specific questions here.

- Mark

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
Explorer ,
Mar 07, 2023 Mar 07, 2023

Copy link to clipboard

Copied

Thanks @m1b !!

 

I combined the script for Export Screen (JPG, PNG & SVG (removed PDF) ) and Save As Option (added AI format), there is wrong on here, but not sure why. Would you please help to take a look where is

// Check if a document is open
if (app.documents.length == 0) {
alert("No open document.");
return;
}? 

/**
 * Export For Screens in various formats.
 * @author m1b
 * @discussion https://community.adobe.com/t5/illustrator-discussions/script-for-export-multiple-artboard-to-jpg-png-etc/m-p/13624982
 */
(function () {

    var exportPathJPG = '~/Desktop/test-export/JPG/',
        exportPathPNG = '~/Desktop/test-export/PNG/',
        exportPathSVG = '~/Desktop/test-export/SVG/',

    // Check if a document is open
    if (app.documents.length == 0) {
        alert("No open document.");
        return;
    }

    var previousInteractionLevel = app.userInteractionLevel;
    app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

    var doc = app.activeDocument;

    // do the exporting
    exportJPG(doc, File(exportPathJPG));
    exportPNG(doc, File(exportPathPNG));
    exportSVG(doc, File(exportPathSVG));

    // clean up
    app.userInteractionLevel = previousInteractionLevel;

    // display a confirmation message
    alert("Export complete.");

    /**
     * Export all artboards as PNG.
     * @param {Document} doc - an Illustrator Document.
     * @param {Folder} [exportFolder] - the folder to export to (default: document's folder).
     */
    function exportPNG(doc, exportFolder) {

        var options = new ExportForScreensOptionsPNG24();
        options.antiAliasing = AntiAliasingMethod.ARTOPTIMIZED;
        options.scaleType = ExportForScreensScaleType.SCALEBYFACTOR;
        options.scaleTypeValue = 1;
        options.transparency = true;

        var itemToExport = new ExportForScreensItemToExport();
        itemToExport.artboards = 'all';
        itemToExport.document = false;
        exportFolder = exportFolder || doc.path;
        doc.exportForScreens(exportFolder, ExportForScreensType.SE_PNG24, options, itemToExport);

    };

    /**
     * Export all artboards as JPG.
     * @param {Document} doc - an Illustrator Document.
     * @param {Folder} [exportFolder] - the folder to export to (default: document's folder).
     */
    function exportJPG(doc, exportFolder) {

        var options = new ExportForScreensOptionsJPEG();
        options.antiAliasing = AntiAliasingMethod.ARTOPTIMIZED;
        options.compressionMethod = JPEGCompressionMethodType.BASELINESTANDARD;
        options.embedICCProfile = false;
        options.scaleType = ExportForScreensScaleType.SCALEBYFACTOR;
        options.scaleTypeValue = 1;

        var itemToExport = new ExportForScreensItemToExport();
        itemToExport.artboards = 'all';
        itemToExport.document = false;
        exportFolder = exportFolder || doc.path;
        doc.exportForScreens(exportFolder, ExportForScreensType.SE_JPEG100, options, itemToExport);

    };

    /**
     * Export all artboards as SVG.
     * @param {Document} doc - an Illustrator Document.
     * @param {Folder} [exportFolder] - the folder to export to (default: document's folder).
     */
    function exportSVG(doc, exportFolder) {

        var options = new ExportForScreensOptionsWebOptimizedSVG();
        options.coordinatePrecision = 3;
        options.cssProperties = SVGCSSPropertyLocation.ENTITIES;
        options.fontType = SVGFontType.OUTLINEFONT;
        options.rasterImageLocation = RasterImageLocation.EMBED;
        options.svgId = SVGIdType.SVGIDREGULAR;
        options.svgMinify = false;
        options.svgResponsive = true;

        var itemToExport = new ExportForScreensItemToExport();
        itemToExport.artboards = 'all';
        itemToExport.document = false;
        doc.exportForScreens(exportFolder, ExportForScreensType.SE_SVG, options, itemToExport);

    };


/**
*Save as AI file
*/

(function () {

    var exportPath = '~/Desktop/test-export/';

    var doc = app.activeDocument;
    var boards = doc.artboards;
    var allSaveOptions = [
        getIllustratorSaveOptions(),
    ];

    var previousInteractionLevel = app.userInteractionLevel;
    app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

    // loop through all artboards, make each one active and render before moving onto the next
    for (var i = 0; i < boards.length; i++) {
        doc.artboards.setActiveArtboardIndex(i);

        for (var j = 0; j < allSaveOptions.length; j++) {
            var saveOptions = allSaveOptions[j];
            saveOptions.artboardRange = i + 1;
            saveActiveArtboardAs(doc, exportPath, boards[i].name, saveOptions);
        }
    }

    app.userInteractionLevel = previousInteractionLevel;

})();

/**
 * Performs saveAs for the active artboard.
 * Using renaming technique by jduncan.
 * @author jduncan and m1b
 * @version 2023-03-07
 * @param {Document} doc - an Illustrator Document.
 * @param {String} exportPath - the export folder path.
 * @param {String} fileName - the desired exported filename without file extension.
 * @param {IllustratorSaveOptions|EPSSaveOptions|PDFSaveOptions} saveOptions - the saveOptions for Document.saveAs().
 */
function saveActiveArtboardAs(doc, exportPath, fileName, saveOptions) {

    // The `baseName` variable is just so I know how to exactly recreate the path for renaming.
    var baseName = 'EXPORTING';

    var fileExtension, exportedFileName;

    // When exporting specific artboards, the api only allows specifying the first part of
    // the file name, then it appends `_Artboard Name` and add the extension which result in:
    //`{baseName}_Artboard X.{extension}`
    var tempFile = new File(exportPath + "/" + baseName);

    if (saveOptions.typename == 'IllustratorSaveOptions') {
        fileExtension = '.ai';
        exportedFileName = baseName + "_" + fileName + fileExtension;
    }

// To be able to correctly name the exported file(s), I am recreating the known
    // naming convention with a new `file` object to that path
    var actualExportedFile = new File(exportPath + "/" + exportedFileName);

    // Since the export exported file path and the preferred file path are different the built-in
    // file overwrite protection will not prompt you so and the `rename` method would not
    // overwrite the existing file. So, here I do the checking and prompt if needed.
    var testFile = new File(exportPath + "/" + fileName + fileExtension);
    if (testFile.exists) {
        if (
            !confirm(
                "File already exists!\nOverwrite " + fileName + fileExtension + "?",
                "noAsDflt",
                "File Already Exists"
            )
        ) {
            // If you choose not to overwrite I just remove the exported artboard
            actualExportedFile.remove();
            return;
        } else {
            // Otherwise, I remove the file at that path so the rename works
            testFile.remove();
        }
    }

    // do the save as
    doc.saveAs(tempFile, saveOptions);

    // set this to the correct name
    if (actualExportedFile.exists)
        actualExportedFile.rename(fileName + fileExtension);

};

/**
 * Example IllustratorSaveOptions
 * @returns {IllustratorSaveOptions}
 */
function getIllustratorSaveOptions() {
    var saveOptions = new IllustratorSaveOptions();
    saveOptions.compatibility = Compatibility.ILLUSTRATOR24;
    saveOptions.compressed = true;
    saveOptions.embedICCProfile = true;
    saveOptions.embedLinkedFiles = false;
    saveOptions.flattenOutput = OutputFlattening.PRESERVEAPPEARANCE;
    saveOptions.fontSubsetThreshold = 100;
    saveOptions.pdfCompatible = true;
    saveOptions.saveMultipleArtboards = true;
    return saveOptions;
};

})();

 

 thanks a lot.

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
Explorer ,
Mar 08, 2023 Mar 08, 2023

Copy link to clipboard

Copied

LATEST

Thanks @m1b, I figured out.

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