【问题标题】:Export script for illustrator to save for web jpg为 Illustrator 导出脚本以保存为 web jpg
【发布时间】:2018-02-08 17:39:51
【问题描述】:

谁能帮我为 illustrator CC2017 编写一个脚本,将文件以 JPG 格式导出到网络(旧版),然后保存文件并在之后关闭。我有 700 个文件,每个文件有 2 个艺术板,单击文件>导出>保存为 Web(旧版)然后右键文件名并保存文件然后关闭会很痛苦。

【问题讨论】:

  • 你试过了吗?
  • 我已经尝试了 2 天,但我所做的所有代码都是废话,无法正常工作!我对 js 不是很熟悉
  • 我可能会推荐像w3schools.com/js 这样的教程,而不是在这里问。
  • 我在 edx.org 上的学习比 w3schools.com 好,但我想试一试可能会发现有人会提供帮助。

标签: javascript export jpeg adobe-illustrator


【解决方案1】:

这是根据您的要求编写的脚本。我刚刚更新了脚本 1 以符合您的要求。默认情况下,它假定标尺以点为单位并将其转换为英寸并在文件名中使用。您可以添加更多检查以处理其他标尺单位。如果画板不超过 26,这将有 a-z,如果画板超过 26,它将显示其他内容。为此使用 ASCII 码

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 jpegFolder = Folder(currentFile.path + "/JPG");
        if (!jpegFolder.exists)
            jpegFolder.create();
        var codeStart = 97; // for a;
        for (var j = 0; j < activeDocument.artboards.length; j++) {
            var activeArtboard = activeDocument.artboards[j];
            activeDocument.artboards.setActiveArtboardIndex(j);
            var bounds = activeArtboard.artboardRect;
            var left = bounds[0];
            var top = bounds[1];
            var right = bounds[2];
            var bottom = bounds[3];
            var width = right - left;
            var height = top - bottom;
            if (app.activeDocument.rulerUnits == RulerUnits.Points) { //Add more if for more conversions
                width = width / 72;
                height = height / 72;
            }
            var fileName = activeDocument.name.split('.')[0] + "-" + String.fromCharCode(codeStart) + "-" + width + "x" + height + ".jpg";
            var destinationFile = File(jpegFolder + "/" + fileName);
            var type = ExportType.JPEG;
            var options = new ExportOptionsJPEG();
            options.antiAliasing = true;
            options.artBoardClipping = true;
            options.optimization = true;
            options.qualitySetting = 100; // Set Quality Setting
            activeDocument.exportFile(destinationFile, type, options);
            codeStart++;
        }
        activeDocument.close(SaveOptions.DONOTSAVECHANGES);
        currentFile = null;
    }
}

【讨论】:

  • 所以这里有问题,如果画板是 1.5 英寸,我认为它与数学有关,脚本会将其显示为 2 英寸
  • 是的,因为我们使用的是圆形方法。它将四舍五入到下一位。所以你想要 1.5??
  • 是的,希望保留画板的确切尺寸。即使是 1.5
【解决方案2】:

这里是将所选文件夹中存在的所有ai文件导出为jpg的javascript代码。此代码将要求您选择一个文件夹。所以选择将有 700 个文件的文件夹

脚本 1:使用 JPEGQuality

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 jpegFolder = Folder(currentFile.path + "/JPG");
        if (!jpegFolder.exists)
            jpegFolder.create();
        for (var j = 0; j < activeDocument.artboards.length; j++) {
            var activeArtboard = activeDocument.artboards[0];
            activeDocument.artboards.setActiveArtboardIndex(j);
            var fileName = activeDocument.name.split('.')[0] + "Artboard" + (j + 1) + ".jpg";
            var destinationFile = File(jpegFolder + "/" + fileName);
            var type = ExportType.JPEG;
            var options = new ExportOptionsJPEG();
            options.antiAliasing = true;
            options.artBoardClipping = true;
            options.optimization = true;
            options.qualitySetting = 100; // Set Quality Setting
            activeDocument.exportFile(destinationFile, type, options);
        }
        activeDocument.close(SaveOptions.DONOTSAVECHANGES);
        currentFile = null;
    }
} 

因为每个 ai 文件中有两个画板。它将为每个画板创建两个单独的 jpg。您可以根据需要更改jpg图像的文件名和文件夹位置。

脚本 2:通过更改分辨率

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 jpegFolder = Folder(currentFile.path + "/JPG");
        if (!jpegFolder.exists)
            jpegFolder.create();
        var fileName = activeDocument.name.split('.')[0] + ".jpg";
        var destinationFile = File(jpegFolder + "/" + 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;
    }
}

对于脚本 2,一个 ai 文件只有一个文件,与画板无关。 所以你可以为你的工作运行这两个脚本并继续。

【讨论】:

  • 非常感谢您的帮助!它工作完美,但我如何选择 jpg 质量我想确保其 100% 并且是否易于使用字母作为名称而不是数字`“画板”+(j + 1)`
  • 欢迎亲爱的。我已经使用属性 qualitySetting 编辑了上面的代码以获得 jpeg 质量。还有另一种方法也可以导出为 jpeg,您可以在其中设置图像的分辨率。但更高的分辨率处理会很慢,但图像质量会很好。因此,您可以使用任何适合您要求的方式。
  • 我已经编辑了我的答案并建议您使用两种不同的脚本。对于文件的名称,您可以根据需要进行相应更改。或者你只是让我知道你想要jpg的名字。希望我的新答案对您的工作有所帮助。
  • 老兄,这太棒了!第一个脚本似乎很完美,options.qualitySetting = 100; 是什么意思? 100%... 如果您可以将名称更改为 ( 文件名 + a + 画板大小 ) 或 bc 取决于那里有多少画板。如果添加画板尺寸很难,那么就忘了它,但如果有它会很棒。您也可以使用连字符,例如 (file-name-a-11x17.jpg)
  • 谢谢,是的options.qualitySetting = 100 表示 100%。是的,我们可以更改文件名的名称,例如 (filename + a + artboard size), 但我担心 az 是 26 个字符,以防任何文件有超过 26 个画板,那么你想要什么下一个用于第 27 个画板的位置 a、b 或 c 等等?
【解决方案3】:

使用一个 Excel 特定语句的 VBA 示例

Sub Export_All()
Dim fs As Object
Dim aiRef As Object
Dim docRef As Object
Dim jpegExportOptions As Object
Dim f As Object
Dim p As String

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set aiRef = CreateObject("Illustrator.Application")
    Set jpegExportOptions = CreateObject("Illustrator.ExportOptionsJPEG")

    ' Specify all export options here
    jpegExportOptions.AntiAliasing = False
    jpegExportOptions.QualitySetting = 70

    p = Application.ActiveWorkbook.Path         ' Excel-specific.  You may change it to whatever you like

    For Each f In fs.GetFolder(p).Files
        If LCase(Right(f.Name, 3) = ".ai") Then
            Debug.Print f.Name
            Set docRef = aiRef.Open(p + "\" + f.Name)
            Call docRef.Export(p + "\" + f.Name + ".jpg", 1, jpegExportOptions)
            Set docRef = Nothing
        End If
    Next

    ' Note that AI is still open and invisible
End Sub

【讨论】:

  • 为什么是js?!打开 Excel,粘贴并运行!
  • 因为我可以将脚本添加到 ai 文件并从文件菜单运行它。我尝试了您使用 Visual Studio Code 提供的这段代码,但没有奏效。不知道我是否做对了。到目前为止,这是我想出的Script
  • 试试 Excel。将此代码粘贴到代码 VBA 模块。将 XLS 保存到 AI 文件所在的同一文件夹中
猜你喜欢
  • 1970-01-01
  • 2016-07-03
  • 1970-01-01
  • 1970-01-01
  • 2013-09-18
  • 2012-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多