[javascript] SVG2

Viewer

  1. /**********************************************************
  2.  
  3. DESCRIPTION
  4.  
  5. This sample gets files specified by the user from the
  6. selected folder and batch processes them and saves them
  7. as SVGs in the user desired destination with the same
  8. file name.
  9.  
  10. **********************************************************/
  11.  
  12. // Main Code [Execution of script begins here]
  13.  
  14. // Uncomment to suppress Illustrator warning dialogs
  15. // app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
  16.  
  17. var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile;
  18.  
  19. // Select the source folder.
  20. sourceFolder = Folder.selectDialog('Select the folder with Illustrator files you want to convert to SVG', '~');
  21.  
  22. // If a valid folder is selected
  23. if (sourceFolder != null) {
  24.   files = new Array();
  25.   fileType = '*.eps';
  26.  
  27.   // Get all files matching the pattern
  28.   files = sourceFolder.getFiles(fileType);
  29.  
  30.   if (files.length > 0) {
  31.     // Get the destination to save the files
  32.     destFolder = Folder.selectDialog('Select the folder where you want to save the converted SVG files.', '~');
  33.  
  34.     for (var i = 0; i < files.length; i++) {
  35.       sourceDoc = app.open(files[i]); // Returns the document object
  36.  
  37.       // Call function getNewName to get the name and file to save the svg
  38.       targetFile = getNewName();
  39.  
  40.       // Create SVG Options Object
  41.       var options = new ExportOptionsSVG();
  42.  
  43.       // Export as SVG
  44.       sourceDoc.exportFile(targetFile, ExportType.SVG, options);
  45.  
  46.       sourceDoc.close(SaveOptions.DONOTSAVECHANGES);
  47.     }
  48.  
  49.     alert('Files are saved as UNCOMPRESSED SVG in ' + destFolder);
  50.   } else {
  51.     alert('No matching files found');
  52.   }
  53. }
  54.  
  55. /*********************************************************
  56.  
  57. getNewName: Function to get the new file name. The primary
  58. name is the same as the source file.
  59.  
  60. **********************************************************/
  61.  
  62. function getNewName() {
  63.   var ext, docName, newName, saveInFile;
  64.   docName = sourceDoc.name;
  65.   ext = '.svg'; // New extension for SVG file
  66.   newName = "";
  67.  
  68.   for (var i = 0; i < docName.length; i++) {
  69.     if (docName[i] !== ".") {
  70.       newName += docName[i];
  71.     } else {
  72.       break;
  73.     }
  74.   }
  75.  
  76.   newName += ext; // Full SVG name of the file
  77.  
  78.   // Create a file object to save the SVG
  79.   saveInFile = new File(destFolder + '/' + newName);
  80.   return saveInFile;
  81. }
  82.  

Editor

You can edit this paste and save as new:


File Description
  • SVG2
  • Paste Code
  • 02 Jun-2023
  • 2.35 Kb
You can Share it: