Skip to main content
Known Participant
Mar 5, 2025 4:36 PM UTC
Question

How to export selection as svg using extendscript?

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

 

 

18 replies

jduncan
Community Expert
Community Expert
Mar 5, 2025 6:47 PM UTC

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

Translate
psar12345Author
Known Participant
Mar 6, 2025 6:15 AM UTC

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

Translate
jduncan
Community Expert
Community Expert
Mar 7, 2025 3:36 AM UTC

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?

Translate