6 Replies Latest reply on Jan 30, 2016 6:45 PM by ojodegato

    Custom Canvas Script

    ojodegato Level 1

      Can the following sequence be scripted in PSCC 2015.1.2 ?

      1- Get the number of layers from active document

      2- Call Canvas Size dialog

      3- Get Height value from Canvas Size Dialog

      4- Select vertical down anchor

      5- Multiply Height by layer number

      6- Create custum canvas

        • 1. Re: Custom Canvas Script
          Chuck Uebele Adobe Community Professional & MVP

          Yes, that can be done. some of the code might be easier to get using ScriptListner. Also there might be issues if your file has groups or adjustment layers - makes the code more complicated determining what has pixels, as well as if there are smartobject that have pixels that extend outside of the canvas. Most likely the simplest way would be to

           

          get the number of layers:

           

          var doc=activeDocument;
          var numLayers = doc.layers.length;//will not get layers in groups.
          

           

          Best to set the document size in percent, then multiple the number of layers by 100 to get the percent you want to expand the canvas. Use scriptlistener to do this.

          • 2. Re: Custom Canvas Script
            ojodegato Level 1

            Which part of the code should be gotten with the script listener?

            • 3. Re: Custom Canvas Script
              Chuck Uebele Adobe Community Professional & MVP

              Just the canvas resize. This should work:

               

              #target photoshop
              var oldPref = app.preferences.rulerUnits
              app.preferences.rulerUnits = Units.PERCENT;
              
              var doc = activeDocument;
              var numLayers = doc.layers.length;
              var newSize = numLayers * 100
              canvasSize ();
              app.preferences.rulerUnits = oldPref;
              
              function canvasSize(){
                  var idCnvS = charIDToTypeID( "CnvS" );
                      var desc8 = new ActionDescriptor();
                      var idHght = charIDToTypeID( "Hght" );
                      var idPrc = charIDToTypeID( "#Prc" );
                      desc8.putUnitDouble( idHght, idPrc, newSize );
                      var idVrtc = charIDToTypeID( "Vrtc" );
                      var idVrtL = charIDToTypeID( "VrtL" );
                      var idTop = charIDToTypeID( "Top " );
                      desc8.putEnumerated( idVrtc, idVrtL, idTop );
                      var idcanvasExtensionColorType = stringIDToTypeID( "canvasExtensionColorType" );
                      var idcanvasExtensionColorType = stringIDToTypeID( "canvasExtensionColorType" );
                      var idBckC = charIDToTypeID( "BckC" );
                      desc8.putEnumerated( idcanvasExtensionColorType, idcanvasExtensionColorType, idBckC );
                  executeAction( idCnvS, desc8, DialogModes.NO );
              }
              
              • 4. Re: Custom Canvas Script
                ojodegato Level 1

                Thank you! the script works great.

                The document consists exclusively of pixel based layers. Do you know if it is possible to evenly distribute the layers along the new canvas size?

                For example:

                Original canvas 300px x 100px

                Layer quantity 3: 300px x 100px

                New canvas size 300px x 300px

                Layer 1 position: x=0 y=0

                Layer 2 position: x=0 y=100

                Layer 3 position: x=0 y=200

                • 5. Re: Custom Canvas Script
                  Chuck Uebele Adobe Community Professional & MVP

                  I'm not sure if this will move them in the right order:

                   

                  #target photoshop
                  var oldPref = app.preferences.rulerUnits
                  app.preferences.rulerUnits = Units.PERCENT;
                  
                  var doc = activeDocument;
                  var numLayers = doc.layers.length;
                  var newSize = numLayers * 100
                  canvasSize ();
                  
                  app.preferences.rulerUnits = Units.PIXELS;
                  
                  for(var i=1;i<numLayers;i++){
                      doc.activeLayer.isBackgroundLayer = false
                      var curLayer = doc.layers[i];
                      curLayer.translate(0,doc.height/numLayers*i)
                      }
                  
                  
                  app.preferences.rulerUnits = oldPref;
                  
                  function canvasSize(){
                      var idCnvS = charIDToTypeID( "CnvS" );
                          var desc8 = new ActionDescriptor();
                          var idHght = charIDToTypeID( "Hght" );
                          var idPrc = charIDToTypeID( "#Prc" );
                          desc8.putUnitDouble( idHght, idPrc, newSize );
                          var idVrtc = charIDToTypeID( "Vrtc" );
                          var idVrtL = charIDToTypeID( "VrtL" );
                          var idTop = charIDToTypeID( "Top " );
                          desc8.putEnumerated( idVrtc, idVrtL, idTop );
                          var idcanvasExtensionColorType = stringIDToTypeID( "canvasExtensionColorType" );
                          var idcanvasExtensionColorType = stringIDToTypeID( "canvasExtensionColorType" );
                          var idBckC = charIDToTypeID( "BckC" );
                          desc8.putEnumerated( idcanvasExtensionColorType, idcanvasExtensionColorType, idBckC );
                      executeAction( idCnvS, desc8, DialogModes.NO );
                  }
                  
                  • 6. Re: Custom Canvas Script
                    ojodegato Level 1

                    It works fantastic!

                    Many thanks again for your generous help!