7 Replies Latest reply on Feb 8, 2014 3:52 AM by c.pfaffenbichler

    I need a script to replace layers. PS CS5

    MShokeir

      Ok, let's say I need to replace layer (x) with layer (y), of course, Layer (y) is not in the composition.


      (NOTE: layer (y) is a Vector Smart Layer while layer (x) is a regular layer named "Layer 4")

      And yes each Layer (x) is named "Layer 4"


      The Problem is ... There are 59 of layer (x) in the composition, and I need to replace each and everyone with layer (y)!


      I would do it manually although it's time consuming, but there's also the problem that the PC slows down as I open the composition because it is 732MB and 900+ Layers, So I figured out the best way to do it is with a script, But unfortunately I have no idea how to type a script and when I googled it, I found 1 Script that can do so but it did not really work!

       

      The one I tried appeared as so,

       

      var prefix = "so_";
      var assetPath = "assets/"
      
      
      var count = 0;
      listLayersets(app.activeDocument);
      listLayers(app.activeDocument);
      var path;
      
      
      function listLayersets(pointer) {
           for (var i = 0; i < pointer.layerSets.length; i++) {
              var layerset = pointer.layerSets[i];
              listLayers (layerset);
          }
      }
      
      
      function listLayers(pointer) {
          for (var i = 0; i < pointer.artLayers.length; i++) {
              var layer = pointer.artLayers[i];
              if (layer.name.indexOf(prefix) == 0) {
                  if (layer.kind == "LayerKind.SMARTOBJECT") {
                      app.activeDocument.activeLayer = layer;
                      replaceSO (app.activeDocument.path + "/" + assetPath + layer.name + ".psb");
                  }
               }
          }
      }
      
      
      function replaceSO (newFile) {
                var id = stringIDToTypeID( "placedLayerReplaceContents" );
          var desc = new ActionDescriptor();
          var idn = charIDToTypeID("null");
          desc.putPath( idn, new File(newFile));
          var idp = charIDToTypeID("PgNm");
          desc.putInteger(idp, 1);
                executeAction(id, desc, DialogModes.NO);
                return app.activeDocument.activeLayer
      };
      
      

       

      I don't really understand most of that, but all I got was that I need to layer x to be renamed as so_layer (x) and layer (y) needed to be renamed as so_layer (y), also layer (y) was needed to be put in a folder named "assets" ,but unfortunatly I can't rename them all because they are alot and mixed up between many files and folders, not to mention the slow PC.

       

      The result from a prototype file, was a faliure, no message, no nothing

        • 1. Re: I need a script to replace layers. PS CS5
          c.pfaffenbichler Level 9
          while layer (x) is a regular layer named "Layer 4")

           

          The line

          if (layer.kind == "LayerKind.SMARTOBJECT") {

          checks for Smart Objects.

          If your Layers are not SOs then naturally the Script can not replace them currently.

           

          Edit: Also I think the Script only goes one Group deep, so Groups in Groups would not be considered.

          • 2. Re: I need a script to replace layers. PS CS5
            MShokeir Level 1

            What is the edit needed to complete my task?

            • 3. Re: I need a script to replace layers. PS CS5
              MShokeir Level 1

              This is BASICALLY what I want a script to do, I know this seams stupid, I never really drew a flow chart before but there it is!

              fgfg.jpg

              • 4. Re: I need a script to replace layers. PS CS5
                c.pfaffenbichler Level 9

                What is the edit needed to complete my task?

                You could convert the Layers to SOs, then replace them, but that would result in many individual SOs.

                 

                As for your flow chart: What good are the temp files supposed to be?

                 

                How experienced are you with JavaScript?

                Because quite frankly what you have in mind may be a bit involved for beginners.

                • 5. Re: I need a script to replace layers. PS CS5
                  zorodude

                  Hey MShokeir,

                   

                  I think that script you found is actually kind of the opposite of what you want. You mentioned that 'Layer x', the one that appears 59 times in your document  is a regular layer, and the one that you are replacing it WITH is a smart object. Correct ?

                   

                  Judging by your flow chart this might be a bit more involved than it seemed at first glance. Good news for you, I've been working extensively on JS for Photoshop for the last several months at work. What you're asking is not that difficult, it just requires clarification about the structure of your document (particularly its complexity) and what your desired outputs are.

                   

                  If you have time this weekend to explain, I can write you a script.

                  • 6. Re: I need a script to replace layers. PS CS5
                    MShokeir Level 1

                    I appreciate your generosity but I could not wait since yesterday, that's why I already completed my task manually!

                    Thank you for your consideration, But I may require your assistance in another task in the future!

                    • 7. Re: I need a script to replace layers. PS CS5
                      c.pfaffenbichler Level 9

                      Could you give this a try?

                      // replace layers of a certain name with instances of one smart object;

                      // 2014, use it at your own risk;

                      #target photoshop

                      if (app.documents.length > 0) {

                      app.activeDocument.suspendHistory("replace", "main ()");

                      };

                      ////////////////////////////////////

                      function main () {

                      var originalUnits = app.preferences.rulerUnits;

                      app.preferences.rulerUnits = Units.PIXELS;

                      // the name to search for;

                      var searchName = "Layer 4";

                      var myDocument = app.activeDocument;

                      var theArray = new Array;

                      // select file;

                      var aFile = selectFile(false);

                      if (aFile != null) {

                      // get number of layers;

                      var ref = new ActionReference();

                      ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

                      var applicationDesc = executeActionGet(ref);

                      var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));

                      // process the layers;

                      for (var m = 0; m <= theNumber; m++) {

                      try {

                      var ref = new ActionReference();

                      ref.putIndex( charIDToTypeID( "Lyr " ), m);

                      var layerDesc = executeActionGet(ref);

                      var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));

                      var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));

                      // if not layer group collect values;

                      if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {

                      var theName = layerDesc.getString(stringIDToTypeID('name'));

                      if (theName == searchName) {

                                var boundsDesc = layerDesc.getObjectValue(stringIDToTypeID('bounds'));

                                var theTop = boundsDesc.getUnitDoubleValue(stringIDToTypeID('top'));

                                var theLeft = boundsDesc.getUnitDoubleValue(stringIDToTypeID('left'));

                                var theWidth = boundsDesc.getUnitDoubleValue(stringIDToTypeID('width'));

                                var theHeight = boundsDesc.getUnitDoubleValue(stringIDToTypeID('height'));

                                var theCenter = [theLeft+theWidth/2, theTop+theHeight/2];

                                selectLayerByIndex(m,false);

                                theArray.push([myDocument.activeLayer, theCenter, theWidth, theHeight])

                                };

                      };

                      }

                      catch (e) {};

                      };

                      // replace with so;

                      var theSO = placeScaleRotateFile (aFile, 0, 0, 100, 100, 0);

                      var ref = new ActionReference();

                      ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

                      var layerDesc = executeActionGet(ref);

                      var boundsDesc = layerDesc.getObjectValue(stringIDToTypeID('bounds'));

                      var theTop = boundsDesc.getUnitDoubleValue(stringIDToTypeID('top'));

                      var theLeft = boundsDesc.getUnitDoubleValue(stringIDToTypeID('left'));

                      var theWidth = boundsDesc.getUnitDoubleValue(stringIDToTypeID('width'));

                      var theHeight = boundsDesc.getUnitDoubleValue(stringIDToTypeID('height'));

                      var theCenter = [theLeft+theWidth/2, theTop+theHeight/2];

                      for (var n = 0; n < theArray.length; n++) {

                      duplicateMoveAndScale (myDocument, theSO, 0, (theArray[n][1][0] - theCenter[0]), (theArray[n][1][1] - theCenter[1]), 100, 100, theArray[n][0]);

                      theArray[n][0].remove();

                      };

                      theSO.remove();

                      };

                      app.preferences.rulerUnits = originalUnits;

                      };

                      // by mike hale, via paul riggott;

                      // http://forums.adobe.com/message/1944754#1944754

                      function selectLayerByIndex(index,add){

                      add = undefined ? add = false:add

                      var ref = new ActionReference();

                          ref.putIndex(charIDToTypeID("Lyr "), index);

                          var desc = new ActionDescriptor();

                          desc.putReference(charIDToTypeID("null"), ref );

                             if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );

                            desc.putBoolean( charIDToTypeID( "MkVs" ), false );

                         try{

                          executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );

                      }catch(e){

                      alert(e.message);

                      }

                      };

                      ////// place //////

                      function placeScaleRotateFile (file, xOffset, yOffset, theXScale, theYScale, theAngle) {

                      // =======================================================

                      var idPlc = charIDToTypeID( "Plc " );

                          var desc5 = new ActionDescriptor();

                          var idnull = charIDToTypeID( "null" );

                          desc5.putPath( idnull, new File( file ) );

                          var idFTcs = charIDToTypeID( "FTcs" );

                          var idQCSt = charIDToTypeID( "QCSt" );

                          var idQcsa = charIDToTypeID( "Qcsa" );

                          desc5.putEnumerated( idFTcs, idQCSt, idQcsa );

                          var idOfst = charIDToTypeID( "Ofst" );

                              var desc6 = new ActionDescriptor();

                              var idHrzn = charIDToTypeID( "Hrzn" );

                              var idPxl = charIDToTypeID( "#Pxl" );

                              desc6.putUnitDouble( idHrzn, idPxl, xOffset );

                              var idVrtc = charIDToTypeID( "Vrtc" );

                              var idPxl = charIDToTypeID( "#Pxl" );

                              desc6.putUnitDouble( idVrtc, idPxl, yOffset );

                          var idOfst = charIDToTypeID( "Ofst" );

                          desc5.putObject( idOfst, idOfst, desc6 );

                          var idWdth = charIDToTypeID( "Wdth" );

                          var idPrc = charIDToTypeID( "#Prc" );

                          desc5.putUnitDouble( idWdth, idPrc, theYScale );

                          var idHght = charIDToTypeID( "Hght" );

                          var idPrc = charIDToTypeID( "#Prc" );

                          desc5.putUnitDouble( idHght, idPrc, theXScale );

                          var idAngl = charIDToTypeID( "Angl" );

                          var idAng = charIDToTypeID( "#Ang" );

                          desc5.putUnitDouble( idAngl, idAng,theAngle );

                          var idLnkd = charIDToTypeID( "Lnkd" );

                          desc5.putBoolean( idLnkd, true );

                      executeAction( idPlc, desc5, DialogModes.NO );

                      return app.activeDocument.activeLayer;

                      };

                      ////// select file //////

                      function selectFile (multi) {

                      if (multi == true) {var theString = "please select files"}

                      else {var theString = "please select one file"};

                      if ($.os.search(/windows/i) != -1) {var theFiles = File.openDialog (theString, '*.jpg;*.tif;*.psd;*.ai;*.eps', multi)}

                      else {var theFiles = File.openDialog (theString, getFiles, multi)};

                      ////// filter files  for mac //////

                      function getFiles (theFile) {

                          if (theFile.name.match(/\.(jpg|tif|psd|eps|ai)$/i) || theFile.constructor.name == "Folder") {

                              return true

                              };

                                };

                      return theFiles

                      };

                      ////// rotate and move //////

                      function duplicateMoveAndScale (myDocument, theLayer, thisAngle, horizontalOffset, verticalOffset, horScale, verScale, placeBefore) {

                      // duplicate;

                      var thisLayer = theLayer.duplicate(placeBefore, ElementPlacement.PLACEBEFORE);

                      app.activeDocument.activeLayer = thisLayer;

                      // do the transformations;

                      // =======================================================

                      var idTrnf = charIDToTypeID( "Trnf" );

                          var desc3 = new ActionDescriptor();

                          var idnull = charIDToTypeID( "null" );

                              var ref2 = new ActionReference();

                              var idLyr = charIDToTypeID( "Lyr " );

                              var idOrdn = charIDToTypeID( "Ordn" );

                              var idTrgt = charIDToTypeID( "Trgt" );

                              ref2.putEnumerated( idLyr, idOrdn, idTrgt );

                          desc3.putReference( idnull, ref2 );

                          var idFTcs = charIDToTypeID( "FTcs" );

                          var idQCSt = charIDToTypeID( "QCSt" );

                          var idQcsa = charIDToTypeID( "Qcsa" );

                          desc3.putEnumerated( idFTcs, idQCSt, idQcsa );

                          var idOfst = charIDToTypeID( "Ofst" );

                              var desc4 = new ActionDescriptor();

                              var idHrzn = charIDToTypeID( "Hrzn" );

                              var idPxl = charIDToTypeID( "#Pxl" );

                              desc4.putUnitDouble( idHrzn, idPxl, horizontalOffset );

                              var idVrtc = charIDToTypeID( "Vrtc" );

                              var idPxl = charIDToTypeID( "#Pxl" );

                              desc4.putUnitDouble( idVrtc, idPxl, verticalOffset );

                          var idOfst = charIDToTypeID( "Ofst" );

                          desc3.putObject( idOfst, idOfst, desc4 );

                          var idWdth = charIDToTypeID( "Wdth" );

                          var idPrc = charIDToTypeID( "#Prc" );

                          desc3.putUnitDouble( idWdth, idPrc, horScale );

                          var idHght = charIDToTypeID( "Hght" );

                          var idPrc = charIDToTypeID( "#Prc" );

                          desc3.putUnitDouble( idHght, idPrc, verScale );

                      if (thisAngle != 0) {

                          var idAngl = charIDToTypeID( "Angl" );

                          var idAng = charIDToTypeID( "#Ang" );

                          desc3.putUnitDouble( idAngl, idAng, thisAngle );

                                };

                      executeAction( idTrnf, desc3, DialogModes.NO );

                      };