7 Replies Latest reply on Sep 8, 2016 12:13 PM by sebd75331243

    SVG batch: where to add resize artboard code ?

    sebd75331243 Level 1

      I wish to include a part of code to get artboard resized and then convert .eps and .ai to SVG files.

      Not sure where it should go ?

      Any clues, let me know

       

      THANKS GUYS !!

       

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

      // artboard resize code

      for (i=0; i < filesToProcess.length; i++) {

       

        app.open(filesToProcess[i]);

       

        var doc = app.activeDocument;

        var myBorder = 10; // Set to width of border desired, in points

        var myVisibleBounds = doc.visibleBounds; // Rect, which is an array;

       

        myVisibleBounds[0] -= myBorder; // left coordinate (use negative values to add artboard)

        myVisibleBounds[1] += myBorder; // top coordinate

        myVisibleBounds[2] += myBorder; // right coordinate

        myVisibleBounds[3] -= myBorder; // bottom coordinate (use negative values to add artboard)

       

        doc.artboards[0].artboardRect = myVisibleBounds;

       

       

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

      WHERE TO PUT IT IN THE SVG BATCH CODE ??

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

       

       

      // Main Code [Execution of script begins here]

       

       

      var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, svgSaveOpts;

       

       

      // Select the source folder.

      sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator files you want to convert to SVG', '~' );

       

       

      // If a valid folder is selected

      if ( sourceFolder != null )

      {

        files = new Array();

        filesToProcess = sourceFolder.getFiles("*.ai","*.eps");

        // Get all files matching the pattern

        files = sourceFolder.getFiles( fileType );

       

        if ( files.length > 0 )

        {

        // Get the destination to save the files

        destFolder = Folder.selectDialog( 'Select the folder where you want to save the converted SVG files.', '~' );

        for ( i = 0; i < files.length; i++ )

        {

        sourceDoc = app.open(files[i]); // returns the document object

       

        // Call function getNewName to get the name and file to save the SVG

        targetFile = getNewName();

       

        // Call function getSVGOptions get the SVGSaveOptions for the files

        svgSaveOpts = getSVGOptions( );

       

        // Save as svg

        sourceDoc.exportFile(targetFile, ExportType.SVG, svgSaveOpts );

       

       

        sourceDoc.close();

        }

        alert( 'Files are saved as SVG in ' + destFolder );

        }

        else

        {

        alert( 'No matching files found' );

        }

      }

       

       

      /*********************************************************

      getNewName: Function to get the new file name. The primary

      name is the same as the source file.

      **********************************************************/

       

      function getNewName()

      {

        var ext, docName, newName, saveInFile, docName;

        docName = sourceDoc.name;

        ext = '.svg'; // new extension for svg file

        newName = "";

       

        for ( var i = 0 ; docName[i] != "." ; i++ )

        {

        newName += docName[i];

        }

        newName += ext; // full svg name of the file

       

        // Create a file object to save the svg

        saveInFile = new File( destFolder + '/' + newName );

       

       

        return saveInFile;

      }

       

       

      function getSVGOptions()

      {

        var svgSaveOpts = new ExportOptionsSVG();

      l

       

        svgSaveOpts.embedRasterImages = true;

       

        return svgSaveOpts;

      }

        • 1. Re: SVG batch: where to add resize artboard code ?
          zertle Level 2

          If you put it between the lines:

           

          sourceDoc = app.open(files[i]); // returns the document object
          
          /* HERE */
          
          // Call function getNewName to get the name and file to save the SVG
          targetFile = getNewName();
          

           

          that should work. Don't forget to update the Resize code to use the new ref to your document object 'sourceDoc' instead of just 'doc'.

           

          Good luck!

          • 2. Re: SVG batch: where to add resize artboard code ?
            Larry G. Schneider Adobe Community Professional & MVP

            Do you want the new border to be permanent or just in the saved SVG files? If second it would be easiest to drop the loop and add the code

             

            var doc = app.activeDocument;

              var myBorder = 10; // Set to width of border desired, in points

              var myVisibleBounds = doc.visibleBounds; // Rect, which is an array;

             

              myVisibleBounds[0] -= myBorder; // left coordinate (use negative values to add artboard)

              myVisibleBounds[1] += myBorder; // top coordinate

              myVisibleBounds[2] += myBorder; // right coordinate

              myVisibleBounds[3] -= myBorder; // bottom coordinate (use negative values to add artboard)

             

              doc.artboards[0].artboardRect = myVisibleBounds;

             

            after the sourceDoc line. Be sure to not save the original when closing.

            • 3. Re: SVG batch: where to add resize artboard code ?
              sebd75331243 Level 1

              Thank for such a fast reply

               

              Well no, the border has to be into the SVG files only but since I won't use the .AI file after conversion it's not a big issue. But since other peoples can enjoy this script, well I think it's better to leave the border to the SVG files.

               

              So you're saying it has to be

               

              ........

              for ( i = 0; i < app.documents.length; i++ ) {

                sourceDoc = app.documents[i]; // returns the document object

               

              !!!!!!!!!!!!!!!!!!!!!!!HERE!!!!!!!!!!!!!!!!!!!!

               

                // Get the file to save the document as svg into

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

               

                // Save as SVG

              .........

               

               

              • 4. Re: SVG batch: where to add resize artboard code ?
                Larry G. Schneider Adobe Community Professional & MVP

                Yes. zertle's reply is the same as mine.

                • 5. Re: SVG batch: where to add resize artboard code ?
                  sebd75331243 Level 1

                  very strange I'm getting an error that wasn't there and that has nothing to the script I added...gee that's strange!

                  ''The file ''.DS_Store'' is an unknown format and cannot be opened.

                   

                  CODE :

                   

                  // Main Code [Execution of script begins here]

                   

                   

                  var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, svgSaveOpts;

                   

                   

                  // Select the source folder.

                  sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator files you want to convert to SVG', '~' );

                   

                   

                  // If a valid folder is selected

                  if ( sourceFolder != null )

                   

                   

                    files = new Array();

                    filesToProcess = sourceFolder.getFiles("*.ai","*.eps");

                    // Get all files matching the pattern

                    files = sourceFolder.getFiles( fileType );

                   

                    if ( files.length > 0 )

                    {

                    // Get the destination to save the files

                    destFolder = Folder.selectDialog( 'Select the folder where you want to save the converted SVG files.', '~' );

                    for ( i = 0; i < files.length; i++ )

                    {

                    sourceDoc = app.open(files[i]); // returns the document object

                   

                    // Increment through the files

                  for (i=0; i < filesToProcess.length; i++) {

                   

                   

                    app.open(filesToProcess[i]);

                   

                   

                    var sourceDoc = app.activeDocument;

                    var myBorder = 5; // Set to width of border desired, in points

                    var myVisibleBounds = sourceDoc.visibleBounds; // Rect, which is an array;

                   

                   

                    myVisibleBounds[0] -= myBorder; // left coordinate (use negative values to add artboard)

                    myVisibleBounds[1] += myBorder; // top coordinate

                    myVisibleBounds[2] += myBorder; // right coordinate

                    myVisibleBounds[3] -= myBorder; // bottom coordinate (use negative values to add artboard)

                   

                   

                    sourceDoc.artboards[0].artboardRect = myVisibleBounds;

                   

                    // Call function getNewName to get the name and file to save the SVG

                    targetFile = getNewName();

                   

                    // Call function getSVGOptions get the SVGSaveOptions for the files

                    svgSaveOpts = getSVGOptions( );

                   

                    // Save as svg

                    sourceDoc.exportFile(targetFile, ExportType.SVG, svgSaveOpts );

                   

                   

                    sourceDoc.close();

                    }

                    alert( 'Files are saved as SVG in ' + destFolder );

                    }

                   

                  }

                   

                   

                  /*********************************************************

                  getNewName: Function to get the new file name. The primary

                  name is the same as the source file.

                  **********************************************************/

                   

                  function getNewName()

                  {

                    var ext, docName, newName, saveInFile, docName;

                    docName = sourceDoc.name;

                    ext = '.svg'; // new extension for svg file

                    newName = "";

                   

                    for ( var i = 0 ; docName[i] != "." ; i++ )

                    {

                    newName += docName[i];

                    }

                    newName += ext; // full svg name of the file

                   

                    // Create a file object to save the svg

                    saveInFile = new File( destFolder + '/' + newName );

                   

                   

                    return saveInFile;

                  }

                   

                   

                  function getSVGOptions()

                  {

                    var svgSaveOpts = new ExportOptionsSVG();

                  l

                   

                    svgSaveOpts.embedRasterImages = true;

                   

                    return svgSaveOpts;

                  }

                  • 6. Re: SVG batch: where to add resize artboard code ?
                    Larry G. Schneider Adobe Community Professional & MVP

                    Is the folder you are accessing on a flash drive or external drive?

                    • 7. Re: SVG batch: where to add resize artboard code ?
                      sebd75331243 Level 1

                      Everything is from internal drive no USB drive here. So this sound like nothing is wrong with the script, like more a computer issue with folders permissions or something else. Can you test drive this script on your computer to see if this problems is on my side ?

                       

                      Thanks for your support!