Program threw the following errors: 8000 (Cannot open the file because the open options are incorrect) on the line var customPalette = app.open(new File("Path_to_file/High Dripping Grayscale 41.act")); 30 (Illegal 'return' outside of a function body)
var shadesFolder = new Folder("Path_to_file/Shades");
var files = shadesFolder.getFiles("*.png");
var docWidth = app.activeDocument.width;
var docHeight = app.activeDocument.height;
var draftDocument = app.documents.add(docWidth, docHeight, 72, "draft", NewDocumentMode.RGB, DocumentFill.TRANSPARENT);
var cutoutLayers = [];
for (var i = 0; i < files.length; i++) {
var file = files[i];
var tempDoc = app.open(file);
var shadeLayer = tempDoc.activeLayer.duplicate(draftDocument, ElementPlacement.PLACEATEND);
shadeLayer.name = "Shade " + i;
activeDocument.activeLayer = shadeLayer;
executeAction(stringIDToTypeID("rasterizeLayer"), undefined, DialogModes.NO);
if (i > 0) {
var prevLayer = cutoutLayers[i - 1];
var pixels = getSameColorPixels(prevLayer, shadeLayer);
var cutoutLayer = createCutoutLayer(draftDocument, pixels);
cutoutLayers.push(cutoutLayer);
}
}
for (var j = 0; j < cutoutLayers.length; j++) {
activeDocument.activeLayer = cutoutLayers[j];
executeAction(stringIDToTypeID("mergeLayersNew"), undefined, DialogModes.NO);
}
var draftFile = new File("Path_to_file/draft-xx.psd");
app.activeDocument.saveAs(draftFile);
app.documents[0].close(SaveOptions.DONOTSAVECHANGES);
tempDoc.close(SaveOptions.DONOTSAVECHANGES);
function getSameColorPixels(prevLayer, currentLayer) {
var pixels = [];
var prevPixels = prevLayer.channels[0];
var currentPixels = currentLayer.channels[0];
for (var y = 0; y < prevLayer.bounds[3]; y++) {
for (var x = 0; x < prevLayer.bounds[2]; x++) {
var prevColor = prevPixels.colorSamplers[0].color.rgb;
var currentColor = currentPixels.colorSamplers[0].color.rgb;
if (compareColors(prevColor, currentColor)) {
pixels.push([x, y]);
}
}
}
return pixels;
}
function compareColors(color1, color2) {
return (color1.red === color2.red && color1.green === color2.green && color1.blue === color2.blue);
}
function createCutoutLayer(document, pixels) {
var cutoutLayer = document.artLayers.add();
cutoutLayer.name = "Cutout";
var fillColor = new SolidColor();
fillColor.rgb.red = 255;
fillColor.rgb.green = 255;
fillColor.rgb.blue = 255;
activeDocument.selection.select(pixels);
activeDocument.selection.fill(fillColor);
activeDocument.selection.deselect();
return cutoutLayer;
}
var fileFormat = SaveDocumentType.PNG;
var colorReduction = ColorReductionType.SELECTIVE;
var customPalette = app.open(new File("Path_to_file/High Dripping Grayscale 41.act"));
var colors = 41;
var ditherAlgorithm = Dither.NONE;
var transparency = true;
var activeDocument = app.activeDocument;
var activePath = activeDocument.path;
var shadesFolder = new Folder(activePath + "/Shades");
if (!shadesFolder.exists) {
shadesFolder.create();
}
for (var i = 1; i <= colors; i++) {
app.runMenuItem(stringIDToTypeID("export"));
app.runMenuItem(stringIDToTypeID("exportSaveForWeb"));
var exportOptions = new ExportOptionsSaveForWeb();
exportOptions.format = fileFormat;
exportOptions.colorReduction = colorReduction;
exportOptions.palette = customPalette;
exportOptions.colors = i;
exportOptions.dither = ditherAlgorithm;
exportOptions.transparency = transparency;
var fileName = (i - 1) + ".png";
var filePath = new File(shadesFolder + "/" + fileName);
activeDocument.exportDocument(filePath, ExportType.SAVEFORWEB, exportOptions);
}
customPalette.close();
var paletteFile = File.openDialog("Choose a palette file", "*.act");
if (!paletteFile) {
alert("No palette file selected. Script aborted.");
return;
}
var exportOptions = new ExportOptionsSaveForWeb();
exportOptions.format = SaveDocumentType.PNG;
exportOptions.colorReduction = ColorReductionType.CUSTOM;
exportOptions.palette = app.open(paletteFile);
exportOptions.colors = 41;
exportOptions.dither = Dither.NOISE;
exportOptions.transparency = true;
var activeDocument = app.activeDocument;
var activePath = activeDocument.path;
var shadesFolder = new Folder(activePath + "/Shades");
if (!shadesFolder.exists) {
shadesFolder.create();
}
for (var i = 1; i <= exportOptions.colors; i++) {
var colorTable = exportOptions.palette.colorTable;
for (var j = 0; j < colorTable.length; j++) {
if (j != i) {
colorTable[j].transparent = true;
}
}
var fileName = (i - 1) + ".png";
var filePath = new File(shadesFolder + "/" + fileName);
activeDocument.exportDocument(filePath, ExportType.SAVEFORWEB, exportOptions);
}
exportOptions.palette.close();
I have created a script for Adobe Photoshop that works as follows:
- Opens the "Save for Web" function for the active image (File/Export/Save for Web).
- Sets the following export settings: Optimized file format — PNG-8; Color reduction algorithm — High Dripping Grayscale 41 (custom palette); Colors — 41; Dither algorithm — Noise; Transparency — On.
- In the Color Table palette, it selects all shades except the current X and makes them transparent (using the "Maps Selected Color to Transparent" button below the palette).
- Saves the file with the name Y.png (where Y=X-1) in the "Shades" subfolder created in the same directory as the active image. It then repeats this process for each color in sequence, where X=X+1. Additionally, there is a second script related to the first one:
- Collects the resulting files and creates a layered layout (each color in a separate layer).
- Rasterizes all layers.
- Analyzes each pixel of each layer in turn for neighboring pixels of the same color (horizontally and vertically). If a neighboring pixel of the same color is found, it is cut into a separate layer and then merged with the next color's layer (a lighter shade).
- Saves the layout in a file named draft-xx.psd.