0

I want to resize multiple smart objects in a project to a specific size, let's say 200x200px. They are all different sizes. The problem is, when I record the resizing using the Free Transform tool, it only records the percentage the layer got bigger/smaller, instead of the new size in pixels.

Is it possible to automate this using an action or script? The layers should stay smart objects, so they can not be rasterized. Thanks in advance!

1

Select one of the Smart Objects and record the following action:

  • Use Layer > Smart Objects > Convert to Smart Object to create a new Smart Object with the currently selected Smart object inside.
  • Double-click the currently selected Smart Object to edit its contents.
  • Use Image > Image Size to scale to the wanted size. Remember to set everything properly according to your needs.
  • Use File > Save to save the Smart Object.
  • Use File > Close to close the Smart Object.
  • Now back in the main document, right-click the still selected Smart Object and select Convert to Layers to unpack the original Smart Object.
  • End the recording.

Now you should have the original Smart Object transformed to the wanted size and the action can be used on other Smart Objects to scale them to the same absolute size.

1
  • Never would have thought about that, thanks a lot. This works perfectly!
    – Adinde
    Sep 11 '20 at 16:27
0

Here's a script that will resize selected layers to specific values (set in the beginning). I'm not sure why but transforming smart object once was giving a slightly wrong result (layers were 2-4 pixels off) — normal layers didn't have this problem. So the script runs transform twice of each layer.

function main()
{
  var width = 200;
  var height = 200;
  var layers = getInfo();
  
  for (var i = 0; i < layers.length; i++)
  {
    selectById(layers[i].id);

    transform(
    {
      layer: layers[i],
      width: width,
      height: height
    });

    transform(
    {
      layer: getInfo()[0],
      width: width,
      height: height
    });
  }

  /////////////////////////////////////////////////////////////////////////////////////
  // funcs ////////////////////////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////////////////////////////////

  function selectById(id) {
      var desc1 = new ActionDescriptor();
      var ref1 = new ActionReference();
      ref1.putIdentifier(charIDToTypeID('Lyr '), id);
      desc1.putReference(charIDToTypeID('null'), ref1);
      executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);
  } // end of selectById()
  

  function getInfo()
  {
    var lyrs = [];
    var lyr, bounds;
    var ref = new ActionReference();
    var desc;
    var tempIndex;
    var ref2;
    ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayers"));
    ref.putEnumerated(charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));

    var targetLayers = executeActionGet(ref).getList(stringIDToTypeID("targetLayers"));
    for (var i = 0; i < targetLayers.count; i++)
    {
      tempIndex = 0;
      ref2 = new ActionReference();
      try
      {
        activeDocument.backgroundLayer;
        ref2.putIndex(charIDToTypeID('Lyr '), targetLayers.getReference(i).getIndex());
        try
        {
          desc = executeActionGet(ref2);
          tempIndex = desc.getInteger(stringIDToTypeID("itemIndex")) - 1;
        }
        catch (e)
        {
          tempIndex = 0;
        }
      }
      catch (o)
      {
        ref2.putIndex(charIDToTypeID('Lyr '), targetLayers.getReference(i).getIndex() + 1);
        desc = executeActionGet(ref2);
        tempIndex = desc.getInteger(stringIDToTypeID("itemIndex"));
      }

      lyr = {};
      bounds = desc.getObjectValue(stringIDToTypeID("bounds"));
      lyr.id = desc.getInteger(stringIDToTypeID("layerID"));
      lyr.height = bounds.getDouble(stringIDToTypeID("height"));
      lyr.width = bounds.getDouble(stringIDToTypeID("width"));


      lyrs.push(lyr);
    }
    return lyrs

  }

  function transform(data)
  {
    var ratioWidth, ratioHeight;

    ratioWidth = data.width * 100 / data.layer.width
    ratioHeight = data.height * 100 / data.layer.height

    var desc = new ActionDescriptor();
    desc.putEnumerated(charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa'));
    desc.putUnitDouble(charIDToTypeID('Wdth'), charIDToTypeID('#Prc'), ratioWidth);
    desc.putUnitDouble(charIDToTypeID('Hght'), charIDToTypeID('#Prc'), ratioHeight);
    executeAction(charIDToTypeID('Trnf'), desc, DialogModes.NO);
  }

}

app.activeDocument.suspendHistory("resize selected", "main()");
1
  • The first solution looks like it's working perfectly so far. If there are any issues I'll try your script for sure. Thanks anyway!
    – Adinde
    Sep 11 '20 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.