Skip to main content
WIDSnonamer
Inspiring
January 30, 2023
Answered

Exporting Multiple Artboards Names

  • January 30, 2023
  • 2 replies
  • 1621 views

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)
}

 

This topic has been closed for replies.
Correct answer jduncan

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);
}

 

2 replies

jduncan
Community Expert
Community Expert
June 18, 2024

Hey @ Synder37927574fian, I just tested this on my Mac and the previews seem to be fine. See the attached screenshots. I do know I sometimes get incorrect preview results when simply saving PDF files from within Illustrator, but this export script seems to work fine "on my machine".

 

Original multi-artboard AI file:

 Resulting previews in Mac Finder after running the script:

jduncan
Community Expert
jduncanCommunity ExpertCorrect answer
Community Expert
January 30, 2023

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);
}

 

m1b
Community Expert
Community Expert
January 30, 2023

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

jduncan
Community Expert
Community Expert
January 30, 2023

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!