【问题标题】:How to write a script that does "Save for web" for mutiple resolutions in Adobe Illustrator?如何在 Adob​​e Illustrator 中为多种分辨率编写“保存为 web”的脚本?
【发布时间】:2013-09-18 08:08:35
【问题描述】:

每次我想将图像创建为不同的分辨率时,都必须单击“保存为网络”并更改分辨率,这很乏味。有没有办法编写一个脚本来自动实现这一点,多个分辨率为一个?

【问题讨论】:

    标签: adobe-illustrator


    【解决方案1】:

    这是将所选文件夹的所有文件导出为 PNG 的脚本。您可以在此代码中指定分辨率,图像质量会很好。在此代码中,默认分辨率为 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;
        }
    }
    

    如果要导出为jpg格式,只需在下面一行代码中更改文件扩展名

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

    改成

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

    此外,您可以根据需要更改文件的名称以及导出文件的存储位置。

    希望我的回答对你有所帮助。

    【讨论】:

      【解决方案2】:

      这是一篇旧帖子,但它启发了我为我想要分享的 Android 项目创建不同的版本。 只需创建一个 144x144 像素的文档并运行:

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

      谢谢!

      【讨论】:

        【解决方案3】:

        为此我自己使用此功能

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

        请注意,它使用“_[width]x[height]”后缀命名生成的 png。

        我根据 adobe 的 javascript 参考 http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/pdf/illustrator/scripting/illustrator_scripting_reference_javascript_cs5.pdf(第 59 页)中的示例编写了我的函数。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-16
          • 1970-01-01
          • 2013-07-01
          • 2018-02-08
          • 1970-01-01
          • 2019-06-30
          相关资源
          最近更新 更多