Skip to main content
Participant
November 21, 2022
Answered

ExportForScreens keeps automatically appending numbers to end of export's file name (javascript)

  • November 21, 2022
  • 2 replies
  • 1270 views

Hi all!

Starting file: filename-thumbnail.ai, this file has multiple artboards. Each is named with the numbers 01, 02, 03....10, 11, 12, etc, until the end.

 

I'm trying to create a script to export all the artboards as individual pngs with a specific filename format of filename-artboardname-thumbnail

 

I almost have it but for some reason each file name is showing up as "filename-01-thumbnail01", "filename-02-thumbnail02". I've tried using slice(0,-2) but that results in "filename-01-thumbna01". I think it's automatically adding the artboard name to the end, but I'm not sure why, and how to stop it from doing so.  

 

var doc = app.activeDocument;
var artboards = doc.artboards;

var fileFolder = doc.path;
//artboards.setActiveArtboardIndex(0);

var pngParam = new ExportForScreensOptionsPNG8();
pngParam.transparency = true;
pngParam.colorCount = 256;

var whatToExport;

//export each artboard individually
for (var i = 0; i < artboards.length; i++) {

    whatToExport = new ExportForScreensItemToExport();
    whatToExport.artboards = i + 1;
    whatToExport.document = false;
    doc.exportForScreens(new File(fileFolder + "/"), ExportForScreensType.SE_PNG8, pngParam, whatToExport, doc.name.replace("thumbnail.ai", artboards[i].name + "-thumbnail"));
}

 

This topic has been closed for replies.
Correct answer Mike Bro

Hi @Vivian_the_CakeEater,

I seem to be late to the party! but I'll share what I've found that works for me....and that's temporarily rernaming the artboards.

var doc = app.activeDocument;
var artboards = doc.artboards;

fileName = doc.name.replace(/\-thumbnail.ai/gi, "")


var fileFolder = doc.path;

for (var i = 0; i < artboards.length; i++) {
artboards.setActiveArtboardIndex(i);
var artboardName = artboards[i].name.toString();
artboards[i].name = fileName + "-" + artboardName + "-thumbnail"; 

var myFilePath = new File(fileFolder + "/")

var pngParam = new ExportForScreensOptionsPNG8();
pngParam.transparency = true;
pngParam.colorCount = 256;

var whatToExport;

//export each artboard individually
    whatToExport = new ExportForScreensItemToExport();
    whatToExport.artboards = i + 1;
    whatToExport.document = false;
    doc.exportForScreens(myFilePath, ExportForScreensType.SE_PNG8, pngParam, whatToExport);
    app.undo()
}

alert("Done Exporting the pnd Files!");

Regards,

Mike

2 replies

Mike BroCorrect answer
Legend
November 21, 2022

Hi @Vivian_the_CakeEater,

I seem to be late to the party! but I'll share what I've found that works for me....and that's temporarily rernaming the artboards.

var doc = app.activeDocument;
var artboards = doc.artboards;

fileName = doc.name.replace(/\-thumbnail.ai/gi, "")


var fileFolder = doc.path;

for (var i = 0; i < artboards.length; i++) {
artboards.setActiveArtboardIndex(i);
var artboardName = artboards[i].name.toString();
artboards[i].name = fileName + "-" + artboardName + "-thumbnail"; 

var myFilePath = new File(fileFolder + "/")

var pngParam = new ExportForScreensOptionsPNG8();
pngParam.transparency = true;
pngParam.colorCount = 256;

var whatToExport;

//export each artboard individually
    whatToExport = new ExportForScreensItemToExport();
    whatToExport.artboards = i + 1;
    whatToExport.document = false;
    doc.exportForScreens(myFilePath, ExportForScreensType.SE_PNG8, pngParam, whatToExport);
    app.undo()
}

alert("Done Exporting the pnd Files!");

Regards,

Mike

Participant
November 21, 2022

Hi Mike,

 

I'm actually teaching myself javascript and automating aspects of my work both as practice and to make my life easier. So I'm really interested in seeing different solutions! I ran your solution and it's pretty interesting. 

 

Can you explain what you were doing with this part? - 

var artboardName = artboards[i].name.toString();

I tried to google toString() but I'm confused at the explanations about what it does. It returns a string as a string?

 

I assume app.undo() is where you are undoing... the renaming. Were you able to tell why the artboard name was attaching to the filename in my original script?

m1b
Community Expert
Community Expert
November 21, 2022

@Vivian_the_CakeEater I am confused by that line also. The toString() call shouldn't be needed, as the "name" property of Artboard should be a string already. But maybe @Mike Bro has found a weird case?

m1b
Community Expert
Community Expert
November 21, 2022

Hi @Vivian_the_CakeEater, I've run into this problem, too. My solution is to predict the wrong filename and duplicate that file with the correct filename, then remove the wrong file. Actually there are two possible wrong namings—sometimes Illustrator will add the artboard index (as it did for you) and sometimes it will repeat the artboard name (as it did for me, with your exact same code!). The script handles either). Give it a try and let me know if it works for you.

- Mark

var doc = app.activeDocument;
var artboards = doc.artboards;

var fileFolder = doc.path;
//artboards.setActiveArtboardIndex(0);

var pngParam = new ExportForScreensOptionsPNG8();
pngParam.transparency = true;
pngParam.colorCount = 256;

var whatToExport;

//export each artboard individually
for (var i = 0; i < artboards.length; i++) {

    whatToExport = new ExportForScreensItemToExport();
    whatToExport.artboards = i + 1;
    whatToExport.document = false;

    var correctFileName = doc.name.replace("thumbnail.ai", artboards[i].name + "-thumbnail"),
        correctFilePath = doc.path.fsName + '/' + correctFileName,
        correctFile = File(correctFilePath);

    doc.exportForScreens(new File(fileFolder + "/"), ExportForScreensType.SE_PNG8, pngParam, whatToExport, correctFileName);

    var wrongFileName1 = correctFileName + '-' + ('00' + (i + 1)).slice(-2) + '.png',
        wrongFile1 = new File(doc.path.fsName + '/' + wrongFileName1);

    var wrongFileName2 = correctFileName + artboards[i].name + '.png',
        wrongFile2 = new File(doc.path.fsName + '/' + wrongFileName2);

    if (correctFile.exists)
        continue;

    if (wrongFile1.exists) {
        wrongFile1.copy(correctFilePath + '.png');
        wrongFile1.remove();
    }

    else if (wrongFile2.exists) {
        wrongFile2.copy(correctFilePath + '.png');
        wrongFile2.remove();
    }

}

 

Participant
November 21, 2022

It too me a moment to understand what you were doing - but I ran it and it works! Thanks so much!!