问题
Can anyone help me write a script for illustrator CC2017 that Export files to web (legacy) as JPG then save the file and close after. I have 700 files each with 2 art boards and it would be painful to click file>Export>Save For Web(legacy) then right the file name and save the file then close.
回答1:
Here is the script as per your requirement. I have just updated Script 1 to match your requirement. By default it assumes ruler is in Points and convert it into inches and use in the file name. You can add more check to handle other ruler units. This will have a-z if artboards are not more than 26 in case artboards are more than 26, it will show something else. Using ASCII code for this
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;
}
}
回答2:
Here is the javascript code that will export all ai files present in the selected folder into a jpg. This code will ask you to select a folder. So select folder that will have 700 files
Script 1: Using 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;
}
}
Since you have two artboards in each ai file. It will create two separate jpg for each artboard. You can change file name and folder location for jpg images as per requirement.
Script 2: By changing resolution
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;
}
}
For script 2, there will be only single file for one ai file, irrespective of the artboards. SO you can run both script for your work and go ahead.
回答3:
VBA example that uses one Excel-specific statement
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
来源:https://stackoverflow.com/questions/45970036/export-script-for-illustrator-to-save-for-web-jpg