Hey guys, I've been working on this on and off trying to find a way to make it work. I have a solution, but I'm not sure it's the greatest. Any ideas? I looked around some and found a few posts, but no answers.
Code: Select allvar docRef = app.activeDocument;
function recurseLayers(currLayers)
{
for(var i = 0; i < currLayers.layers.length; i++)
{
alert(currLayers.layers);
//test if it's a layer set
if(isLayerSet(currLayers.layers))
{
recurseLayers(currLayers.layers);
}
}
}
//a test for a layer set
function isLayerSet(layer)
{
try{
if(layer.layers.length > 0)
return true;}
catch(err){
return false;}
}
recurseLayers(docRef);
Recursive Layer Exploration
Recursive Layer Exploration
Code: Select allfunction collectLayersByDOM( theParent ) {
if (!allLayers) {
var allLayers = new Array();
};
else {};
for ( var m = theParent.layers.length - 1; m >= 0; m-- ) {
var theLayer = theParent.layers[ m ];
if ( theLayer.typename != "LayerSet" ) {
try{
allLayers = allLayers.concat( theLayer );
}catch(e){}
}
else {
// apply the function to layersets;
//allLayers = allLayers.concat( theLayer );// uncomment to include layersets in array
allLayers = allLayers.concat( collectLayersByDOM( theLayer ) );
};
};
return allLayers
};
collectLayersByDOM(app.activeDocument );
if (!allLayers) {
var allLayers = new Array();
};
else {};
for ( var m = theParent.layers.length - 1; m >= 0; m-- ) {
var theLayer = theParent.layers[ m ];
if ( theLayer.typename != "LayerSet" ) {
try{
allLayers = allLayers.concat( theLayer );
}catch(e){}
}
else {
// apply the function to layersets;
//allLayers = allLayers.concat( theLayer );// uncomment to include layersets in array
allLayers = allLayers.concat( collectLayersByDOM( theLayer ) );
};
};
return allLayers
};
collectLayersByDOM(app.activeDocument );
Recursive Layer Exploration
bb/viewtopic.php?f=5&t=828&p=15595&hili ... cd3#p15674
that code is much faster!
Code: Select all function getAllLayersByIndex(){
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 hasBackground() {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr"), charIDToTypeID( "Bckg" ));
ref.putEnumerated(charIDToTypeID( "Lyr " ),charIDToTypeID( "Ordn" ),charIDToTypeID( "Back" ))//bottom Layer/background
var desc = executeActionGet(ref);
var res = desc.getBoolean(charIDToTypeID( "Bckg" ));
return res
};
function getLayerType(idx,prop) {
var ref = new ActionReference();
ref.putIndex(charIDToTypeID( "Lyr " ), idx);
var desc = executeActionGet(ref);
var type = desc.getEnumerationValue(prop);
var res = typeIDToStringID(type);
return res
};
function getLayerVisibilityByIndex( idx ) {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Vsbl" ));
ref.putIndex( charIDToTypeID( "Lyr " ), idx );
return executeActionGet(ref).getBoolean(charIDToTypeID( "Vsbl" ));;
};
var cnt = getNumberLayers()+1;
var res = new Array();
if(hasBackground()){
var i = 0;
}else{
var i = 1;
};
var prop = stringIDToTypeID("layerSection")
for(i;i<cnt;i++){
var temp = getLayerType(i,prop);
if(temp != "layerSectionEnds") res.push(i);
};
return res;
};
function makeActiveByIndex( idx, visible ){
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIndex(charIDToTypeID( "Lyr " ), idx)
desc.putReference( charIDToTypeID( "null" ), ref );
desc.putBoolean( charIDToTypeID( "MkVs" ), visible );
executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
};
function getLayerBoundsByIndex( idx ) {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID( "bounds" ));
ref.putIndex( charIDToTypeID( "Lyr " ), idx );
var desc = executeActionGet(ref).getObjectValue(stringIDToTypeID( "bounds" ));
var bounds = [];// array of Numbers as pixels regardless of ruler
bounds.push(desc.getUnitDoubleValue(stringIDToTypeID('top')));
bounds.push(desc.getUnitDoubleValue(stringIDToTypeID('left')));
bounds.push(desc.getUnitDoubleValue(stringIDToTypeID('bottom')));
bounds.push(desc.getUnitDoubleValue(stringIDToTypeID('right')));
return bounds;
}
function getLayerInformation(layer){
this.layerWidth = layer.bounds[2].value - layer.bounds[0].value;
this.layerHeight = layer.bounds[3].value - layer.bounds[1].value;
return this;
}
function main(){
var layers = getAllLayersByIndex();
for (var i=0 ; i < layers.length ; i++) {
//var b = getLayerBoundsByIndex(layers);
// var width = b[2]-b[0];
// var height = b[3]-b[1];
makeActiveByIndex( layers, false );
var layer = activeDocument.activeLayer;
var layerBounds = getLayerInformation(layer);
//var layerBounds = layer.bounds;
// var height = layerBounds.layerHeight;
// var width = layerBounds.layerWidth;
}
}
var t = new Timer();
main();
t.getElapsed();
///////////////////////////////////////////////////////////////////////////////
// Object: Timer
// Usage: Time how long things take or delay script execution
// Input: <none>
// Return: Timer object
// Example:
//
// var a = new Timer();
// for (var i = 0; i < 2; i++)
// a.pause(3.33333);
// a.getElapsed();
// jeff tranberry
///////////////////////////////////////////////////////////////////////////////
function Timer() {
// member properties
this.startTime = new Date();
this.endTime = new Date();
// member methods
// reset the start time to now
this.start = function () { this.startTime = new Date(); }
// reset the end time to now
this.stop = function () { this.endTime = new Date(); }
// get the difference in milliseconds between start and stop
this.getTime = function () { return (this.endTime.getTime() - this.startTime.getTime()) / 1000; }
// get the current elapsed time from start to now, this sets the endTime
this.getElapsed = function () { this.endTime = new Date(); return this.getTime(); }
// pause for this many seconds
this.pause = function ( inSeconds ) {
var t = 0;
var s = new Date();
while( t < inSeconds ) {
t = (new Date().getTime() - s.getTime()) / 1000;
}
}
}
that code is much faster!
Code: Select all function getAllLayersByIndex(){
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 hasBackground() {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr"), charIDToTypeID( "Bckg" ));
ref.putEnumerated(charIDToTypeID( "Lyr " ),charIDToTypeID( "Ordn" ),charIDToTypeID( "Back" ))//bottom Layer/background
var desc = executeActionGet(ref);
var res = desc.getBoolean(charIDToTypeID( "Bckg" ));
return res
};
function getLayerType(idx,prop) {
var ref = new ActionReference();
ref.putIndex(charIDToTypeID( "Lyr " ), idx);
var desc = executeActionGet(ref);
var type = desc.getEnumerationValue(prop);
var res = typeIDToStringID(type);
return res
};
function getLayerVisibilityByIndex( idx ) {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Vsbl" ));
ref.putIndex( charIDToTypeID( "Lyr " ), idx );
return executeActionGet(ref).getBoolean(charIDToTypeID( "Vsbl" ));;
};
var cnt = getNumberLayers()+1;
var res = new Array();
if(hasBackground()){
var i = 0;
}else{
var i = 1;
};
var prop = stringIDToTypeID("layerSection")
for(i;i<cnt;i++){
var temp = getLayerType(i,prop);
if(temp != "layerSectionEnds") res.push(i);
};
return res;
};
function makeActiveByIndex( idx, visible ){
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIndex(charIDToTypeID( "Lyr " ), idx)
desc.putReference( charIDToTypeID( "null" ), ref );
desc.putBoolean( charIDToTypeID( "MkVs" ), visible );
executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
};
function getLayerBoundsByIndex( idx ) {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID( "bounds" ));
ref.putIndex( charIDToTypeID( "Lyr " ), idx );
var desc = executeActionGet(ref).getObjectValue(stringIDToTypeID( "bounds" ));
var bounds = [];// array of Numbers as pixels regardless of ruler
bounds.push(desc.getUnitDoubleValue(stringIDToTypeID('top')));
bounds.push(desc.getUnitDoubleValue(stringIDToTypeID('left')));
bounds.push(desc.getUnitDoubleValue(stringIDToTypeID('bottom')));
bounds.push(desc.getUnitDoubleValue(stringIDToTypeID('right')));
return bounds;
}
function getLayerInformation(layer){
this.layerWidth = layer.bounds[2].value - layer.bounds[0].value;
this.layerHeight = layer.bounds[3].value - layer.bounds[1].value;
return this;
}
function main(){
var layers = getAllLayersByIndex();
for (var i=0 ; i < layers.length ; i++) {
//var b = getLayerBoundsByIndex(layers);
// var width = b[2]-b[0];
// var height = b[3]-b[1];
makeActiveByIndex( layers, false );
var layer = activeDocument.activeLayer;
var layerBounds = getLayerInformation(layer);
//var layerBounds = layer.bounds;
// var height = layerBounds.layerHeight;
// var width = layerBounds.layerWidth;
}
}
var t = new Timer();
main();
t.getElapsed();
///////////////////////////////////////////////////////////////////////////////
// Object: Timer
// Usage: Time how long things take or delay script execution
// Input: <none>
// Return: Timer object
// Example:
//
// var a = new Timer();
// for (var i = 0; i < 2; i++)
// a.pause(3.33333);
// a.getElapsed();
// jeff tranberry
///////////////////////////////////////////////////////////////////////////////
function Timer() {
// member properties
this.startTime = new Date();
this.endTime = new Date();
// member methods
// reset the start time to now
this.start = function () { this.startTime = new Date(); }
// reset the end time to now
this.stop = function () { this.endTime = new Date(); }
// get the difference in milliseconds between start and stop
this.getTime = function () { return (this.endTime.getTime() - this.startTime.getTime()) / 1000; }
// get the current elapsed time from start to now, this sets the endTime
this.getElapsed = function () { this.endTime = new Date(); return this.getTime(); }
// pause for this many seconds
this.pause = function ( inSeconds ) {
var t = 0;
var s = new Date();
while( t < inSeconds ) {
t = (new Date().getTime() - s.getTime()) / 1000;
}
}
}