Copy link to clipboard
Copied
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"));
}
1 Correct answer
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";
v
...
Explore related tutorials & articles
Copy link to clipboard
Copied
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();
}
}
Copy link to clipboard
Copied
It too me a moment to understand what you were doing - but I ran it and it works! Thanks so much!!
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
@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?
Copy link to clipboard
Copied
The toString(); method is used to return a number as text, in this case it is not needed, I missed that when I copied the code from another script where I was referencing an index. Yes, the app.undo() is where I'm undoing the renaming and should be a safe method being used inside the loop.
RE: Were you able to tell why the artboard name was attaching to the filename in my original script?
Sorry, no...just like @m1b stated, I've too run into this problem and can't explain the weirdness with Illustrator.
Regards,
Mike
Copy link to clipboard
Copied
Hi Mike, so your trick is to make sure the full artboard name exactly matches the name of the file you are exporting, and if it does, Illustrator doesn't do any weirdness. Good spotting! I like this way better than mine! Thanks.
- Mark
Copy link to clipboard
Copied
Yes I really like both your solutions and love how they go at it from different angles. I'm learning a lot 🙂 Thank you to both of you!
Copy link to clipboard
Copied
The thing I would do differently in your script @Mike Bro is I wouldn't use app.undo(), and would just rename the artboard back to its original name. The reason is that if we used that code as part of a longer script that had done other actions, I feel that there is a chance that the app.undo() might undo more than we wanted it to. - Mark
Edit: also app.undo(); is hard to understand in the code vs, say, artboard.name = originalArtboardName;

