18 Replies Latest reply on Jul 3, 2017 10:47 AM by jajohns8

    export as WOSVG produces file with null content on first run

    jajohns8

      I need to export multiple files as WOSVG (web optimized SVG).

       

      This script, when run after opening a new instance of illustrator, produces an SVG file of 0 kb size the first time when run. (Empty, invalid file.)

       

      If I leave the instance of Illustrator open, and run the same script again, it works fine. The file looks fine.

       

      I altered the script to export as standard SVG (changing the export type and options) and this script performs fine in all cases. (New instance of illustrator or not.)

       

      Am I doing something wrong with my approach to export as WOSVG?

       

      #target illustrator
      app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
      
      var sampleDWG = "myPath\\myCadfile.dwg";
      
      processDrawing (sampleDWG);
      
      function processDrawing(mySampleDWG){
          openFile(mySampleDWG);
          exportAsSVGWO();
         
          }
      
      function openFile(myFile){
          var autoCADOpenOptions = app.preferences.AutoCADFileOptions; 
          var fileRef =myFile;
          app.open(new File( fileRef, autoCADOpenOptions ));
       }
      
      function exportAsSVGWO(){
          try {
      if (app.documents.length > 0 ) {
      
      
      // Get the folder to save the files into
      var destFolder = null;
      destFolder = "myfinalLocation\\";
      
      if (destFolder != null) {
      var options, i, sourceDoc, targetFile;
      
          // Get the SVG options to be used.
      options = this.getOptions();
             
      for ( i = 0; i < app.documents.length; i++ ) {
      sourceDoc = app.documents[i]; // returns the document object
      
      // Get the file to save the document as svg into
      targetFile = this.getTargetFile(sourceDoc.name, '.svg', destFolder);
      
      // Save as SVG
      sourceDoc.exportFile(targetFile, ExportType.WOSVG, options); //works ok as standard svg
      
      }
      }
      }
      else{
      throw new Error('There are no documents open!');
      }
      }
      catch(e) {
      alert( e.message, "Script Alert", true);
      }
          }
      
      /** Returns the options to be used for the generated files.
      @return ExportOptionsSVG object
      */
      function getOptions()
      {
      
          //Works ok as standard SVG
          //var options = new ExportOptionsSVG(); 
      
          var options = new ExportOptionsWebOptimizedSVG();
           
      options.svgMinify= true;
      options.coordinatePrecision=4;
      options.rasterImageLocation=RasterImageLocation.EMBED; 
      options.svgId=SVGIdType.SVGIDMINIMAL;
       options.artboardRange=1;
      
      return options;
      }
      
      function getTargetFile(docName, ext, destFolder) {
      var newName = "";
      
      // if name has no dot (and hence no extension),
      // just append the extension
      if (docName.indexOf('.') < 0) {
      newName = docName + ext;
      } else {
      var dot = docName.lastIndexOf('.');
      newName += docName.substring(0, dot);
      newName += ext;
      }
      
      // Create the file object to save to
      var myFile = new File( destFolder + '/' + newName );
      
      // Preflight access rights
      if (myFile.open("w")) {
      myFile.close();
      }
      else {
      throw new Error('Access is denied');
      }
      return myFile;
      }
      
        • 1. Re: export as WOSVG produces file with null content on first run
          Silly-V Adobe Community Professional

          Try by removing the lines dealing with creating a file with File.open("w").

          • 2. Re: export as WOSVG produces file with null content on first run
            jajohns8 Level 1

            Hi Silly V. Thanks for taking the time to look at this.

             

            When I comment out that section, the files do not save at all. Everything seems to work, it just doesn't write the file to destination.

             

            I was not expecting the file to not write AT ALL when removing that section. I thought that part only checked if a file was open.

             

            Edit - On second run through without restarting AI, the script works.

             

            Why will it not work the first time through?

            • 3. Re: export as WOSVG produces file with null content on first run
              Silly-V Adobe Community Professional

              change this line:

              targetFile = this.getTargetFile(sourceDoc.name, '.svg', destFolder);

               

              to this:

              targetFile = File(destFolder + "/" + sourceDoc.name.replace(/\..+&/, '') + ".svg");

               

              Not sure why it doesn't work on first run - rather I'm not sure why it does work on the second run! haha!

              • 4. Re: export as WOSVG produces file with null content on first run
                jajohns8 Level 1

                Unfortunately, still does not work. Same behavior.

                 

                It will work if I don't close illustrator and run the script a second time.

                 

                If I open an autocad file first, then run a script that only exports as WOSVG, it works.

                 

                It seems that scripting a file to open, then calling the export as SVG in same script will fail the first time through.

                • 5. Re: export as WOSVG produces file with null content on first run
                  Silly-V Adobe Community Professional

                  Oh, maybe it's cause the file opening line is messed up. Disregard my previous advice!

                   

                  app.open(new File( fileRef, autoCADOpenOptions ));

                   

                  This open command takes the file object, color space, and options.

                  So it should be app.open(File(fileRef), undefined, autoCADOpenOptions);

                   

                  Try this now.

                  • 6. Re: export as WOSVG produces file with null content on first run
                    jajohns8 Level 1

                    Silly V - Still same behavior with the changed open command. It fails on first try, then runs on the second try as long as I don't close illustrator.

                    • 7. Re: export as WOSVG produces file with null content on first run
                      Silly-V Adobe Community Professional

                      I made a couple of changes such as file paths to my paths, the sample file to an .ai file and no using open options arguments, and I put the entire script into a wrapper function. The "this." were removed from the function call lines. It worked on the first try.
                      Also, I am on a Mac

                      • 8. Re: export as WOSVG produces file with null content on first run
                        jajohns8 Level 1

                        Hi Silly,

                         

                        Can you post your code you used?

                         

                        Here is what I have just tried and it has the same behavior. It works only on the second attempt with same session of Illustrator. (I am on windows 10.)

                         

                        #target illustrator 
                        app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS; 
                        function wrapperFunc(){
                           
                        var sampleDWG = "\\myPath\\arch_5_mod.ai"; 
                        
                        processDrawing (sampleDWG); 
                        
                        function processDrawing(mySampleDWG){ 
                            openFile(mySampleDWG); 
                            exportAsSVGWO(); 
                            app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                            } 
                        
                        function openFile(myFile){ 
                            var autoCADOpenOptions = app.preferences.AutoCADFileOptions; 
                            var fileRef =myFile; 
                            app.open(new File( fileRef, autoCADOpenOptions )); 
                        } 
                        
                        function exportAsSVGWO(){ 
                            try { 
                        if (app.documents.length > 0 ) { 
                        
                        
                        // Get the folder to save the files into 
                        var destFolder = null; 
                        destFolder = "myPath\\svgFinal\\"; 
                        
                        if (destFolder != null) { 
                        var options, i, sourceDoc, targetFile; 
                        
                            // Get the SVG options to be used. 
                        options = getOptions(); 
                               
                        for ( i = 0; i < app.documents.length; i++ ) { 
                        sourceDoc = app.documents[i]; // returns the document object 
                        
                        // Get the file to save the document as svg into 
                        targetFile = getTargetFile(sourceDoc.name, '.svg', destFolder); 
                        
                        // Save as SVG 
                        sourceDoc.exportFile(targetFile, ExportType.WOSVG); //works ok as standard svg 
                        
                        } 
                        } 
                        } 
                        else{ 
                        throw new Error('There are no documents open!'); 
                        } 
                        } 
                        catch(e) { 
                        alert( e.message, "Script Alert", true); 
                        } 
                            } 
                        
                        /** Returns the options to be used for the generated files.
                        @return ExportOptionsSVG object
                        */ 
                        function getOptions() 
                        { 
                              //Works ok as standard SVG 
                            //var options = new ExportOptionsSVG(); 
                        
                            var options = new ExportOptionsWebOptimizedSVG(); 
                             
                        options.svgMinify= true; 
                        options.coordinatePrecision=4; 
                        options.rasterImageLocation=RasterImageLocation.EMBED; 
                        options.svgId=SVGIdType.SVGIDMINIMAL; 
                        options.artboardRange=1; 
                        
                        return options; 
                        } 
                        
                        function getTargetFile(docName, ext, destFolder) { 
                        var newName = ""; 
                        
                        // if name has no dot (and hence no extension), 
                        // just append the extension 
                        if (docName.indexOf('.') < 0) { 
                        newName = docName + ext; 
                        } else { 
                        var dot = docName.lastIndexOf('.'); 
                        newName += docName.substring(0, dot); 
                        newName += ext; 
                        } 
                        
                        // Create the file object to save to 
                        var myFile = new File( destFolder + '/' + newName ); 
                        
                        // Preflight access rights 
                        if (myFile.open("w")) { 
                        myFile.close(); 
                        } 
                        else { 
                        throw new Error('Access is denied'); 
                        } 
                        return myFile; 
                        } 
                        
                        
                        }
                        
                        
                        wrapperFunc();
                        
                        • 9. Re: export as WOSVG produces file with null content on first run
                          Silly-V Adobe Community Professional

                          you still have the "app.open(new File( fileRef, autoCADOpenOptions ));" . That 2nd argument does not go into a File constructor, and it goes in the 3rd spot if needed, inside the app.open arguments.

                          • 10. Re: export as WOSVG produces file with null content on first run
                            jajohns8 Level 1

                            I changed it to: app.open(File( fileRef )); and still same issue.

                            • 11. Re: export as WOSVG produces file with null content on first run
                              Silly-V Adobe Community Professional

                              Oh you know what else, I only used forward-slashes in my file paths. The way you have them, the line destFolder + '/' + newName would then translate to "\myPath\svgFinal/fileName.svg" , which may mess it up too.

                              • 12. Re: export as WOSVG produces file with null content on first run
                                jajohns8 Level 1

                                Hmm, when I debug and display the values for myFile, it seems ok

                                This also showed that I was incorrectly using a for loop around line 35 of my original code at top.

                                 

                                I removed the for loop  in favor of sourceDoc = app.activeDocument;.

                                 

                                Thanks again for taking your time to look at this. It's much appreciated.

                                • 13. Re: export as WOSVG produces file with null content on first run
                                  Silly-V Adobe Community Professional

                                  So it works now? What did it say about the for-loop?

                                  • 14. Re: export as WOSVG produces file with null content on first run
                                    jajohns8 Level 1

                                    No, still does not work. Same behavior.

                                     

                                    Sorry for confusion. I meant the values of the variable myFile is ok, in response to your question about the construction of that path.

                                     

                                    I mentioned that I removed the for loop in my original code at the top so that sourceDoc = app.activeDocument.

                                    • 15. Re: export as WOSVG produces file with null content on first run
                                      moluapple Level 4

                                      Hi, it's better using simple snippet for debug.

                                      #target illustrator
                                      app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
                                      
                                      var sampleDWG = "myPath\\myCadfile.dwg";
                                      
                                      processDrawing(sampleDWG);
                                      
                                      function processDrawing(mySampleDWG) {
                                          exportAsSVGWO(openFile(mySampleDWG));
                                      }
                                      
                                      function openFile(myFile) {
                                          var autoCADOpenOptions = app.preferences.AutoCADFileOptions;
                                          return app.open(new File(myFile));
                                      }
                                      
                                      function exportAsSVGWO(doc) {
                                          // Get the folder to save the files into
                                          var destFolder = "myfinalLocation\\";
                                          if (destFolder != null) {
                                              var options = getOptions(),
                                                  targetFile = destFolder + doc.name.replace(/(\w*)(\.\w*)?$/, '$1.svg');
                                              doc.exportFile(new File(targetFile), ExportType.WOSVG, options);
                                              doc.close(SaveOptions.DONOTSAVECHANGES);
                                          }
                                      }
                                      
                                      /** Returns the options to be used for the generated files. 
                                      @return ExportOptionsSVG object 
                                      */
                                      function getOptions() {
                                          var options = new ExportOptionsWebOptimizedSVG();
                                          options.svgMinify = true;
                                          options.coordinatePrecision = 4;
                                          options.rasterImageLocation = RasterImageLocation.EMBED;
                                          options.svgId = SVGIdType.SVGIDMINIMAL;
                                          options.artboardRange = 1;
                                          return options;
                                      }
                                      
                                      
                                      • 16. Re: export as WOSVG produces file with null content on first run
                                        jajohns8 Level 1

                                        Thanks moluapple. Your snippet is definitely more efficient for debugging. I wish I could change my original post to this snippet.

                                        I was able to run your snippet and it has the same behavior. It runs but does not produce output the first time (With a new session of illustrator) but second time I run it with an illustrator session, it works.

                                         

                                        Again, after changing the export format to standard SVG, the code runs as expected without any issues.

                                        • 17. Re: export as WOSVG produces file with null content on first run
                                          Silly-V Adobe Community Professional

                                          My code which runs first time as expected on my Mac:

                                           

                                          #target illustrator
                                          function test(){
                                          
                                            app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
                                          
                                          
                                            // var sampleDWG = "myPath\\myCadfile.dwg";
                                            var sampleDWG = "/Users/Me/Desktop/Test Sample File.ai";
                                          
                                          
                                            processDrawing(sampleDWG);
                                          
                                          
                                            function processDrawing(mySampleDWG) {
                                              openFile(mySampleDWG);
                                              exportAsSVGWO();
                                          
                                          
                                            }
                                          
                                          
                                            function openFile(myFile) {
                                              var autoCADOpenOptions = app.preferences.AutoCADFileOptions;
                                              var fileRef = myFile;
                                              app.open(new File(fileRef));
                                            }
                                          
                                          
                                            function exportAsSVGWO() {
                                              try {
                                                if (app.documents.length > 0) {
                                          
                                          
                                          
                                          
                                                  // Get the folder to save the files into 
                                                  var destFolder = null;
                                                  destFolder = Folder("/Users/me/Desktop/");
                                          
                                          
                                                  if (destFolder != null) {
                                                    var options, i, sourceDoc, targetFile;
                                          
                                          
                                                    // Get the SVG options to be used. 
                                                    options = getOptions();
                                          
                                          
                                                    for (i = 0; i < app.documents.length; i++) {
                                                      sourceDoc = app.documents[i]; // returns the document object 
                                          
                                          
                                                      // Get the file to save the document as svg into 
                                                      targetFile = getTargetFile(sourceDoc.name, '.svg', destFolder);
                                          
                                          
                                                      // Save as SVG 
                                                      sourceDoc.exportFile(targetFile, ExportType.WOSVG, options); //works ok as standard svg 
                                          
                                          
                                                    }
                                                  }
                                                } else {
                                                  throw new Error('There are no documents open!');
                                                }
                                              } catch (e) {
                                                alert(e.message, "Script Alert", true);
                                              }
                                            }
                                          
                                          
                                            /** Returns the options to be used for the generated files.
                                            @return ExportOptionsSVG object
                                            */
                                            function getOptions() {
                                          
                                          
                                              //Works ok as standard SVG 
                                              //var options = new ExportOptionsSVG(); 
                                          
                                          
                                              var options = new ExportOptionsWebOptimizedSVG();
                                          
                                          
                                              options.svgMinify = true;
                                              options.coordinatePrecision = 4;
                                              options.rasterImageLocation = RasterImageLocation.EMBED;
                                              options.svgId = SVGIdType.SVGIDMINIMAL;
                                              options.artboardRange = 1;
                                          
                                          
                                              return options;
                                            }
                                          
                                          
                                            function getTargetFile(docName, ext, destFolder) {
                                              var newName = "";
                                          
                                          
                                              // if name has no dot (and hence no extension), 
                                              // just append the extension 
                                              if (docName.indexOf('.') < 0) {
                                                newName = docName + ext;
                                              } else {
                                                var dot = docName.lastIndexOf('.');
                                                newName += docName.substring(0, dot);
                                                newName += ext;
                                              }
                                          
                                          
                                              // Create the file object to save to 
                                              var myFile = new File(destFolder + '/' + newName);
                                          
                                          
                                              // Preflight access rights 
                                              if (myFile.open("w")) {
                                                myFile.close();
                                              } else {
                                                throw new Error('Access is denied');
                                              }
                                              return myFile;
                                            }
                                          };
                                          test();
                                          
                                          • 18. Re: export as WOSVG produces file with null content on first run
                                            jajohns8 Level 1

                                            Silly-

                                            Thanks again for your time in looking at this.

                                            Unfortunately this doesn't work for me on windows 10. It still produces a file of 0 kb the first time through on a new instance of Adobe Illustrator and then works correctly the second time. (I need it to work on the first try of a new instance of AI as this will be part of an automated/scheduled task.)

                                             

                                            My only change of your code was to adjust my file paths.

                                             

                                            Again, everything works fine for export as standard SVG.

                                             

                                            Is it possible the export as Web Optimized SVG is bugged for Windows 10?