Created
June 9, 2017 05:59
-
-
Save drugoi/8a46a0d5b7ec3b217a011e2336a4b9c4 to your computer and use it in GitHub Desktop.
Script for counting layers in PSD file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use: