How to write a script that does "Save for the Internet" for multiple resolutions in Adobe Illustrator?

Just click “Save for Web” and change the resolution every time I want to create an image as a different resolution. Is there a way to write a script to automatically achieve this, for multiple permissions as one?

+4
source share
3 answers

For this I myself use this function

function saveDocumentAsPng(d, width, height) { var fileName = d.fullName.toString(); if(fileName.lastIndexOf(".") >= 0) { fileName = fileName.substr(0, fileName.lastIndexOf("."));} fileName += "_wxh.png".replace("w", width).replace("h", height); var exportOptions = new ExportOptionsPNG24(); exportOptions.horizontalScale = exportOptions.verticalScale = 100 * Math.max(width/d.width, height/d.height); var file = new File(fileName); app.activeDocument = d; d.exportFile(file, ExportType.PNG24, exportOptions); } 

Notice that he calls the resulting png with the suffix _ _ [width] x [height].

I wrote my function based on a sample from the adobe JavaScript reference http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/pdf/illustrator/scripting/illustrator_scripting_reference_javascript_cs5.pdf (page 59 )

+4
source

This is an old post, but it inspired me to create another version of my Android project that I would like to share. Just create a document sized 144x144 pixels and run this:

 var doc = app.activeDocument; // This is the scale factor that you can see in "Percent" textbox of // the "Save for Web..." dialog box while changing the size of the image var scale = [100, 66.67, 50, 33.33] // This is the image size for the PNGs files names var size = [144, 96, 72, 48] for(var i = 0; i<4; i++) { var fileName = doc.fullName.toString(); if (fileName.lastIndexOf(".") >= 0) { // Get the file name without the extension fileName = fileName.substr(0, fileName.lastIndexOf(".")); } // Name each file with yours size to avoid overwrite fileName += "_wxh.png".replace("w", size[i]).replace("h", size[i]); var exportOptions = new ExportOptionsPNG24(); exportOptions.horizontalScale = exportOptions.verticalScale = scale[i]; exportOptions.artBoardClipping = true; var file = new File(fileName); doc.exportFile(file, ExportType.PNG24, exportOptions); } Window.alert ("Successfully exported!", "Success"); 

Thanks!

+1
source

Here is a script that will export all the files of the selected folder to PNG. You can specify the resolution in this code, and the image quality will be good. In this code, the default resolution is 600

 var folder = Folder.selectDialog(); if (folder) { var files = folder.getFiles("*.ai"); for (var i = 0; i < files.length; i++) { var currentFile = files[i]; app.open(currentFile); var activeDocument = app.activeDocument; var pngFolder = Folder(currentFile.path + "/PNG"); if (!pngFolder.exists) pngFolder.create(); var fileName = activeDocument.name.split('.')[0] + ".png"; var destinationFile = File(pngFolder + "/" + fileName); // Export Artboard where you can set resolution for an image. Set to 600 by default in code. var opts = new ImageCaptureOptions(); opts.resolution = 600; opts.antiAliasing = true; opts.transparency = true; try { activeDocument.imageCapture(new File(destinationFile), activeDocument.geometricBounds, opts); } catch (e) { } activeDocument.close(SaveOptions.DONOTSAVECHANGES); currentFile = null; } } 

If you want to export in jpg format, just change the file extension in the code in the next line

 var fileName = activeDocument.name.split('.')[0] + ".png"; 

change to

  var fileName = activeDocument.name.split('.')[0] + ".jpg"; // For jpg 

In addition, you can change the file name and where the exported file will be saved as per your requirement.

Hope my answer helps you.

0
source

Source: https://habr.com/ru/post/1502076/


All Articles