Skip to main content
sebd75331243
Known Participant
September 8, 2016
Answered

SVG batch: where to add resize artboard code ?

  • September 8, 2016
  • 3 replies
  • 1305 views

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

  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); // 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++ )

  {

  newName += docName;

  }

  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;

}

This topic has been closed for replies.
Correct answer Larry G. Schneider

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

3 replies

Larry G. Schneider
Community Expert
Community Expert
September 8, 2016

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

sebd75331243
Known Participant
September 8, 2016

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!

Larry G. Schneider
Community Expert
Community Expert
September 8, 2016

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.

sebd75331243
Known Participant
September 8, 2016

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; // 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

.........

Larry G. Schneider
Community Expert
Larry G. SchneiderCommunity ExpertCorrect answer
Community Expert
September 8, 2016

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

Inspiring
September 8, 2016

If you put it between the lines:

sourceDoc = app.open(files); // 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!