Skip to main content
Known Participant
March 5, 2025
Question

How to export selection as svg using extendscript?

  • March 5, 2025
  • 1 reply
  • 5286 views

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'.

 

 

1 reply

jduncan
Community Expert
Community Expert
March 5, 2025

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

psar12345Author
Known Participant
March 6, 2025

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

jduncan
Community Expert
Community Expert
March 7, 2025

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?