Skip to main content
Disposition_Dev
Legend
January 31, 2018
解決済み

Export SVG of single artboard without appending text to filename?

  • January 31, 2018
  • 返信数 4.
  • 2237 ビュー

I'm trying to export each artboard of an AI file as an SVG without including all of the artwork that's not on the current artboard.

I'm able to use the following settings to achieve this, but then the file name gets text appended during the save, and then i end up with an incorrect filename after the export.

for example, if i'm exporting an artboard called "my-special-artboard", the resulting file is called "my-special-artboard_my-special-artboard.svg".

Here's the svg export functions.

function setSvgOptions(abIndex)

{

    var options = new ExportOptionsSVG();

    options.preserveEditability = false;

    options.saveMultipleArtboards = true;

    options.artboardRange = "" + (abIndex + 1);

    return options;

}

function saveSvg(doc, filePath, options, artboardIndex, artboardName)

{

    var destFile = new File(filePath + "/" + artboardName + ".svg");

    // var destFile = new File(filePath + "/" + ".svg");

    doc.exportFile(destFile, ExportType.SVG, options);

}

//and the function call. x represents the active artboard index.

svgOptions = setSvgOptions(x);

saveSvg(docRef, curSvgFolder, svgOptions, x, curAbName);

I can fix the file name problem by removing "saveMultipleArtboards" and "artboardRange" from the exportOptions, but then the resulting SVG contains all the artwork from the other artboards.

Any ideas??

このトピックへの返信は締め切られました。
解決に役立った回答 Loic.Aigon

Let Illustrator export with its own naming but by using a temp location we can target the exported file and copy it to a proper location and a proper naming :

function setSvgOptions(abIndex) 

    var options = new ExportOptionsSVG(); 

    options.preserveEditability = false; 

    options.saveMultipleArtboards = true; 

    options.artboardRange = "" + (abIndex + 1); 

    return options; 

 

 

function saveSvg(doc, filePath, options, artboardIndex, artboardName) 

//Create a temporary folder

var tmpFolder = Folder ( Folder.temp + "/"+artboardName );

tmpFolder.create();

//Create a reference to the file. Naming isn't really important as Illustrator will ignore naming

//as we asked for saveMultipleArtboards : true

    var tmpFile = new File(tmpFolder.fsName + "/" + artboardName + ".svg"); 

destFile = new File(filePath + "/" + artboardName + ".svg"); 

tmpFile.exists && tmpFile.remove();

//Exporting to temp file

    doc.exportFile(tmpFile, ExportType.SVG, options); 

//We know the file has been exported to tmpFolder

//But we may not be sure of the naming

//However there should be only one svg file there

//So we presume it's our recently exported svg

tmpFile = tmpFolder.getFiles("*.svg");

if ( !tmpFile.length ) {

alert("Couldn't export files sorry");

tmpFolder.remove();

}

//Now we copy temp file to final destination with correct naming

//And remove temp stuff

tmpFile = tmpFile[0];

tmpFile.copy ( destFile );

tmpFile.remove();

tmpFolder.remove();

HTH

Loic

Ozalto | Productivity Oriented - Loïc Aigon

返信数 4

Disposition_Dev
Legend
February 2, 2018

blast!

ok so apparently there's one last issue. When i save the SVG this way, the file name ends up correct, but the resulting file still contains empty layers for all of the other artboards that were stripped out during export.

For example, if there are 30 artboards (each artboard has artwork that is on it's own sublayer) in the document, and an SVG is exported per Loic's method above, when you open that SVG file back up in illustrator, it still contains 29 empty layers.

Are there any settings available to prevent that (i would have thought it would be 'preserveEditability = false' would do it...) or do i have to re-open the SVG's after the fact and remove the empty layers?

Or or or.. maybe I could just consolidate all the layers before running the export loop?

Thoughts on the most efficient way?

Loic.Aigon
Legend
February 2, 2018

Normally  options.preserveEditability = false; should do but you can still edit the svg itself.

function setSvgOptions(abIndex)   

{   

    var options = new ExportOptionsSVG();   

    options.preserveEditability = true;   

    options.saveMultipleArtboards = true;   

    options.artboardRange = "" + (abIndex + 1);   

    return options;   

}   

   

    saveSvg ( app.activeDocument, Folder.desktop,  setSvgOptions(0), 0, "bla" );

function saveSvg(doc, filePath, options, artboardIndex, artboardName)   

{   

 

//Create a temporary folder 

var tmpFolder = Folder ( Folder.temp + "/"+artboardName ); 

tmpFolder.create(); 

 

//Create a reference to the file. Naming isn't really important as Illustrator will ignore naming 

//as we asked for saveMultipleArtboards : true 

    var tmpFile = new File(tmpFolder.fsName + "/" + artboardName + ".svg");   

destFile = new File(filePath + "/" + artboardName + ".svg");   

tmpFile.exists && tmpFile.remove(); 

 

 

//Exporting to temp file 

    doc.exportFile(tmpFile, ExportType.SVG, options);   

 

//We know the file has been exported to tmpFolder 

//But we may not be sure of the naming 

//However there should be only one svg file there 

//So we presume it's our recently exported svg 

tmpFile = tmpFolder.getFiles("*.svg"); 

if ( !tmpFile.length ) { 

alert("Couldn't export files sorry"); 

tmpFolder.remove(); 

 

 

//Now we copy temp file to final destination with correct naming 

//And remove temp stuff 

tmpFile = tmpFile[0]; 

tmpFile.copy ( destFile ); 

tmpFile.remove(); 

tmpFolder.remove(); 

removeAIData ( destFile );

//remove native AI parts

function removeAIData( svgFile ){

svgFile.encoding = "UTF-8";

svgFile.open('r');

var xmlObj = XML(  svgFile.read() );

svgFile.close();

delete xmlObj.descendants ("i:pgf" )[0];

var dec = """<?xml version="1.0" encoding="iso-8859-1"?>

<!-- Generator: Adobe Illustrator 21.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [

<!ENTITY ns_extend "http://ns.adobe.com/Extensibility/1.0/">

<!ENTITY ns_ai "http://ns.adobe.com/AdobeIllustrator/10.0/">

<!ENTITY ns_graphs "http://ns.adobe.com/Graphs/1.0/">

<!ENTITY ns_vars "http://ns.adobe.com/Variables/1.0/">

<!ENTITY ns_imrep "http://ns.adobe.com/ImageReplacement/1.0/">

<!ENTITY ns_sfw "http://ns.adobe.com/SaveForWeb/1.0/">

<!ENTITY ns_custom "http://ns.adobe.com/GenericCustomNamespace/1.0/">

<!ENTITY ns_adobe_xpath "http://ns.adobe.com/XPath/1.0/">

]>""";

svgFile.encoding = "UTF-8";

svgFile.open('w');

svgFile.write ( dec+"\r"+xmlObj.toXMLString() )

svgFile.close();

}

Loic.Aigon
Legend
February 1, 2018

Glad it helped.

Silly-V
Legend
January 31, 2018

I always made new documents and pasted the artboards into them, old school.

Loic.Aigon
Loic.Aigon解決!
Legend
January 31, 2018

Let Illustrator export with its own naming but by using a temp location we can target the exported file and copy it to a proper location and a proper naming :

function setSvgOptions(abIndex) 

    var options = new ExportOptionsSVG(); 

    options.preserveEditability = false; 

    options.saveMultipleArtboards = true; 

    options.artboardRange = "" + (abIndex + 1); 

    return options; 

 

 

function saveSvg(doc, filePath, options, artboardIndex, artboardName) 

//Create a temporary folder

var tmpFolder = Folder ( Folder.temp + "/"+artboardName );

tmpFolder.create();

//Create a reference to the file. Naming isn't really important as Illustrator will ignore naming

//as we asked for saveMultipleArtboards : true

    var tmpFile = new File(tmpFolder.fsName + "/" + artboardName + ".svg"); 

destFile = new File(filePath + "/" + artboardName + ".svg"); 

tmpFile.exists && tmpFile.remove();

//Exporting to temp file

    doc.exportFile(tmpFile, ExportType.SVG, options); 

//We know the file has been exported to tmpFolder

//But we may not be sure of the naming

//However there should be only one svg file there

//So we presume it's our recently exported svg

tmpFile = tmpFolder.getFiles("*.svg");

if ( !tmpFile.length ) {

alert("Couldn't export files sorry");

tmpFolder.remove();

}

//Now we copy temp file to final destination with correct naming

//And remove temp stuff

tmpFile = tmpFile[0];

tmpFile.copy ( destFile );

tmpFile.remove();

tmpFolder.remove();

HTH

Loic

Ozalto | Productivity Oriented - Loïc Aigon

Disposition_Dev
Legend
February 1, 2018

Huzzah. Thanks Loic,

That was exactly what i needed. bonus points for your method not appreciably slowing down the execution, as the script was designed to batch many many files each with 30+ artboards. I shudder to think how the process would slow if i were creating new documents for each svg export.. Sorry, V. ;-P