Copy link to clipboard
Copied
Using scriptlistener I can create a new layer called "newlayer" after the active layer. So far so good.
var id197 = charIDToTypeID( "Mk " );
var desc49 = new ActionDescriptor();
var id198 = charIDToTypeID( "null" );
var ref41 = new ActionReference();
var id199 = charIDToTypeID( "Lyr " );
ref41.putClass( id199 );
desc49.putReference( id198, ref41 );
var id200 = charIDToTypeID( "Usng" );
var desc50 = new ActionDescriptor();
var id201 = charIDToTypeID( "Nm " );
desc50.putString( id201, "newlayer" );
var id202 = charIDToTypeID( "Lyr " );
desc49.putObject( id200, id202, desc50 );
executeAction( id197, desc49, DialogModes.NO );
}
However, without the script listener we have
var layerRef = app.activeDocument.artLayers.add()
layerRef.name = newlayername
layerRef.blendMode = BlendMode.NORMAL
// Move the new layer after the current layer
layerRef.moveAfter(app.activeDocument.artLayers[layerIndex])
The moveAfter command needs an index, which would mean looping though all the layers till to find the active layer (since we can't access that throughthe DOM). This seems a bit long winded to me. Or am I missing a trick here?
Yes, you want to add a line before all of you above code such as:
var origLayer = docRef.activeLayer
Then you can say:
layerRef.moveAfter(origLayer)
Copy link to clipboard
Copied
Well I would just use the scriptlistener code but if you don't want to for some reason you should make a reference to the activeLayer before add the new layer. Then use that reference as the moveAfter argument. The moveAfter method needs a layer object not a layer index. You need the index only because you are trying to supply that layer object by index.
layerRef.moveAfter(someLayerRef);
Also ( not that it really matters ) when scripting was first added there were several methods for moving a layer, moveAfter() was one of those. Those methods were combined into one method and the old methods are no longer documented in the javascript reference. So below is the documented way to move the layer.
layerRef.move(someLayerRef, ElementPlacement.PLACEAFTER);
Copy link to clipboard
Copied
That all makes sense, but why can't you create a self-referential layer?
var layerRef = app.activeDocument.artLayers.add()
layerRef.name = "newlayername"
layerRef.blendMode = BlendMode.NORMAL
// Move the new layer after the current layer
layerRef.moveAfter(layerRef)
Copy link to clipboard
Copied
Wherever you go, there you are. Using the layer you want to move as the argument really says 'move to the layer below yourself''. If you want it to move in relation to another layer you need to use that layer as the argument.
Copy link to clipboard
Copied
"You can't get there from here". Ok - i'll watch out for duplicate layernames in this instance.
Copy link to clipboard
Copied
It's more like here and there are the same place when you self reference the layer. It's always a good idea to watch out for dupe layernames but as you need a layer object you don't have to reference by name. The code I posted doesn't care about names. Nor would using the suggestion of getting activeLayer index before the add as csuebele suggested.
Copy link to clipboard
Copied
Yes, you want to add a line before all of you above code such as:
var origLayer = docRef.activeLayer
Then you can say:
layerRef.moveAfter(origLayer)
Copy link to clipboard
Copied
Get the index of the layer before you create the new layer. When you create the new layer, it will be placed above the active layer, pushing the active layer down by one index number. So just add the appropiate index number when moving the layer.
Looks, Like Mike just beat me to the same answer, more or less.