Make a new doc, add some layers, then run the following to check for yourself:
Code: Select allfunction sTID(s) { return app.stringIDToTypeID(s); };
// Get index of the selected layer
function getLayerIndex()
{
index_actRef = new ActionReference;
index_actRef.putProperty(sTID("property"), sTID("itemIndex"));
index_actRef.putEnumerated(sTID("layer"), sTID("ordinal"), sTID("targetEnum"));
return app.executeActionGet(index_actRef).getInteger(sTID("itemIndex"));
}
// Get layer name from index
function getLayerNameByIndex(index)
{
var ref = new ActionReference();
ref.putProperty( sTID("property") , sTID( "name" ));
ref.putIndex( sTID( "layer" ), index);
alert("The layer index of the current layer is " + index );
return executeActionGet(ref).getString(sTID( "name" ));;
}
// Select layer by layer name
function selLayer(layerName)
{
var select = new ActionDescriptor();
var actRef = new ActionReference();
// Construct actRef, connect to select -actDesc
actRef.putName( sTID("layer"), layerName );
select.putReference( sTID("null"), actRef );
select.putBoolean( sTID("makeVisible"), false );
alert("The name associated with that index is " + layerName);
// Execute: select layer layerName
executeAction( sTID("select"), select, DialogModes.NO );
}
// Execute everything
var layerIndex = getLayerIndex();
var layerName = getLayerNameByIndex(layerIndex);
selLayer(layerName);
The first alert tells me that the index of the selected number is 1 (Background). Okay... I'm used to start counting from 0 but I know that sometimes counting can start from 1.
So that is passed along to the getLayerNameByIndex -function, in order to get the name of the layer by index 1.
The returned name is not the background -layer though, but the one on top according to the alert.
What is going on here??? Why does it appear that the counting starts from 1 when the first function executes, but then when the name is to be fetched, the counting starts from 0?? At first I thought that I had accidently incremented the index (++) but that's not the case.
Also, on a side note: How do I count layers in the open document? I want to store this value in a var so I can add some flow control to the script (so I can circle around)
Confused. Layer index starts counting from 0, AND 1?
-
tyr
Confused. Layer index starts counting from 0, AND 1?
I'm not scripting expert and have no experience with ActionReferences but I guess your functions somehow counts layers differently depending on presence of background layer.
I would use another method (indexes are from 0 and from top of the document)
Code: Select allvar doc = app.activeDocument;
var currentLayer = doc.activeLayer;
for(i=0; i < doc.layers.length; i++)
{
if(doc.layers==currentLayer)
{
alert('index = '+i)
}
}
How do I count layers in the open document?
Is that what you were asking?
Code: Select allapp.activeDocument.layers //all the layers
app.activeDocument.layers.length // the number of layers
I would use another method (indexes are from 0 and from top of the document)
Code: Select allvar doc = app.activeDocument;
var currentLayer = doc.activeLayer;
for(i=0; i < doc.layers.length; i++)
{
if(doc.layers==currentLayer)
{
alert('index = '+i)
}
}
How do I count layers in the open document?
Is that what you were asking?
Code: Select allapp.activeDocument.layers //all the layers
app.activeDocument.layers.length // the number of layers
-
pfaffenbichler
Confused. Layer index starts counting from 0, AND 1?
Code: Select allapp.activeDocument.layers.length // the number of layers
is DOM and disregards the Layers in Groups.
is DOM and disregards the Layers in Groups.
-
xbytor
Confused. Layer index starts counting from 0, AND 1?
Code: Select all// Get index of the selected layer
function getLayerIndex()
{
var index_actRef = new ActionReference;
index_actRef.putProperty(sTID("property"), sTID("itemIndex"));
index_actRef.putEnumerated(sTID("layer"), sTID("ordinal"), sTID("targetEnum"));
var idx = app.executeActionGet(index_actRef).getInteger(sTID("itemIndex"));
var doc = app.activeDocument;
var hasBackground = doc.layers[doc.layers.length-1].isBackgroundLayer;
return (hasBackground ? idx - 1 : idx);
}
This should return the correct index whether or not there is a background layer. I haven't tested it but did pull bits from working code in xtools/xlib/stdlib.js.
function getLayerIndex()
{
var index_actRef = new ActionReference;
index_actRef.putProperty(sTID("property"), sTID("itemIndex"));
index_actRef.putEnumerated(sTID("layer"), sTID("ordinal"), sTID("targetEnum"));
var idx = app.executeActionGet(index_actRef).getInteger(sTID("itemIndex"));
var doc = app.activeDocument;
var hasBackground = doc.layers[doc.layers.length-1].isBackgroundLayer;
return (hasBackground ? idx - 1 : idx);
}
This should return the correct index whether or not there is a background layer. I haven't tested it but did pull bits from working code in xtools/xlib/stdlib.js.
-
Mike Hale
Confused. Layer index starts counting from 0, AND 1?
To add a bit of explanation to the code posted, Action Manager uses the index of zero only for the background layer. If the document doesn't have a background layer the bottom layer has an index of one.
Another difference between the DOM and Action Manger when it comes to layer indexes is layerSets have two indexes. One for the start of the layerSet and one for the end( an invisible layer at the bottom of the set ).
Another difference between the DOM and Action Manger when it comes to layer indexes is layerSets have two indexes. One for the start of the layerSet and one for the end( an invisible layer at the bottom of the set ).