AI Script ExportSVG only works in root folder, can't save SVGs in a new folder called /SVG/

Community Beginner ,
May 16, 2022 May 16, 2022

Copy link to clipboard

Copied

Hi Adobe fans, I have a big blocker with exportSVG. My Png exports work fine, but my export SVG opens the Save As prompt. It also converts my activeDocument to .svg from .ai. It also rebases the activeDocument inside the new /SVG/ folder. I have been very stuck. How can I stop this behavior? I have tried different ways but no success. Full repo: https://github.com/Artchibald/WTW-illustrator-script

 

// this works for PNGs
    function saveAsPNGAt512x512(layerName) {
      for (let j = 0; j < myIconsSublayers.length; j++) {
        let iconLayer = myIconsSublayers[j];
        iconLayer.visible = true;
        let pngFile = new File(
          `${sourceDoc.path}/${nameByDimensions}/512x512/${iconLayer.name}${layerName}.png`
        );
        let type = ExportType.PNG24;
        let opts = new ExportOptionsPNG24();
        ExportOptionsPNG24.antiAliasing = false;
        ExportOptionsPNG24.transparency = true;
        ExportOptionsPNG24.artBoardClipping = true;
        ExportOptionsPNG24.horizontalScale = 200;
        ExportOptionsPNG24.verticalScale = 200;
        sourceDoc.exportFile(pngFile, type, opts);
        iconLayer.visible = false;
      }
    } 

 

This export svg rebases the main document inside SVG folder and prompts a Save As. If I remove /SVG/ from the dynamic file URL, the SVGs will save in the root folder no problem.

 

    function saveAsSVG(layerName) { 
      for (let k = 0; k < myIconsSublayers.length; k++) {
        let iconLayer = myIconsSublayers[k];
        iconLayer.visible = true; 
        let svgFile = new File(          
          // Blocker: If I add /SVG/ after .path/ here it doesn't work! It uses save prompt, I dont want this
          `${sourceDoc.path}/SVG/${iconLayer.name}${layerName}.svg`
        );  
        let type = ExportType.SVG;
        sourceDoc.exportFile(svgFile, type);
        iconLayer.visible = false;
      } 
    }

 

Perhaps there is some API options I have missed! 

 

TOPICS
Bug , Scripting

Views

93

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 16, 2022 May 16, 2022

Copy link to clipboard

Copied

I fixed it by resetting the main doc

 

    function saveAsSVG(layerName) { 
      for (let k = 0; k < myIconsSublayers.length; k++) {
        let iconLayer = myIconsSublayers[k];
        iconLayer.visible = true; 
        let svgFile = new File(          
          // Blocker: If I add /SVG/ after .path/ here it doesn't work! It uses save prompt, I dont want this
          `${sourceDoc.path}/${nameByDimensions}/${nameSVG}/${iconLayer.name}${layerName}`
        );
        let aiFile = new File(          
          // Blocker: If I add /SVG/ after .path/ here it doesn't work! It uses save prompt, I dont want this
          `${sourceDoc.path}/${sourceDoc.name}`
        );
        let type = ExportType.SVG;
        ExportOptionsSVG.optimizeForSVGViewer = true;
        ExportOptionsSVG.saveMultipleArtboards = true;
        sourceDoc.exportFile(svgFile, type);
        iconLayer.visible = false;
        // redeclare what and where to original to avoid SVG export MEGA bug
        DocumentType.ILLUSTRATOR;
        sourceDoc.saveAs(aiFile);
      } 
    }

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 16, 2022 May 16, 2022

Copy link to clipboard

Copied

Hi @Archibald22224126t3xi, the problem may be that you are writing code in ES5+, but sadly Illustrator scripting API is ExtendScript which is like ES3 (Javascript from 1995?).

So some notes to fix:

- Replace "let" with "var"

- change your template literals to strings, eg instead of

`${sourceDoc.path}/SVG/${iconLayer.name}${layerName}.svg`

change to

sourceDoc.path + "/SVG/" + iconLayer.name + layerName + ".svg"

 and make use of ExtendScript's version of console.log:

$.writeln("sourceDoc.path = " + sourceDoc.path);
$.writeln("layerName = " + layerName);

That should get things working much better.

- Mark

 

P.S. any improvements to Javascript after ES3 probably won't exist and you'll need to make polyfills. eg. Array.filter()

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 17, 2022 May 17, 2022

Copy link to clipboard

Copied

Hi M1b, thank you, I solved the issue though, and I am using Node and Typescript, my Typescript gets compiled down to ECMA3 already in my tsconfig file. 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 29, 2022 May 29, 2022

Copy link to clipboard

Copied

LATEST

Oh I see. 🙂 Next time please mention that in your post, and might be best to post the transpiled sample code, not the typescript, to make sure it's not a transpiling issue. All the best.

- Mark

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines