1

I am trying to build a script to "paste in place" all selected objects to all unlocked layers. My attempt is probably nearly there, but something's not working.

var myDoc = app.activeDocument;

var al=myDoc.activeLayer.name; // remembers current layer name
app.copy(); // copies whatever is selected

// paste in place for all unlocked layers, excepting source layer
for (var i=0;i<myDoc.layers.length;i++) {
    if ((myDoc.layers[i].locked=false) && (myDoc.layers[i].name!=al)) {
        myDoc.activeLayer=myDoc.layers[i];
        app.pasteInPlace();
    }
}
3

Your check if the layer is locked is missing a = (it's assigning rather than evaluating otherwise). It should be;

if ( myDoc.layers[i].locked == false ) ...

I just tested and the script works perfectly with that corrected.

The full working script is then:

var myDoc = app.activeDocument;

var al = myDoc.activeLayer.name; // remembers current layer name
app.copy(); // copies whatever is selected

// paste in place for all unlocked layers, excepting source layer
for ( var i = 0; i < myDoc.layers.length; i++ ) {
    if ( myDoc.layers[i].locked == false && myDoc.layers[i].name != al ) {
        myDoc.activeLayer = myDoc.layers[i];
        app.pasteInPlace();
    }
}
| improve this answer | |
  • Heah, i knew i must have missed something basic thing :)) thanks.. also added this to the question in case somebody googles this – Lucian Mar 7 '17 at 12:51
  • 2
    @Lucian no worries, you should really leave the incorrect code in the question though, the question doesn't really make sense if the code is working :) (I've added the full fixed code here though) – Cai Mar 7 '17 at 13:02
1

Variant script to paste in place selected objects on all other locked layers:

var myDoc = app.activeDocument;

var al=myDoc.activeLayer.name; // remembers current layer
app.copy(); // copies whatever is selected

for (var i=0;i<myDoc.layers.length;i++) {
    if ((myDoc.layers[i].locked==true) && (myDoc.layers[i].name!=al)) {
        myDoc.activeLayer=myDoc.layers[i]; // select next layer
        myDoc.layers[i].locked=false; // unlock it
        app.pasteInPlace(); // paste in place
        myDoc.layers[i].locked=true; // relock it   
    }
}

myDoc.activeLayer=al; // Go back to original  layer
| improve this answer | |
  • That is pasting to all locked layers, OP says "all unlocked layers" – Cai Mar 7 '17 at 12:58
  • Apparently Paste in place only works on UNLOCKED AND VISIBLE layers, so yes i just discovered this while editing. – Lucian Mar 7 '17 at 13:00
  • Oh I get it. This is why I suggested to just unlock the target layer, paste in place, then lock it back (if wanted) – Vinny Mar 7 '17 at 13:01
  • It is a good point. Let me rethink this :) – Lucian Mar 7 '17 at 13:02
  • 1
    @Lucian I does work but selecting layer and selecting object are 2 different things. If you check your layer panel, you'll see the active layer is the one you first selected your object but the object selected is on the last layer of your loop. In my variant, the object was unselected because layer was re-locked, so it wasn't an issue. On your variant, it's different. Just add app.selection = null; If you want to go back to the original selections, that would be a different story. – Vinny Mar 7 '17 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.