I’m working on two Photoshop JavaScript (JSX) scripts using Photoshop 23.1.1 on Windows to automate sprite processing. The first script aims to count unique colors in sprite frames (extracted with Alferd Spritesheet Unpacker version 19) by converting images to Indexed Color mode and logging results to C:/Video Factory/color_log.txt. The second script is intended to open sprites and center them on a 512×512 canvas, optionally processing multiple files from a folder. Both scripts are encountering persistent errors, and I need help resolving them.
Issue 1: Counting Unique Colors with Indexed Color Mode
What I’ve Tried:
- Basic changeMode Call: Used doc.changeMode(ChangeMode.INDEXEDCOLOR) to convert images, followed by doc.colorTable.length to count colors. Resulted in Error 8107: The specified conversion to indexed color mode requires conversion options.
2.Adding Conversion Options: Passed an options object ({ dither: Dither.NONE, palette: Palette.EXACT, colors: 256 }) to changeMode, leading to Error 1243: Illegal argument – argument 2 – Object expected.
3.IndexedColorMode Object: Attempted var options = new IndexedColorMode(); options.palette = Palette.EXACT; options.dither = Dither.NONE; options.colors = 256;, but got Error 22: IndexedColorMode does not have a constructor.
4.RGB Pre-Conversion: Added if (doc.mode != ChangeMode.RGB) doc.changeMode(ChangeMode.RGB) before conversion, still resulting in Error 8107.
5.Action Manager: Tried executeAction(charIDToTypeID(“CnvM”), new ActionDescriptor().putClass(charIDToTypeID(“T “), charIDToTypeID(“IndC”)), DialogModes.NO), but it also failed with Error 8107.
Current Script (Color Count):
#target photoshop
function getUniqueColorCount(doc) {
if (doc.mode != ChangeMode.RGB) {
doc.changeMode(ChangeMode.RGB);
}
var idconvertMode = charIDToTypeID("CnvM");
var desc = new ActionDescriptor();
var idtoMode = charIDToTypeID("T ");
desc.putClass(idtoMode, charIDToTypeID("IndC"));
executeAction(idconvertMode, desc, DialogModes.NO);
var colorTable = doc.colorTable;
return colorTable.length;
}
function processSprites(folder) {
var logFile = new File("C:/Video Factory/color_log.txt");
logFile.open("w");
logFile.writeln("FilenametColor Count");
var files = folder.getFiles(function(file) {
return file instanceof File && (file.name.match(/.(jpg|jpeg|png|tiff)$/i));
});
if (files.length === 0) {
alert("No image files found in the selected folder.");
return;
}
for (var i = 0; i < files.length; i++) {
var doc = app.open(files[i]);
try {
var colorCount = getUniqueColorCount(doc);
logFile.writeln(files[i].name + "t" + colorCount);
} catch (e) {
logFile.writeln(files[i].name + "tError: " + e.message);
}
doc.close(SaveOptions.DONOTSAVECHANGES);
}
logFile.close();
}
var folder = Folder.selectDialog("Select the folder with sprites");
if (folder != null) {
processSprites(folder);
} else {
alert("No folder selected. Exiting script.");
}
Problems:
-
Error 8107 persists, indicating Photoshop requires conversion options that I can’t successfully provide.
-
Manual conversion (Image > Mode > Indexed Color) works but prompts for settings, which I can’t replicate in the script.
-
The API seems to reject both object-based options and Action Manager calls for this purpose.
Issue 2: Centering Sprites on 512×512 Canvas
What I’ve Tried:
Single Sprite Script: Created a script to open a sprite, create a 512×512 canvas, and center it using doc.activeLayer.translate((doc.width – spriteWidth) / 2, (doc.height – spriteHeight) / 2) after copying and pasting. This worked for a single file but required a hardcoded path.
Batch Processing: Modified the script to loop through a folder using Folder.selectDialog and process multiple sprites. The script runs but fails to center sprites correctly, often placing them off-center or throwing errors like Error 8802: General Photoshop error when handling multiple files.
Current Script (Canvas Centering):
#target photoshop
function createCanvas() {
var doc = app.documents.add(512, 512, 72, "Sprite Canvas", NewDocumentMode.RGB, DocumentFill.TRANSPARENT);
return doc;
}
function openSpriteAndPlace(spritePath) {
var sprite = app.open(new File(spritePath));
var doc = createCanvas();
var spriteWidth = sprite.width;
var spriteHeight = sprite.height;
var x = (doc.width - spriteWidth) / 2;
var y = (doc.height - spriteHeight) / 2;
sprite.selection.selectAll();
sprite.selection.copy();
doc.paste();
doc.activeLayer.translate(x, y);
sprite.close(SaveOptions.DONOTSAVECHANGES);
}
var folder = Folder.selectDialog("Select the folder with sprites");
if (folder != null) {
var files = folder.getFiles(function(file) {
return file instanceof File && (file.name.match(/.(jpg|jpeg|png|tiff)$/i));
});
for (var i = 0; i < files.length; i++) {
openSpriteAndPlace(files[i]);
}
}
Problems:
Centering Issues: The translate method sometimes misaligns sprites, especially with varying sizes, and the script doesn’t always maintain a new canvas per sprite in batch mode, leading to overlap.
Error 8802: Occurs intermittently during batch processing, possibly due to document state conflicts or layer handling.
No Export: The script lacks a save function, and adding doc.saveAs with a custom filename causes further errors (e.g., Error 1302: File already exists without overwrite handling).
Questions:
Color Count: How can I correctly pass conversion options to changeMode or Action Manager to convert to Indexed Color mode without Error 8107?
Is there a way to simulate the manual dialog settings in script?
Canvas Centering: How can I ensure each sprite is centered on a new 512×512 canvas in batch mode, avoiding Error 8802 and enabling export with unique filenames (e.g., appending an index or original name)?
Alternative Methods: Are there better approaches (e.g., Action recording) to achieve these tasks in Photoshop scripting?