Copy link to clipboard
Copied
Hi,
I need to export the selection as an SVG using ExtendScript, just like the manual 'Export Selection' functionality.
var svgexportOptions = new ExportOptionsSVG();
svgexportOptions.embedRasterImages = true;
svgexportOptions.coordinatePrecision = 3;
It is working fine, but there is some empty space at the top compared to the manual 'Export Selection'.
Copy link to clipboard
Copied
If you are using the `app.activeDocument.exportFile()` method, it will export at the current document size. You could try fitting the current artboard to the selected artwork and then exporting (see below). FYI, this will get weird with multiple artboards, so you would need to account for that edge case (and probably a few more).
(function () {
// get the active document
try {
var doc = app.activeDocument;
} catch (e) {
alert("No active document.\n" + e);
return;
}
var fp = Folder.desktop + "/export.svg";
var ab = doc.artboards.getActiveArtboardIndex();
doc.fitArtboardToSelectedArt(ab);
var exportOptions = new ExportOptionsSVG();
exportOptions.embedRasterImages = true;
var type = ExportType.SVG;
var fileSpec = new File(fp);
doc.exportFile(fileSpec, type, exportOptions);
})();
Copy link to clipboard
Copied
Hi @jduncan
Thanks for your help. It is working fine but some styles is missing after export svg using extendscript like mix-blend-mode etc.,
I try this
svgexportOptions.cssProperties = SVGCSSPropertyLocation.STYLEELEMENTS;
But no luck. Please help
Copy link to clipboard
Copied
There are a handful of export options that would need to match you setting from the Export Selection dialog. Can you share a screenshot of your Asset Export dialog and also share a PDF of the base file you are exporting from?
Copy link to clipboard
Copied
Hi @jduncan,
what is the Asset Export dialog? - I selected the vector group item in the artwork and ran the script to export SVG. Also, I attached the PDF of the base file, the manual, and the automatically downloaded SVG file.
Copy link to clipboard
Copied
The dialog that pops up when you manually do File > Export Selection... (see attached).
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
So, after digging around a bit, I can get the blend mode to show up in the exported SVG xml by using setting the export option `optimizeForSVGViewer` to `true`. Unfortunately, the attribute comes as `adobe-blending-mode` instead of `mix-blend-mode` so it doesn't actually work. Also, the exported SVG xml isn't valid either. In line 3 (see below), Adobe include a few "&" characters which are invalid.
My best guess, is that when using File > Export Selection, Adobe uses a more moden SVG export method than the one that is used when calling the `exportFile` method from ExtendScript. If you inspect the two types of exported xml code you'll see that they are completely different.
Unfortunately, this may be a dead end for you...
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 29.3.0, SVG Export Plug-In . SVG Version: 9.03 Build 55982) -->
<svg version="1.1" id="Layer_1" xmlns:x="ns_extend;" xmlns:i="&ns_ai;" xmlns:graph="&ns_graphs;"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"
x="0px" y="0px" viewBox="0 0 157.84 184.76" style="enable-background:new 0 0 157.84 184.76;" xml:space="preserve">
<g style="enable-background:new ;">
<g id="Layer_1-2">
<circle style="fill:#ED1C24;stroke:#231F20;stroke-miterlimit:10;adobe-blending-mode:difference;enable-background:new ;" cx="56.7" cy="56.7" r="56.2"/>
<circle style="fill:#FFF200;stroke:#231F20;stroke-miterlimit:10;adobe-blending-mode:difference;enable-background:new ;" cx="86.4" cy="113.32" r="70.94"/>
</g>
</g>
</svg>
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi @jduncan,
below the mentioned line, the ab value is 0. In this scenario, it shows error.
var ab = doc.artboards.getActiveArtboardIndex();
doc.fitArtboardToSelectedArt(ab);
Please help.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi @jduncan,
I attached the pdf file here. Error is "an Illustrator error occurred: 1346458189 ('PARM')".
Copy link to clipboard
Copied
Hmm, that file seems to run fine for me with no errors. Have you edited the script any or does you script do other things then what we discussed above? I know that error can happen sometimes when switching between files and closing files but that is not technically happening in this script so I woudn't expect you to get that error... Let me know?
Copy link to clipboard
Copied
Hi @jduncan,
I export to svg based on the selected items in the artboard, not the entire artboard.
(function () {
try {
var selectedItems = app.activeDocument.selection;
if (selectedItems.length) {
for(var selIndex = 0;selIndex < selectedItems.length;selIndex++) {
var selItem = selectedItems[selIndex];
var doc = app.activeDocument;
var docColorSpace = (doc.documentColorSpace === DocumentColorSpace.RGB) ? DocumentColorSpace.RGB : DocumentColorSpace.CMYK;
var bounds = selItem.geometricBounds;
var left = bounds[0];
var top = bounds[1];
var right = bounds[2];
var bottom = bounds[3];
var width = Math.abs(right - left);
var height = Math.abs(top - bottom);
// Define maximum safe dimension (in points)
var MAX_DIMENSION = 16329;
width = Math.abs(Math.max(1, Math.min(MAX_DIMENSION, Math.round(width))));
height = Math.abs(Math.max(1, Math.min(MAX_DIMENSION, Math.round(height))));
var temDoc = app.documents.add(docColorSpace, width, height);
var nItem = selItem.duplicate(temDoc.layers[0], ElementPlacement.PLACEATEND);
var ab = doc.artboards.getActiveArtboardIndex();
doc.fitArtboardToSelectedArt(ab);
var fp = Folder.desktop + "/export.svg";
var fileSpec = File(fp);
var exportOptions = new ExportOptionsSVG();
exportOptions.embedRasterImages = true;
exportOptions.coordinatePrecision = 3;
exportOptions.documentEncoding = SVGDocumentEncoding.UTF8;
exportOptions.DTD = SVGDTDVersion.SVG1_0;
exportOptions.cssProperties = SVGCSSPropertyLocation.STYLEELEMENTS;
exportOptions.optimizeForSVGViewer = true;
var type = ExportType.SVG;
temDoc.exportFile(fileSpec, type, exportOptions);
temDoc.close(SaveOptions.DONOTSAVECHANGES);
}
}
} catch(e) {
alert(e.message+"("+e.line+")");
}
})();
Copy link to clipboard
Copied
Yes, this code is different from what was previously worked on. Anytime you adjust the code, please make sure to update it here so everyone can see what code you are actually running and can better diagnose any issues you are having.
So, in your updated code, you are:
So, do you want to either...
Your current code seems like you want to export every selected item individually but then you are adjusting the artboard of the original source doc. You also calculate the size for the temp doc so you wouldn't need to worry about the fitArtboardToSelection method.
I am just confused about what you want to happen. Can you supply a list (like mine above) of the steps you need to code to take along with the result you expect (1 file, multiple files, etc.)?