Copy link to clipboard
Copied
Hello,
I am using a script to export artboards from al multi-artboard file to single artboard files with the following code. Unfortunately I have issues with the way Illustrator names them.
I use "/.ai" as the Filename because I only need the Artboardnames as the filenames. But Illustrator alwas uses this naming method "filename_artboardname.ai" so my files always start with "_" e.g.: "_artboard1.ai".
Is there a way to prevent the "_" or maybe another way to save the Artboards?
Thanks in advance!
function exportFiles(){
var expOptions = new IllustratorSaveOptions();
var destFile = new File(doc.path + "/.ai")
expOptions.flattenOutput = OutputFlattening.PRESERVEAPPEARANCE;
expOptions.saveMultipleArtboards = true;
expOptions.artboardRange = "1";
doc.saveAs(destFile, expOptions);
expOptions.artboardRange = "2";
doc.saveAs(destFile, expOptions);
doc.close(SaveOptions.DONOTSAVECHANGES)
}
I recently ran across this same issue and couldn't determine a way to set the name correctly before saving/exporting so I instead just rename the files after the save/export. The code below will export the artboard(s) you specify in the `artboardsToExport` array (0-index), then rename the exported file(s) to just "Artboad X.ai". The `baseName` variable is just so I know how to exactly recreate the path for renaming.
doc = app.activeDocument;
exportFiles("TEST");
function exportFiles(baseName) {
...
Copy link to clipboard
Copied
I recently ran across this same issue and couldn't determine a way to set the name correctly before saving/exporting so I instead just rename the files after the save/export. The code below will export the artboard(s) you specify in the `artboardsToExport` array (0-index), then rename the exported file(s) to just "Artboad X.ai". The `baseName` variable is just so I know how to exactly recreate the path for renaming.
doc = app.activeDocument;
exportFiles("TEST");
function exportFiles(baseName) {
var expOptions = new IllustratorSaveOptions();
expOptions.flattenOutput = OutputFlattening.PRESERVEAPPEARANCE;
expOptions.saveMultipleArtboards = true;
var artboardsToExport = [1, 2];
var ab, tempFile, finalFile, testFile;
for (var i = 0; i < artboardsToExport.length; i++) {
ab = doc.artboards[artboardsToExport[i]];
// 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}`
tempFile = new File(doc.path + "/" + baseName);
// 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
finalFile = new File(doc.path + "/" + baseName + "_" + ab.name + ".ai");
// 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.
testFile = new File(doc.path + "/" + ab.name + ".ai");
if (testFile.exists) {
if (
!Window.confirm(
"File already exists!\nOverwrite " + ab.name + ".ai?",
"noAsDflt",
"File Already Exists"
)
) {
// If you choose not to overwrite I just remove the exported artboard
finalFile.remove();
continue;
} else {
// Otherwise, I remove the file at that path so the rename works
testFile.remove();
}
}
// Export the file
expOptions.artboardRange = artboardsToExport[i] + 1 + "";
doc.saveAs(tempFile, expOptions);
// Rename the file
finalFile.rename(ab.name + ".ai");
}
doc.close(SaveOptions.DONOTSAVECHANGES);
}
Copy link to clipboard
Copied
Hi @jduncan, this is what I've always done, too, but there's another way: see Mike Bro's answer. His 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. So you rename the artboard, export and then return the name to normal. - Mark
Copy link to clipboard
Copied
Hey @m1b, this sounds like a nice solution but I'm not sure I follow (sorry, I couldn't find "Mike's" answer). Does this still require renaming the file object or can it all happen without working with the file object host operating system methods? Thanks!
Copy link to clipboard
Copied
Oops! sorry forgot to add the link. Have edited and added it now. Look at Mike's code in the correct answer. - Mark
Copy link to clipboard
Copied
No worries! Now, unless I'm just completely missing something (which could definitely be the issue), Mark's solution won't work for this OP's case.
The OP was wanting the exported files to be named just the artboard name (e.g. Artboard 1.ai, Artboard 2.ai, etc.). Mark's solution is using the `exportForScreens` method which seems to actually just export the file with the artboard name as is (e.g. Artboard 1 -> Artboard 1.png). This would be perfect for the OP but he's exporting .ai files so `exportForScreens` won't work.
The issue (best I can tell) is that the `saveAs` method from the OP's post automatically prepends the file name plus an underscore to the artboard name when it is called.
So a file named "TextExport.ai" with two artboards named "Artboard 1" and "Artboard 2" would export two files named:
I've tried many different approaches and I can't find one that works for this case other than the file object rename method.
Please let me know if I'm missing something obvious. Thanks again!
Copy link to clipboard
Copied
Oh yes! Very sorry, I didn't read the post properly! OP can ignore all my comments here. 🫢 Still, I was quite excited to find Mike's workaround for exporting (not saving- as) and wanted to share. All the best. - Mark
Copy link to clipboard
Copied
@m1b, no worries! And, yes I agree, Mike's approach is way better for the export methods. Thanks for sharing!
Copy link to clipboard
Copied
That worked realy well, thank you very much!