Search code examples
photoshopextendscriptphotoshop-script

ScriptListener code creates new layer above active layer


I've used the scriptlistener to create a new layer. The clever thing it does is places it above the active layer (without moving it from the top after creation) However, I'm at a loss to work out which part of the code it is since they all get complied with executeAction:

function create_new_layer(layername)
{
    var idMk = charIDToTypeID( "Mk  " );
    var desc447 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
    var ref217 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    ref217.putClass( idLyr );
    desc447.putReference( idnull, ref217 );
    var idUsng = charIDToTypeID( "Usng" );
    var desc448 = new ActionDescriptor();
    var idNm = charIDToTypeID( "Nm  " );
    desc448.putString( idNm, layername );
    var idLyr = charIDToTypeID( "Lyr " );
    desc447.putObject( idUsng, idLyr, desc448 );
    var idLyrI = charIDToTypeID( "LyrI" );
    desc447.putInteger( idLyrI, 57 );
    executeAction( idMk, desc447, DialogModes.NO );
}

Solution

  • I think AM code represent actual functions in Photoshop, how they work inside, and DOM is some kind of overlay on top of that. Generally DOM code is limited and can't do a lot of things AM can. In this case ArtLayer.add() pushes a layer to activeDocument.layers so it appear on top of the layers stack, while AM command executes New Layer... command. Also generally AM code works much faster than DOM (up to 10-15x), especially with things like selecting layers and working with them.