6 Replies Latest reply on Aug 25, 2013 8:46 PM by lilsmokie

    Duplicate Layers to new composition?  Weird problem with duplicate() command.

    lilsmokie Level 1

      So I am trying to write a script that grabs selected layers and throws them into a new document that matches the origional.

       

      But something strange is happening when I use duplicate several times in a row.  It duplicates my duplicated layers.

      I start with this selection:

      origional selection.PNG

       

      But end up with this result in my new doc:

      resulting problem.PNG

       

       

       

      This has me very confused.  I would appreciate someone looking at what I have.

      Thanks for looking.

       

       

       

       

       

       

      Here is the code:

       

      var FSTdoc = app.activeDocument;

      var curPATH = app.activeDocument.path+'/';

      var fullNAME = app.activeDocument.fullName;

      var  ABSOfull = fullNAME.absoluteURI;

      var curPATHstr = String(curPATH);

      var fullNAMEstr = String(fullNAME);

       

       

       

      var theLayers = GetSelectedLayers();

       

       

      var HLDRdoc = app.documents.add(FSTdoc.width, FSTdoc.height, FSTdoc.resolution, FSTdoc.name, NewDocumentMode.RGB, FSTdoc.initialFill,FSTdoc.pixelAspectRatio, FSTdoc.bitsPerChannel, FSTdoc.colorProfileName);

       

       

      app.activeDocument = FSTdoc;

       

      lay1 = theLayers[0].duplicate(HLDRdoc, ElementPlacement. PLACEATBEGINNING);

      lay2 = theLayers[1].duplicate( HLDRdoc, ElementPlacement. PLACEATBEGINNING);

      lay3 = theLayers[2].duplicate(HLDRdoc, ElementPlacement. PLACEATBEGINNING);

       

       

       

      // function to get selected layers by paul mr;

      function GetSelectedLayers() {

      var A=[];

          var desc11 = new ActionDescriptor();

              var ref9 = new ActionReference();

              ref9.putClass( stringIDToTypeID('layerSection') );

          desc11.putReference( charIDToTypeID('null'), ref9 );

              var ref10 = new ActionReference();

              ref10.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

          desc11.putReference( charIDToTypeID('From'), ref10 );

          // try catch to stop script

          try {

          executeAction( charIDToTypeID('Mk  '), desc11, DialogModes.NO );

          } catch (err) {

                  alert("Script will not work on locked layers.");

                  throw new Error("Locked Layer");

              }

      var gL = activeDocument.activeLayer.layers;

      for(var i=0;i<gL.length;i++){

      A.push(gL[i]);

      }

      executeAction( charIDToTypeID('undo'), undefined, DialogModes.NO );

      return A;

      };