11 Replies Latest reply on Jan 2, 2015 3:38 PM by williamadowling

    fitArtboardToSelectedArt and saveASSVG is not working

    csm_phil Level 4

      Hi Experts,

       

      I have a problem in the below script, the script will execute the "fitArtboardToSelectedArt" function after Export the SVG file in output folder, After I opened the output folder files, not applying the fitartboardToSelectedArt options.

       

      I want to apply the this:

      Object-->Fit to Selected Art. And Export or saveAs SVG file.

       

      Any one help me Where I am made the mistake please guide.

       

      #target illustrator
      
       var myFolder = Folder.selectDialog( "Select a 'INPUT folder' with contains '*.svg' files" );
       if (!myFolder){
          alert("User cancel the process!");
          }
      
      var myOutFolder = Folder.selectDialog("Select the 'OUTPUT  folder'", ""); 
      if (!myOutFolder){
          alert("User cancel the process!");
          }
      
      if ( myFolder != null ) {
          var myFiles = [];
          GetSubFolders(myFolder);
          if ( myFiles.length > 0 ) {
              for ( i=0; i < myFiles.length; i++ ) {
                  app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
                  var myDoc =app.open(myFiles[i]); 
                  var doc = app.activeDocument;
                  var mySaveName = doc.name;
                  doc.layers[0].hasSelectedArtwork = true;
                  doc.fitArtboardToSelectedArt();
                  SaveAsSVG(myOutFolder, mySaveName);
                  }
              }
          else{
              alert("No file in the selected folder");
              }
          }
      
      
      function SaveAsSVG(myOutFolder, mySaveName){
          var exportOptions = new ExportOptionsSVG();
          var type = ExportType.SVG;
          var fileSpec = new File(myOutFolder);
          exportOptions.embedRasterImages = true;
          exportOptions.embedAllFonts = false;
          exportOptions.fontSubsetting = SVGFontSubsetting.GLYPHSUSED;
          //app.activeDocument.saveAs(new File(myOutFolder+"/"+ mySaveName), exportOptions);
          app.activeDocument.exportFile(new File(myOutFolder+"/"+ mySaveName), type, exportOptions);
          }
      
      
      
      //=================================== FUNCTIONS =========================================
      function GetSubFolders(theFolder) {
           var myFileList = theFolder.getFiles();
           for (var i = 0; i < myFileList.length; i++) {
                var myFile = myFileList[i];
                if (myFile instanceof Folder){
                     GetSubFolders(myFile);
                }
                else if (myFile instanceof File && myFile.name.match(/\.svg/i)) {
                     myFiles.push(myFile);
                }
           }
      }
      //---------
      

       

       

      Before Run the Script original svg Image.

       

      Before.png

       

      After the run the script the output is below snapshots

       

      After.png

      Why this happend I don't know how to solve this problem.

       

       

      thx,

      csm_phil

       

      Message was edited by: csm_phil

        • 1. Re: fitArtboardToSelectedArt and saveASSVG is not working
          Muppet Mark Level 5

          Phil, that is most odd and I have not seen this before… Firstly your script kept ripping down AI everytime I ran it… So I did comment out…

           

          //doc.layers[0].hasSelectedArtwork = true;
          //doc.fitArtboardToSelectedArt();
          

           

          and changed it to my prefered method of…

           

          doc.artboards[0].artboardRect = doc.visibleBounds;
          app.redraw();
          
          

           

          With the forced redraw you can see the open file is correctly placed… If I close and reopen then its offset like yours… hum this could be a bug of some sort… I can't find any options of the export class that you are missing so sorry to be of no help…

          1 person found this helpful
          • 2. Re: fitArtboardToSelectedArt and saveASSVG is not working
            csm_phil Level 4

            Hi Muppert,

             

            Thanks for changing the lines.

             

            Plesae check I have modified the JS code. Please check and help me.

             

             

            #target illustrator
            
             var myFolder = Folder.selectDialog( "Select a 'INPUT folder' with contains '*.svg' files" );
             if (!myFolder){
                alert("User cancel the process!");
                }
            
            var myOutFolder = Folder.selectDialog("Select the 'OUTPUT  folder'", ""); 
            if (!myOutFolder){
                alert("User cancel the process!");
                }
            
            if ( myFolder != null ) {
                var myFiles = [];
                GetSubFolders(myFolder);
                if ( myFiles.length > 0 ) {
                    for ( i=0; i < myFiles.length; i++ ) {
                        app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
                        var myDoc =app.open(myFiles[i]); 
                        var doc = app.activeDocument;
                        var mySaveName = doc.name;
            //~             doc.layers[0].hasSelectedArtwork = true;
            //~             doc.fitArtboardToSelectedArt();
                        app.activeDocument.artboards[0].artboardRect = app.activeDocument.visibleBounds;
                        app.redraw();
                        SaveAsSVG(myOutFolder, mySaveName);
                        app.activeDocument.close(SaveOptions.SAVECHANGES)//( SaveOptions.DONOTSAVECHANGES );
                        //app.activeDocument = null;
                        }
                    }
                else{
                    alert("No file in the selected folder");
                    }
                }
            
            
            function SaveAsSVG(myOutFolder, mySaveName){
                var exportOptions = new ExportOptionsSVG();
                var type = ExportType.SVG;
                var fileSpec = new File(myOutFolder);
                exportOptions.embedRasterImages = true;
                exportOptions.embedAllFonts = false;
                exportOptions.fontSubsetting = SVGFontSubsetting.GLYPHSUSED;
                //app.activeDocument.saveAs(new File(myOutFolder+"/"+ mySaveName), exportOptions);
                app.activeDocument.exportFile(new File(myOutFolder+"/"+ mySaveName), type, exportOptions);
                }
                
                
            
            //=================================== FUNCTIONS =========================================
            function GetSubFolders(theFolder) {
                 var myFileList = theFolder.getFiles();
                 for (var i = 0; i < myFileList.length; i++) {
                      var myFile = myFileList[i];
                      if (myFile instanceof Folder){
                           GetSubFolders(myFile);
                      }
                      else if (myFile instanceof File && myFile.name.match(/\.svg/i)) {
                           myFiles.push(myFile);
                      }
                 }
            }
            //---------
            

             

             

            I am not clear the below Point, what I requried now, Once the script redraw, after I want to save As the SVG file in out put folder, or Export SVG file in the output folder.

            ------------------

            "I can't find any options of the export class that you are missing so sorry to be of no help…"

             

            Please help me I am very stuck this point.

             

            thx,

            csm_phil

            • 3. Re: fitArtboardToSelectedArt and saveASSVG is not working
              Muppet Mark Level 5

              Phil, I don't think I can help… after looking at this issue I can't even get this to work using the GUI… If I manually set the artbaord to the document bounds selection whatever… I can save the result to *.svg file but when I re-open to check the art is offset again… SVG is not a format that I use myself so I don't know if this is or isn't an issue… It may be better to ask in the main forum why this happens…?

              • 4. Re: fitArtboardToSelectedArt and saveASSVG is not working
                csm_phil Level 4

                Dear Muppet,

                 

                Can you please move this main forum, or tell the forum I will post this request.

                 

                My process is correct or worng?

                 

                thx,

                csm_phil

                • 5. Re: fitArtboardToSelectedArt and saveASSVG is not working
                  Muppet Mark Level 5

                  Sorry I can't move this thread to another forum… Im just an end user… You will need to re-ask and find a solution in the AI general forum… If it can be done then we may be able to replicate the fix in the script… As for you syntax it looks fine to me… I don't think the issue is in that but the AI application itself…

                  • 6. Re: fitArtboardToSelectedArt and saveASSVG is not working
                    CarlosCanto Adobe Community Professional & MVP

                    it seems to be a bug...or a "feature"? if you ask adobe. What is shifting is the Artboard, not the Art, the artboad is going back to its original (0,0) position.

                    svgFitABtoArt.PNG

                    1 person found this helpful
                    • 7. Re: fitArtboardToSelectedArt and saveASSVG is not working
                      Larry G. Schneider Adobe Community Professional & MVP

                      Could this be a problem with the coordinate system?

                       

                      CoordinateSystem

                      DOCUMENTCOORDINATESYSTEM

                      ARTBOARDCOORDINATESYSTEM

                      • 8. Re: fitArtboardToSelectedArt and saveASSVG is not working
                        CarlosCanto Adobe Community Professional & MVP

                        I just tried that in the UI and didn't make a difference, artboard still shifting back to home plate.

                        • 9. Re: fitArtboardToSelectedArt and saveASSVG is not working
                          csm_phil Level 4

                          Hi Muppter/Carlos/Larry.

                           

                          Thanks for your interested to help my request.

                           

                          Carlos your example is good, The same problem I am getting still. Now I am clear there is no chance get the original position. This is the Bug in Illustrator.

                           

                          thx,

                          csm_phil

                          • 10. Re: fitArtboardToSelectedArt and saveASSVG is not working
                            Muppet Mark Level 5

                            While this does look more of a bug than a feature ( why would anybody want this? ). It would also appear Carlos has told you how to work around it too… You need to translate all the art to the top of the doc then resize the artboard… This should be simple enough to do…

                             

                            app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
                            var doc = app.activeDocument;
                            var offset = doc.visibleBounds;
                            var items = doc.pageItems;
                            var count = items.length;
                            for ( var i = 0; i < count; i++ ) {
                                items[i].translate( -(offset[0]), -(offset[1]) );
                            };
                            doc.artboards[0].artboardRect = doc.visibleBounds;
                            app.redraw();
                            
                            • 11. Re: fitArtboardToSelectedArt and saveASSVG is not working
                              williamadowling Level 4

                              Is there a way to do this while adding a new artboard? i'm looking to create a new artboard for each groupItem within a document.

                               

                              Mark, your solution works great, but only on an artboard that currently exists.

                               

                              little help? = )

                               

                              **UPDATE**

                               

                              i got the function to create a new artboard as needed, but now i ran into an issue with grouping order. is there a way to refer only to top level groups?

                               

                              the script below is creating a new artboard for each child group within each parent group. sorry to hijack.. but this thread had everything else i was looking for on the subject. =)

                               

                              var docRef = app.activeDocument;
                              var aB = docRef.artboards;
                              var gI = docRef.groupItems;
                              
                              for (a = 0; a< gI.length; a++){
                                  var currentGroup = gI[a];
                                  currentGroup.selected = true;
                                  var vB = currentGroup.visibleBounds;
                                  aB.add(vB);
                              }