Loop Trough Nested Layers
Posted: Fri Jul 08, 2011 6:58 pm
Hi there people!
Here is a nice way to loop trough nested layers using the DOM.
The script is just counting the layers and layer sets and displaying the elapsed time but easily can be edited for other purposes.
One important thing is that the script is not going straight wich means that loops in the following way:
1) All document's layers
2) All main sets
3) Nested sets, nested sets …
P.S. People with CS2 will be lucky, its performing a lot faster in that version.
Code: Select all
// GLOBAL VARIABLES ====================================================================================== //
var docRef = app.activeDocument;
var docLys = docRef.layers.length; // All document's layers //
var allLys = 0; // All layers (counter) //
var allSts = 0; // All sets (counter) //
var actLay; // The active layer is going to be placed here //
var setArray = []; // Detected sets are goingto be placed here //
var cnt = 0; // Counter for the main layers loop //
// FUNCTIONS ============================================================================================ //
function cntTime (elap){ // Time Calculation //
tmArray = [
"Empty",
[ Math.floor ( (elap/(1000*60*60)) %24) ], // Hours //
[ Math.floor ( (elap/(1000*60)) % 60) ], // Minutes //
[ Math.floor ( (elap/1000) % 60 ) ] // Seconds //
]
for (i=1; i<=3; i++){ // Loop trough the values //
tmArray = (tmArray < 10 ? "0" : "") + tmArray // If the value is less than 10 // Add 0 //
}
}
// ==================================================================================================== //
function activeLay (lyr){
actLay = lyr
docRef.activeLayer = actLay // Set the active layer //
}
// ==================================================================================================== //
function setInArray (set){
if (set.typename == 'LayerSet'){ // Check active layer type //
allSts ++
setArray.push (set) // Place the set in the array //
}
else{
allLys ++
}
}
// ==================================================================================================== //
function loopInSets (set){ // Loop trough nested layers //
var length = set.layers.length-1 // All layers in the set //
for (i=0; i<=length; i++){
activeLay (set.layers) // Set active layer //
setInArray (actLay) // Place subsets in the array //
}
setArray.splice (0,1) // Remove the checked set from the array //
}
// GET STARTING TIME ================================================================================== //
var stDate = new Date () // Get starting date //
var stTime = stDate.getTime () // Get starting time //
// START PROCESS ====================================================================================== //
try{
while ( cnt !== docLys+1){
activeLay (docRef.layers[cnt]) // Set active layer //
setInArray (actLay) // Check for set //
cnt ++
}
}
catch (e){ // Catch error when doument`s layers end // Loop trough sets //
while ( setArray.length !==0 ){ // Loop until the array is not empty //
loopInSets (setArray[0]) // Always work with the 0 element in the array //
}
}
// ================================================================================================== //
// COUNT THE TIME =================================================================================== //
var ndDate = new Date (); // End date //
var ndTime = ndDate.getTime (); // End time //
var elapsed = ndTime - stTime; // Elapsed time //
var tmArray = ["Empty", ] // Time array //
cntTime (elapsed) // Count the time //
alert (
"Total Sets " + allSts + "\n\n"
+ "Total Layers: " + allLys + "\n\n"
+ "Time Elapsed: " + tmArray[2] + " : " + tmArray[3] // Изминало време //
+ " " , 'Statistic'
)
Here is a nice way to loop trough nested layers using the DOM.
The script is just counting the layers and layer sets and displaying the elapsed time but easily can be edited for other purposes.
One important thing is that the script is not going straight wich means that loops in the following way:
1) All document's layers
2) All main sets
3) Nested sets, nested sets …
P.S. People with CS2 will be lucky, its performing a lot faster in that version.
Code: Select all
// GLOBAL VARIABLES ====================================================================================== //
var docRef = app.activeDocument;
var docLys = docRef.layers.length; // All document's layers //
var allLys = 0; // All layers (counter) //
var allSts = 0; // All sets (counter) //
var actLay; // The active layer is going to be placed here //
var setArray = []; // Detected sets are goingto be placed here //
var cnt = 0; // Counter for the main layers loop //
// FUNCTIONS ============================================================================================ //
function cntTime (elap){ // Time Calculation //
tmArray = [
"Empty",
[ Math.floor ( (elap/(1000*60*60)) %24) ], // Hours //
[ Math.floor ( (elap/(1000*60)) % 60) ], // Minutes //
[ Math.floor ( (elap/1000) % 60 ) ] // Seconds //
]
for (i=1; i<=3; i++){ // Loop trough the values //
tmArray = (tmArray < 10 ? "0" : "") + tmArray // If the value is less than 10 // Add 0 //
}
}
// ==================================================================================================== //
function activeLay (lyr){
actLay = lyr
docRef.activeLayer = actLay // Set the active layer //
}
// ==================================================================================================== //
function setInArray (set){
if (set.typename == 'LayerSet'){ // Check active layer type //
allSts ++
setArray.push (set) // Place the set in the array //
}
else{
allLys ++
}
}
// ==================================================================================================== //
function loopInSets (set){ // Loop trough nested layers //
var length = set.layers.length-1 // All layers in the set //
for (i=0; i<=length; i++){
activeLay (set.layers) // Set active layer //
setInArray (actLay) // Place subsets in the array //
}
setArray.splice (0,1) // Remove the checked set from the array //
}
// GET STARTING TIME ================================================================================== //
var stDate = new Date () // Get starting date //
var stTime = stDate.getTime () // Get starting time //
// START PROCESS ====================================================================================== //
try{
while ( cnt !== docLys+1){
activeLay (docRef.layers[cnt]) // Set active layer //
setInArray (actLay) // Check for set //
cnt ++
}
}
catch (e){ // Catch error when doument`s layers end // Loop trough sets //
while ( setArray.length !==0 ){ // Loop until the array is not empty //
loopInSets (setArray[0]) // Always work with the 0 element in the array //
}
}
// ================================================================================================== //
// COUNT THE TIME =================================================================================== //
var ndDate = new Date (); // End date //
var ndTime = ndDate.getTime (); // End time //
var elapsed = ndTime - stTime; // Elapsed time //
var tmArray = ["Empty", ] // Time array //
cntTime (elapsed) // Count the time //
alert (
"Total Sets " + allSts + "\n\n"
+ "Total Layers: " + allLys + "\n\n"
+ "Time Elapsed: " + tmArray[2] + " : " + tmArray[3] // Изминало време //
+ " " , 'Statistic'
)