duplicate and movebefore layerSelected

Community Beginner ,
Sep 21, 2022 Sep 21, 2022

Copy link to clipboard

Copied

Hi everyone

I want to duplicate selected layers
Then put the duplicated layers on top of the selected layers

I tried it for an hour but it was all messed up
can someone help me, thanks a lot

var comp  = app.project.activeItem; 
var layers= comp.selectedLayers;
var numLayers = layers.length;
        if (numLayers > 0) {
            for (var i = 0; i < numLayers; i++) { 
                layers[i].duplicate();
                layers[i].moveAfter(comp.layer(layers[i].index+1));
            }
    }

 

image_2022-09-22_105353672.png

TOPICS
Scripting

Views

49

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 21, 2022 Sep 21, 2022

Copy link to clipboard

Copied

I did it but it's still not ok

var comp  = app.project.activeItem; 
var layers= comp.selectedLayers;
var numLayers = layers.length;
var myArray=[];
app.beginUndoGroup("Set Marker");
if (numLayers > 0) {
    for (var i = 0; i < numLayers; i++) { 
        myArray.push(layers[i].name); //Get layer name selected
    }
    for (var i = 0; i < myArray.length; i++) { 
        comp.layer(myArray[i]).duplicate(); // Duplicate
    }
    var indexLayer=comp.layer(myArray[0]).index; // Get index
    for (var i = 0; i < myArray.length; i++) { 
        comp.layer(myArray[i]).moveBefore(comp.layer(indexLayer-1));
    }
    }
    app.endUndoGroup();

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

LATEST

I would not recommend to rely on layer names - this always causes trouble when you have multiple layers with the same name. Better follow the logic of my Automation Blocks example.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 21, 2022 Sep 21, 2022

Copy link to clipboard

Copied

Why do you create a new loop everytime? Of course your variable i will not carry over and the index be all messed up because after the duplication it shifts by +1. Do it all in one.

 

Mylenium

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

Here is an implementation with Automation Blocks:Screenshot 2022-09-22 at 09.40.47.png

 

You also find it now in our Community Library at

Layer/Duplicate/Duplicate Selected Layers and Move Above First Selected Layer

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

Make your promotional social content with Adobe Express.

Get started quickly with free templates: