Known Participant
June 2, 2024
Answered
Trying to fix my script to batch process 3400 files
- June 2, 2024
- 4 replies
- 3664 views
I am trying to fix my script, but it is just not working. I attached a before and after file of what result I am seeking. I have over 3400 files to process.
Basically, I need the script to do the following:
Move Cutlines layer (Top layer) below Artwork layer (Bottom layer)
Hide top layer, which is now Artwork
Select all on bottom layer, which is Cutlines
Set fill color to #FFFFFF
Set stroke color to #F5F5F5
Set stroke weight to 0.1 px
Select all
Apply drop shadow effect (Mode: Normal, Opacity: 100%, X Offset: 2px, Y Offset: 2px, Blur: 3px, and color #b4b4b4)
Show all layers
Select all
Export selection as PNG 1000ppi PNG 8
Save as eps file.
The script below is what I have and it's just not working.
// Prompt the user to select the source and destination folders
var sourceFolder = Folder.selectDialog("Select the folder with EPS files to process");
var destinationFolder = Folder.selectDialog("Select the destination folder to save processed files");
if (sourceFolder != null && destinationFolder != null) {
var files = sourceFolder.getFiles("*.eps");
for (var i = 0; i < files.length; i++) {
var doc = app.open(files[i]);
if (doc) {
moveCutlinesBelowArtwork(doc);
hideTopLayer(doc);
processCutlinesLayer(doc);
showAllLayers(doc);
exportSelectionAsPNG(doc, destinationFolder, files[i].name);
saveAsEPS(doc, destinationFolder, files[i].name);
doc.close(SaveOptions.DONOTSAVECHANGES);
} else {
$.writeln("Could not open file: " + files[i].name);
}
}
$.writeln("Processing complete!");
} else {
$.writeln("Folder selection was cancelled.");
}
function moveCutlinesBelowArtwork(doc) {
if (doc.layers.length >= 2) {
var cutlinesLayer = doc.layers[0];
var artworkLayer = doc.layers[1];
cutlinesLayer.move(artworkLayer, ElementPlacement.PLACEAFTER);
} else {
$.writeln("Document does not have at least two layers.");
}
}
function hideTopLayer(doc) {
if (doc.layers.length >= 2) {
var artworkLayer = doc.layers[0];
artworkLayer.visible = false;
} else {
$.writeln("Document does not have at least two layers.");
}
}
function processCutlinesLayer(doc) {
var cutlinesLayer = doc.layers[doc.layers.length - 1]; // Ensure it's the bottom layer
selectAllOnLayer(cutlinesLayer);
for (var i = 0; i < cutlinesLayer.pageItems.length; i++) {
var item = cutlinesLayer.pageItems[i];
if (item.typename === "PathItem") {
item.filled = true;
item.fillColor = hexToRgbColor("#FFFFFF");
item.stroked = true;
item.strokeColor = hexToRgbColor("#F5F5F5");
item.strokeWidth = 0.1;
}
}
applyDropShadow();
}
function selectAllOnLayer(layer) {
layer.hasSelectedArtwork = true;
app.executeMenuCommand("group");
}
function hexToRgbColor(hex) {
var rgb = new RGBColor();
rgb.red = parseInt(hex.substring(1, 3), 16);
rgb.green = parseInt(hex.substring(3, 5), 16);
rgb.blue = parseInt(hex.substring(5, 7), 16);
return rgb;
}
function applyDropShadow() {
var dropShadowEffect = app.effects.getByName("Drop Shadow");
var options = new DropShadowOptions();
options.mode = ShadowMode.NORMAL;
options.opacity = 100;
options.xOffset = 2;
options.yOffset = 2;
options.blur = 3;
options.color = hexToRgbColor("#b4b4b4");
app.executeMenuCommand("Live Effects");
app.executeMenuCommand("Drop Shadow");
}
function showAllLayers(doc) {
for (var i = 0; i < doc.layers.length; i++) {
doc.layers[i].visible = true;
}
}
function exportSelectionAsPNG(doc, destinationFolder, fileName) {
var exportOptions = new ExportOptionsPNG24();
exportOptions.transparency = true;
exportOptions.horizontalScale = 1000 / 72 * 100; // 1000 ppi
exportOptions.verticalScale = 1000 / 72 * 100; // 1000 ppi
exportOptions.artBoardClipping = true;
var file = new File(destinationFolder + "/" + fileName.replace(".eps", ".png"));
doc.exportFile(file, ExportType.PNG24, exportOptions);
$.writeln("Exported to PNG: " + fileName.replace(".eps", ".png"));
}
function saveAsEPS(doc, destinationFolder, fileName) {
var epsOptions = new EPSSaveOptions();
epsOptions.compatibility = Compatibility.ILLUSTRATOR10;
epsOptions.preview = EPSPreview.None;
epsOptions.cmykPostScript = false;
epsOptions.embedAllFonts = true;
epsOptions.includeDocumentThumbnails = true;
epsOptions.saveMultipleArtboards = false;
var file = new File(destinationFolder + "/" + fileName);
doc.saveAs(file, epsOptions);
$.writeln("Saved as EPS: " + fileName);
}
