Skip to content

Instantly share code, notes, and snippets.

@drugoi
Created June 9, 2017 05:59
Show Gist options
  • Save drugoi/8a46a0d5b7ec3b217a011e2336a4b9c4 to your computer and use it in GitHub Desktop.
Save drugoi/8a46a0d5b7ec3b217a011e2336a4b9c4 to your computer and use it in GitHub Desktop.
Script for counting layers in PSD file.
var totalProgress = 0 // I assume this is defined eleswhere but is needed for the scriptler
function layerCounter(inObj) { // recursive function to count layers
totalProgress += inObj.artLayers.length;
for (var i = 0; i < inObj.layerSets.length; i++) {
totalProgress++;
layerCounter(inObj.layerSets[i]); // recursive call to layerCounter
}
return totalProgress;
}
function getLayerCount() {
function getNumberLayers() {
var ref = new ActionReference();
ref.putProperty(charIDToTypeID('Prpr'), charIDToTypeID('NmbL'))
ref.putEnumerated(charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
return executeActionGet(ref)
.getInteger(charIDToTypeID('NmbL'));
}
function getLayerType(idx) {
var ref = new ActionReference();
ref.putProperty(charIDToTypeID('Prpr'), stringIDToTypeID('layerSection'));
ref.putIndex(charIDToTypeID('Lyr '), idx);
return typeIDToStringID(executeActionGet(ref)
.getEnumerationValue(stringIDToTypeID('layerSection')));
};
var cnt = getNumberLayers();
var res = cnt;
if (activeDocument.layers[activeDocument.layers.length - 1].isBackgroundLayer) {
var i = 0;
res++; // comment out this to exclude background from count
} else {
var i = 1;
};
for (i; i < cnt; i++) {
var temp = getLayerType(i);
if (temp === 'layerSectionEnd') res--;
// if (temp === 'layerSectionStart') res--; // uncomment to count just artLayers
};
return res;
};
function main() {
var answer = confirm('Go through your file and count all the layers?');
if (answer) {
var reporter1 = layerCounter(app.activeDocument);
alert('All done!\nLayer count = ' + reporter1 + ';\nAuthor: github.com/drugoi');
} else {
reporter2 = getLayerCount();
alert('All done!\nLayer count = ' + reporter2 + ';\nAuthor: github.com/drugoi');
}
}
main();
@drugoi
Copy link
Author

drugoi commented Jun 9, 2017

How to use:

  1. Save this .js file to your computer.
  2. Open .psd file then: File > Scripts > Browse… and choose count-layers-psd.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment