Skip to main content
Participating Frequently
December 11, 2021
Answered

How to use JavaScript to export to PNG/TIFF as multiple assets?

  • December 11, 2021
  • 1 reply
  • 2584 views

I have a lot of .ai files with logo and I need to export them into PNG files. Is there an antomatic way of doing it? I know you cannot record the export part in Action...

This topic has been closed for replies.
Correct answer Charu Rajput

like this?

https://drive.google.com/file/d/1mqULmWWj7SkhSs-eoA1Nc4VuzR4Vc1sW/view?usp=sharing


Hi,

You can use following snippet for your documents. When you run the script it will ask for the folder where source file exists. So select source_files folder in your case and png will be exported at the Desktop.

 

var _folder = Folder.selectDialog();
if (_folder != null) {
    var _files = _folder.getFiles('*.ai');
    for (var f = 0; f < _files.length; f++) {
        var doc = app.open(_files[f]);
        var docAssets = doc.assets;
        var assetIDArray = new Array();
        var itemToExport = new ExportForScreensItemToExport();
        itemToExport.artboards = '';
        itemToExport.document = false;
        var assetArray = new Array();
        var _layer = doc.layers[0];
        for (var i = 0; i < _layer.groupItems.length; i++) {
            var asset = docAssets.add(doc.groupItems[i]);
            assetIDArray.push(asset.assetID);
        }
        itemToExport.assets = assetIDArray;
        var options = new ExportForScreensOptionsPNG24();
        app.activeDocument.exportForScreens(File('~/Desktop'), ExportForScreensType.SE_PNG24, options, itemToExport);
    }
}

 

1 reply

Charu Rajput
Community Expert
Community Expert
December 11, 2021
XintongWAuthor
Participating Frequently
December 11, 2021

that is what I'm doing for the moment. The painful things is to open up hundreds of .ai files one by one, export them as multiple assets, and close them one by one. 

I think there is a way to speed it up by using scrips since I only need to open a file and export them to png, right?

Charu Rajput
Community Expert
Community Expert
December 11, 2021

Hi,

Yes this possible via script. I have simple script that exports all selected items of the active document as PNG, but this can be modified to run on all multiple documents.

My question, is do you want to export all items inside all documents or some specific? Coudl you share some ai sample files

 

Below is sample script

var doc = app.activeDocument;
var docAssets = doc.assets;
var assetIDArray = new Array();
var itemToExport = new ExportForScreensItemToExport();
itemToExport.artboards = '';
itemToExport.document = false;
var assetArray = new Array();
for (var i = 0; i < app.selection.length; i++) {
    var asset = docAssets.add(app.selection[i]);
    assetIDArray.push(asset.assetID);
}
itemToExport.assets = assetIDArray;
var options = new ExportForScreensOptionsPNG24();
app.activeDocument.exportForScreens(File('~/Desktop'), ExportForScreensType.SE_PNG24, options, itemToExport);

 

NOTE: This will export only selected items of the active document. if you answer above quetsion, we can help you in right direction.

Best regards