[javascript] SVG1

Viewer

  1. /**********************************************************   
  2.  
  3. DESCRIPTION  
  4.  
  5. This sample gets files specified by the user from the   
  6.  
  7. selected folder and batch processes them and saves them   
  8.  
  9. as SVGs in the user desired destination with the same   
  10.  
  11. file name.  
  12.  
  13. **********************************************************/    
  14.  
  15. // Main Code [Execution of script begins here]    
  16.  
  17. // uncomment to suppress Illustrator warning dialogs    
  18.  
  19. // app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;    
  20.  
  21. var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pngExportOpts;    
  22.  
  23. // Select the source folder.    
  24.  
  25. sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator files you want to convert to SVG', '~' );    
  26.  
  27. // If a valid folder is selected    
  28.  
  29. if ( sourceFolder != null )    
  30.  
  31. {    
  32.  
  33.     files = new Array();    
  34.  
  35.     //fileType = prompt( 'Select type of Illustrator files to you want to process. Eg: *.ai', ' ' );    
  36.  
  37.     fileType = '*.eps';
  38.  
  39.     
  40.  
  41.     // Get all files matching the pattern    
  42.  
  43.     files = sourceFolder.getFiles( fileType );    
  44.  
  45.     if ( files.length > 0 )    
  46.  
  47.     {    
  48.  
  49.         // Get the destination to save the files    
  50.  
  51.         destFolder = Folder.selectDialog( 'Select the folder where you want to save the converted PNG files.', '~' );    
  52.  
  53.         for (var i = 0; i < files.length; i++ )    
  54.  
  55.         {    
  56.  
  57.             sourceDoc = app.open(files[i]); // returns the document object   
  58.  
  59.             // Call function getNewName to get the name and file to save the svg    
  60.  
  61.             targetFile = getNewName();    
  62.  
  63.             // Create SVG Options Object  
  64.  
  65.             var options = new ExportOptionsSVG();  
  66.  
  67.             // Export as SVG    
  68.  
  69.             sourceDoc.exportFile( targetFile, ExportType.SVG, options );    
  70.  
  71.             sourceDoc.close(SaveOptions.DONOTSAVECHANGES);    
  72.  
  73.         }    
  74.  
  75.         alert( 'Files are saved as UNCOMPRESSED SVG in ' + destFolder );    
  76.  
  77.     }    
  78.  
  79.     else    
  80.  
  81.     {    
  82.  
  83.         alert( 'No matching files found' );    
  84.  
  85.     }    
  86.  
  87. }    
  88.  
  89. /*********************************************************  
  90.  
  91. getNewName: Function to get the new file name. The primary  
  92.  
  93. name is the same as the source file.  
  94.  
  95. **********************************************************/    
  96.  
  97. function getNewName()    
  98.  
  99. {    
  100.  
  101.     var ext, docName, newName, saveInFile, docName;    
  102.  
  103.     docName = sourceDoc.name;    
  104.  
  105.     ext = '.svg'; // new extension for svg file    
  106.  
  107.     newName = "";    
  108.  
  109.     for ( var i = 0 ; docName != "." ; i++ )    
  110.  
  111.     {    
  112.  
  113.         newName += docName;    
  114.  
  115.     }    
  116.  
  117.     newName += ext; // full svg name of the file    
  118.  
  119.     // Create a file object to save the svg  
  120.  
  121.     saveInFile = new File( destFolder + '/' + newName );    
  122.  
  123.     return saveInFile;    
  124.  
  125. }  

Editor

You can edit this paste and save as new:


File Description
  • SVG1
  • Paste Code
  • 02 Jun-2023
  • 2.86 Kb
You can Share it: