Skip to main content
Participant
February 21, 2023
Answered

Batch export Ai to PNG of a specific size

  • February 21, 2023
  • 3 replies
  • 4244 views

I've had real difficulty finding a solution for this one.

I have over 300 files which I want convert to png files. This is for a vocabulary quiz program for students.

However, I need the files to be a specific width and for the aspect ratio to remain the same, so they load nicely.

 

The Actions option is no use because resizing is recorded as a percentage not a specific size, so files  have larger or smaller dimensions so it'll result in random sizes. Seems there is no other mode, am I wrong?

 

Placing all files in one file on multiple artboards would be just as slow as doing each picture myself, so that's pointless option.

 

Some websites do it but have not done it correctly. Either too blurry, wrong size or the background isn't transparent.

 

Tried Adobe media encoder, but I either have set a specific size or keep the source. They all have different aspect ratios and as I said I want a specific size, so this doesn't help.

 

I baffled why this still isn't a feature, especially as some online services come close despite obviously not having Adobe's deep pockets. Has anyone done something like this before? Is there a script I can import. I rarely do scripts so hopefully it's something straightforward.

Correct answer m1b

Hi @Mike28506974aaxn, give this script a try. You have to edit the script so that exportPath matches where you want the png files to go. The script will ask you to locate the folder of .ai files and will then process them. Note it will find all .ai files in that folder including sub-folders.

- Mark

 

/**
 * Export every .ai file in folder as .png 400px wide.
 * @author m1b
 * @discussion: https://community.adobe.com/t5/illustrator-discussions/batch-export-ai-to-png-of-a-specific-size/m-p/13594166
 */
(function () {

    // Path to the exported files
    var exportPath = '~/Desktop/PNG';

    var files = getFilesOfFolder({ filterRegex: /\.ai$/ }),
        counter = 0;

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

    for (var i = 0; i < files.length; i++) {
        var doc = app.open(files[i]),
            fileName = doc.name.replace(/\.[^\.]+/, ""),
            myFilePath = new File(exportPath + fileName);

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

        var itemToExport = new ExportForScreensItemToExport();
        itemToExport.artboards = '';
        itemToExport.document = true;
        doc.exportForScreens(myFilePath, ExportForScreensType.SE_PNG24, options, itemToExport);
        doc.close(SaveOptions.DONOTSAVECHANGES);

        counter++;
    }

    app.userInteractionLevel = previousInteractionLevel;

    if (counter > 0)
        alert('Exported ' + counter + ' png files.');



    /**
     * Collects all files inside a Folder,
     * recusively searching in sub-folders.
     * If no folder is supplied, will ask
     * user. Search can be filtered using
     * RegExp and also depth-limited.
     * @author m1b
     * @version 2022-09-24
     * @param {Object} options - parameters
     * @param {Folder} [options.folder] - the folder to look in.
     * @param {Function} [options.filter] - function, given a File, must return true.
     * @param {RegExp} [options.filterRegex] - file name's must match this.
     * @param {Boolean} [options.returnFirstMatch] - whether to return only the first found file (default: false).
     * @param {Number} [options.maxDepth] - deepest folder level (recursion depth limit) (default: 9999).
     * @private {Number} [options.depth] - the current depth.
     * @private {Array} [options.foundFiles] - array of files.
     * @returns {Array|File} - all the found files, or the first found file if `returnFirstMatch`.
     */
    function getFilesOfFolder(options) {

        // defaults and sanity
        if (options.depth == undefined)
            options.depth = 0;

        if (
            options.maxDepth == undefined
            || !options.maxDepth instanceof Number
        )
            options.maxDepth = 9999;

        if (options.foundFiles == undefined)
            options.foundFiles = [];

        if (
            options.folder != undefined
            && options.folder.constructor.name == 'String'
            && File(options.folder).exists
        )
            options.folder = File(options.folder);

        else if (
            options.folder != undefined
            && options.folder.constructor.name == 'String'
        )
            options.folder = undefined;

        if (options.folder == undefined)
            options.folder = Folder.selectDialog("Select a folder:");

        if (
            options.folder === null
            || !(options.folder instanceof Folder)
        )
            return options.foundFiles;

        // get files from this level
        var files = options.folder.getFiles(),
            notHiddenFile = /^[^\.\~]/;

        for (var i = 0; i < files.length; i++) {

            // folder
            if (
                files[i] instanceof Folder
                && options.depth < options.maxDepth
            )
                options.foundFiles = options.foundFiles.concat(getFilesOfFolder(
                    {
                        folder: files[i],
                        filter: options.filter,
                        filterRegex: options.filterRegex,
                        maxDepth: options.maxDepth,
                        returnFirstMatch: options.returnFirstMatch,
                        depth: options.depth + 1,
                        foundFiles: []
                    }
                ));

            // file
            else if (
                files[i] instanceof File
                && notHiddenFile.test(files[i].name)
                && (
                    options.filterRegex == undefined
                    || options.filterRegex.test(files[i].name)
                )
                && (
                    options.filter == undefined
                    || options.filter(files[i])
                )
            ) {

                // matched a file

                if (options.returnFirstMatch === true)
                    return files[i];

                else
                    options.foundFiles.push(files[i]);

            }

        }

        // this level done
        return options.foundFiles;

    };

})();

3 replies

BobbyG66
Participant
November 11, 2025

Thanks for a great script!

I rewrote lin #11 to convert both .eps and .ai files. Hope this helps others.

var files = getFilesOfFolder({ filterRegex: /\.(eps|ai)$/i }),

Kurt Gold
Community Expert
Community Expert
February 21, 2023

I'd recommend to use Mark's script, but just for the record it can also be done with a simple action, possibly in Batch mode. An action may include:

 

- Select all

- In the Transform palette, set the desired width and scale proportionally

- Export as .png

 

Participant
February 21, 2023

I thought so too, but I guess I do know how to use actions or something because this failed.

I followed a tutorial just to understand how iactions worked. But the scaling works for the drawing, but you can't scale the artboard with it. I scale the artboard too seperately and it didn't record that, I don't know why. So I tried just exporting selection, but it didn't record exporting selection, and I don't know why that is too.

I guess I need more study on this function.

Kurt Gold
Community Expert
Community Expert
February 21, 2023

If you want to change the artboard size according to the artwork size, you can record it by using the Insert Menu Item command in the Actions palette flyout menu and choosing the Object menu > Artboards > Fit to Artwork Bounds command (or Fit to Selected Art command).

 

m1b
Community Expert
Community Expert
February 21, 2023

Yes it's likely very possible with a script. Can you post a couple of example files (saved as pdf with Illustrator editing capabilities—this forum won't let us post .ai files) as well as a couple of ideal output .png files for those sample files?

- Mark

Participant
February 21, 2023

I wish to export the AI files as Png-24, transparent background, with a width of 400px with the aspect ration kept the same.

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
February 21, 2023

Hi @Mike28506974aaxn, give this script a try. You have to edit the script so that exportPath matches where you want the png files to go. The script will ask you to locate the folder of .ai files and will then process them. Note it will find all .ai files in that folder including sub-folders.

- Mark

 

/**
 * Export every .ai file in folder as .png 400px wide.
 * @author m1b
 * @discussion: https://community.adobe.com/t5/illustrator-discussions/batch-export-ai-to-png-of-a-specific-size/m-p/13594166
 */
(function () {

    // Path to the exported files
    var exportPath = '~/Desktop/PNG';

    var files = getFilesOfFolder({ filterRegex: /\.ai$/ }),
        counter = 0;

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

    for (var i = 0; i < files.length; i++) {
        var doc = app.open(files[i]),
            fileName = doc.name.replace(/\.[^\.]+/, ""),
            myFilePath = new File(exportPath + fileName);

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

        var itemToExport = new ExportForScreensItemToExport();
        itemToExport.artboards = '';
        itemToExport.document = true;
        doc.exportForScreens(myFilePath, ExportForScreensType.SE_PNG24, options, itemToExport);
        doc.close(SaveOptions.DONOTSAVECHANGES);

        counter++;
    }

    app.userInteractionLevel = previousInteractionLevel;

    if (counter > 0)
        alert('Exported ' + counter + ' png files.');



    /**
     * Collects all files inside a Folder,
     * recusively searching in sub-folders.
     * If no folder is supplied, will ask
     * user. Search can be filtered using
     * RegExp and also depth-limited.
     * @author m1b
     * @version 2022-09-24
     * @param {Object} options - parameters
     * @param {Folder} [options.folder] - the folder to look in.
     * @param {Function} [options.filter] - function, given a File, must return true.
     * @param {RegExp} [options.filterRegex] - file name's must match this.
     * @param {Boolean} [options.returnFirstMatch] - whether to return only the first found file (default: false).
     * @param {Number} [options.maxDepth] - deepest folder level (recursion depth limit) (default: 9999).
     * @private {Number} [options.depth] - the current depth.
     * @private {Array} [options.foundFiles] - array of files.
     * @returns {Array|File} - all the found files, or the first found file if `returnFirstMatch`.
     */
    function getFilesOfFolder(options) {

        // defaults and sanity
        if (options.depth == undefined)
            options.depth = 0;

        if (
            options.maxDepth == undefined
            || !options.maxDepth instanceof Number
        )
            options.maxDepth = 9999;

        if (options.foundFiles == undefined)
            options.foundFiles = [];

        if (
            options.folder != undefined
            && options.folder.constructor.name == 'String'
            && File(options.folder).exists
        )
            options.folder = File(options.folder);

        else if (
            options.folder != undefined
            && options.folder.constructor.name == 'String'
        )
            options.folder = undefined;

        if (options.folder == undefined)
            options.folder = Folder.selectDialog("Select a folder:");

        if (
            options.folder === null
            || !(options.folder instanceof Folder)
        )
            return options.foundFiles;

        // get files from this level
        var files = options.folder.getFiles(),
            notHiddenFile = /^[^\.\~]/;

        for (var i = 0; i < files.length; i++) {

            // folder
            if (
                files[i] instanceof Folder
                && options.depth < options.maxDepth
            )
                options.foundFiles = options.foundFiles.concat(getFilesOfFolder(
                    {
                        folder: files[i],
                        filter: options.filter,
                        filterRegex: options.filterRegex,
                        maxDepth: options.maxDepth,
                        returnFirstMatch: options.returnFirstMatch,
                        depth: options.depth + 1,
                        foundFiles: []
                    }
                ));

            // file
            else if (
                files[i] instanceof File
                && notHiddenFile.test(files[i].name)
                && (
                    options.filterRegex == undefined
                    || options.filterRegex.test(files[i].name)
                )
                && (
                    options.filter == undefined
                    || options.filter(files[i])
                )
            ) {

                // matched a file

                if (options.returnFirstMatch === true)
                    return files[i];

                else
                    options.foundFiles.push(files[i]);

            }

        }

        // this level done
        return options.foundFiles;

    };

})();