2 Replies Latest reply on Mar 8, 2018 5:09 PM by LeafArrow20

    How can I capture a smart object's original dimension via scripts?

    LeafArrow20 Level 1

      The Setting

      I have a document with a canvas size of 1920x1080 and a large number of layers. Each layer is a smart object which I've placed by dragging and dropping the contents of a folder onto the canvas. No two layers have the same dimensions. Some of the smart objects are larger than the canvas and photoshop is automatically shrinking them down to fit.

       

      The Goal

      I want all my smart objects to be their original dimensions.

      I want to avoid going to each object, transforming, clicking the "link dimensions" button, manually typing "100%", and placing the transform.

       

      The Problem

      First, I tried creating an action to select the layer > transform width and height to 100% > select next layer. Next, I tried creating a script to go through each layer and resize() to 100%. Both attempts were foiled by the same problem, the resize function is relative to the object's current dimensions.

       

      for example

      When creating an action, I recorded my actions and by manually typing in "100%" for the smart object transform. The smart object resized to its original dimensions. Perfect, this is what I want. However, when I drilled into the action it had actually resized 405%. I typed in "100%" but it recorded "405%" making this action unusable unless the layers were all the same exact size.

       

      My Questions

      How can I capture a smart object's original dimensions?

      Is there a better way to go about this?

       

      ---------

      Cheers!

      ---------

       

      My Current Script

      var doc = app.activeDocument;
      var allLayers = [];
      var allLayers = collectAllLayers(doc, allLayers);
      
      
      function collectAllLayers (doc, allLayers){
          for (var m = 0; m < doc.layers.length; m++){
              var theLayer = doc.layers[m];
              if (theLayer.typename === "ArtLayer"){
                  allLayers.push(theLayer);
              }else{
                  collectAllLayers(theLayer, allLayers);
              }
          }
          return allLayers;
      }
      
      
      for(i = (allLayers.length-1); i >= 0; i--){
      //resize smart object to original dimension
      };
      
        • 1. Re: How can I capture a smart object's original dimension via scripts?
          JJMack Most Valuable Participant

          GOTO the photoshop scripting forum r-bin pos%ed some script code that works in cc 2018 that can be used to find that information

          • 2. Re: How can I capture a smart object's original dimension via scripts?
            LeafArrow20 Level 1

            Thanks JJMack for pointing me in the right direction. I now realize this should be in the Photoshop Scripts forum. If anyone knows how to move it over let me know.

             

            -------

             

            I figured it out. This will resize all smart object to their original dimensions within a document, including smart objects nested in groups. I'm not much of a developer so my code is neither beautiful nor efficient. For anyone curious:

             

            var doc = app.activeDocument;
            
            function resizeSmartObject() {
            
            //target current active layer
            var ref = new ActionReference(); 
            ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );    
            
            //check the type of layer
            //1 = ArtLayer
            //2 = Adjustment
            //3 = Text
            //4 = Shape
            //5 = Smart Object
            // ...
            //9 = Gradient Fil
            //10 = Pattern Fill
            //11 = Solid Fill
            var layerType = executeActionGet(ref).getInteger(stringIDToTypeID("layerKind"));
            
            //if layer is a smart object, get diminensions of source
            if (layerType == "5") {
                var obj = executeActionGet(ref).getObjectValue(stringIDToTypeID("smartObjectMore"));    
                with (obj) {  
                    var _tmp = getObjectValue(stringIDToTypeID("size"));  
                    var size = new Object({  
                        width:  _tmp.getDouble(stringIDToTypeID("width")),  
                        height: _tmp.getDouble(stringIDToTypeID("height")),  
                    });  
                }
            
                //divide the original/current
                var bounds = app.activeDocument.activeLayer.bounds;  
                var w_new = (size.width/(bounds[2].value - bounds[0].value))*100;
                var h_new = (size.height/(bounds[3].value - bounds[1].value))*100;
                app.activeDocument.activeLayer.resize(w_new, h_new);
            
                return;
            
            } else { return; }
            }
            
            
            function cycleLayers (set){
                 for (var i = 0; i < set.layers.length; i++){
                    doc.activeLayer = set.layers[i];
                    if(doc.activeLayer.typename == 'LayerSet'){
                        if(doc.activeLayer.layers.length > 0) {
                            cycleLayers(doc.activeLayer);
                        }
                    } else {
                        resizeSmartObject();
                    }
                 }
            }
            
            
            cycleLayers(doc);
            
            1 person found this helpful