Files
jiapuapp/scripts/photoshop/a01-build-layered-psd.jsx
T
2026-07-16 07:58:53 +08:00

64 lines
1.7 KiB
React

#target photoshop
app.displayDialogs = DialogModes.NO;
var createdDocument = null;
var previousRulerUnits = app.preferences.rulerUnits;
function readJson(filePath) {
var file = File(filePath);
if (!file.exists) {
throw new Error('Manifest does not exist: ' + filePath);
}
file.encoding = 'UTF8';
if (!file.open('r')) {
throw new Error('Unable to open manifest: ' + filePath);
}
var content = file.read();
file.close();
return eval('(' + content + ')');
}
try {
app.preferences.rulerUnits = Units.PIXELS;
var manifest = readJson(JIAPU_A01_MANIFEST);
var psdFile = File(JIAPU_A01_PSD);
if (psdFile.exists) {
throw new Error('Refusing to overwrite existing PSD: ' + psdFile.fsName);
}
createdDocument = app.documents.add(
manifest.canvas.width,
manifest.canvas.height,
manifest.canvas.resolution,
'JIAPU_A01_SCRIPT_SOURCE',
NewDocumentMode.RGB,
DocumentFill.TRANSPARENT
);
for (var groupIndex = manifest.groups.length - 1; groupIndex >= 0; groupIndex--) {
var groupSpec = manifest.groups[groupIndex];
var group = createdDocument.layerSets.add();
group.name = groupSpec.name;
group.visible = groupSpec.visible;
}
if (createdDocument.artLayers.length > 0) {
createdDocument.artLayers[0].remove();
}
var saveOptions = new PhotoshopSaveOptions();
saveOptions.layers = true;
createdDocument.saveAs(psdFile, saveOptions, true, Extension.LOWERCASE);
createdDocument.close(SaveOptions.DONOTSAVECHANGES);
createdDocument = null;
} catch (error) {
if (createdDocument !== null) {
createdDocument.close(SaveOptions.DONOTSAVECHANGES);
}
throw error;
} finally {
app.preferences.rulerUnits = previousRulerUnits;
}