如何编写在Adobe Illustrator中为多个分辨率执行“为Web保存”的脚本?

时间:2013-09-13 15:10:17

标签: adobe-illustrator

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

3 个答案:

答案 0 :(得分:4)

为此我自己使用此功能

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页)中的示例编写了我的函数。

答案 1 :(得分:1)

这是一篇旧帖子,但它激发了我为我的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");

谢谢!

答案 2 :(得分:0)

以下是将所选文件夹的所有文件导出为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

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

希望我的回答能帮助你。

相关问题