Skip to main content
Inspiring
July 8, 2010
Question

Send backwards

  • July 8, 2010
  • 1 reply
  • 1151 views

Hi. How do you code it in javascript to send a layer backwards, as in Layer > arrange > send backward?  It#s part of a simple script that has two layers and I need to arrange them so the new layer moves from the top to underneath the other layer.

I've got it as the following, but I'm after something cleaner

// =======================================================
var id122 = charIDToTypeID( "move" );
    var desc28 = new ActionDescriptor();
    var id123 = charIDToTypeID( "null" );
        var ref10 = new ActionReference();
        var id124 = charIDToTypeID( "Lyr " );
        var id125 = charIDToTypeID( "Ordn" );
        var id126 = charIDToTypeID( "Trgt" );
        ref10.putEnumerated( id124, id125, id126 );
    desc28.putReference( id123, ref10 );
    var id127 = charIDToTypeID( "T   " );
        var ref11 = new ActionReference();
        var id128 = charIDToTypeID( "Lyr " );
        var id129 = charIDToTypeID( "Ordn" );
        var id130 = charIDToTypeID( "Prvs" );
        ref11.putEnumerated( id128, id129, id130 );
    desc28.putReference( id127, ref11 );
executeAction( id122, desc28, DialogModes.NO );

Cheers

This topic has been closed for replies.

1 reply

Paul Riggott
Inspiring
July 8, 2010

You could create a function IE:


moveLayerUpOrDown("Down"); // "UP" or "DOWN"

function moveLayerUpOrDown(Direction) {
switch(Direction.toLowerCase()){
    case 'up' : Direction = 'Nxt '; break;
    case 'down' : Direction = 'Prvs'; break;
    default : Direction = 'Prvs'; break;
}
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID('Lyr '),charIDToTypeID('Ordn'),charIDToTypeID('Trgt') );
desc.putReference(charIDToTypeID('null'), ref );
var ref2 = new ActionReference();
ref2.putEnumerated(charIDToTypeID('Lyr '),charIDToTypeID('Ordn'),charIDToTypeID(Direction) );
try{
desc.putReference(charIDToTypeID('T   '), ref2 ); executeAction(charIDToTypeID('move'), desc, DialogModes.NO );
}catch(e){}
}

Inspiring
July 8, 2010

You could also add"Back" and "Frnt" for 'send to back' and 'send to front'.

About the best you can do with scriptlistner log output is clean it up and turn it into a function like in Paul's post. It may never be as 'clean' as a DOM method but it is often the only way to do something. Or it is either faster or eaiser than the DOM.

You can move layers using the DOM but you need to create layer references and take layerSets into account. The advantage of using the DOM is if you know the layer structure you can move the layer anywhere in the layer stack.

var lyr1 = app.activeDocument.artLayers[1];
var lyr2 = app.activeDocument.artLayers[2];
lyr1.move(lyr2, ElementPlacement.PLACEAFTER);// same as menu item send backward in this case

GhoulfoolAuthor
Inspiring
July 8, 2010

Michael,

Thanks. the DOM version makes more sense to me. But I'm still a bit confused as to why, in order to get it to work I had to change the art layers to 0 and 1. Which now works (with or without an extra background layer in) Is that anything to do with layersets?? - (which I've not covered yet BTW)

var lyr1 = app.activeDocument.artLayers[0];
var lyr2 = app.activeDocument.artLayers[1];
lyr1.move(lyr2, ElementPlacement.PLACEAFTER);