Skip to main content
Inspiring
April 26, 2022
Question

How to copy the shape layer contents to another shape layer by scripting

  • April 26, 2022
  • 2 replies
  • 944 views

I want to copy the shape layer contents to another shape layer by scripting.

 

I tried to this by using

app.executeCommand(19); // Copy
and
app.executeCommand(20); // Paste
 
but this way not work in some environments.
 
Please help me.
This topic has been closed for replies.

2 replies

Mylenium
Legend
April 27, 2022

Both your functions are in the same loop and will constantly interfere with each other by toggling the selection state. Why not simply create the new layer after the copy and then insert the content i a structured why into the default shape group?

 

Mylenium

Inspiring
April 27, 2022

it mean this?

      for (var i = 0; i < Object.keys(contents).length; i++) {
        for (var j = 1; j < contents[Object.keys(contents)[i]].length; j++) {
          deselectAll(comp);
          contents[Object.keys(contents)[i]][j].selected = true;
          app.executeCommand(19);
        }
      }

      var newShapeLayer = comp.layers.addShape();

      for (var i = 0; i < Object.keys(contents).length; i++) {
        var shapeGroup = newShapeLayer.property('ADBE Root Vectors Group').addProperty('ADBE Vector Group')
        shapeGroup.name = Object.keys(contents)[i];
        deselectAll(comp);
        shapeGroup.selected = true;
        app.executeCommand(20);
      }

 

this will paste only last one copied content

Mylenium
Legend
April 26, 2022

And what is the rest of the code around it? Of course none of this will do anything without checking whether a layer is correctly selected, what type it is, in which comp it is and so on. If that's all you have, this will never work.

 

Mylenium

Inspiring
April 26, 2022
  function shapeMerge() {
    try {
      var comp = app.project.activeItem
      var selectedLayers = comp.selectedLayers;

      var contents = {};
      for (var i = 0; i < selectedLayers.length; i++) {
        var selectedLayer = selectedLayers[i];
        contents[selectedLayer.name] = []
        for (var j = 1; j <= selectedLayer.property("ADBE Root Vectors Group").numProperties; j++) {
          contents[selectedLayer.name][j] = selectedLayer.property("ADBE Root Vectors Group")(j);
        }
      }

      var newShapeLayer = comp.layers.addShape();

      for (var i = 0; i < Object.keys(contents).length; i++) {
        var shapeGroup = newShapeLayer.property('ADBE Root Vectors Group').addProperty('ADBE Vector Group')
        shapeGroup.name = Object.keys(contents)[i];

        for (var j = 1; j < contents[Object.keys(contents)[i]].length; j++) {
          deselectAll(comp);
          contents[Object.keys(contents)[i]][j].selected = true;
          app.executeCommand(19);

          deselectAll(comp);
          shapeGroup.selected = true;
          app.executeCommand(20);
        }
      }

      newShapeLayer.moveBefore(getTopLayer(selectedLayers));

      for (var i = 0; i < selectedLayers.length; i++) {
        var selectedLayer = selectedLayers[i];
        selectedLayer.enabled = false;
      }
    } catch (e) {
      alert(e)
    }
  }

  function deselectAll(theComp) {
    for (var i = 1; i <= theComp.numLayers; i++) {
      theComp.layer(i).selected = false;
    }
    var theProps = theComp.selectedProperties;
    for (var i = 0; i < theProps.length; i++) {
      theProps[i].selected = false;
    }
  }