0

I have a script modified from here: specific resize layer action

Which will resize a selected layer to fit within 300px (I tweaked it to check if width or height are bigger, and scale accordingly)

Is there a way to make this script apply to all selected layers?

(function (){
    var targetSize = 300
    var startRulerUnits = app.preferences.rulerUnits;  
    app.preferences.rulerUnits = Units.PIXELS;  
    var bounds = activeDocument.activeLayer.bounds; 
    var width = bounds[2].value - bounds[0].value;
    var height = bounds[3].value - bounds[1].value;
    if(width>height){
        var newSize = (100 / width) * targetSize;  
    }else{
        var newSize = (100 / height) * targetSize; 
    }
    activeDocument.activeLayer.resize(newSize, newSize, AnchorPosition.MIDDLECENTER);
    app.preferences.rulerUnits = startRulerUnits;  
})(); 

1 Answer 1

0

Yes, it's possible.

Firstly you need to find a way of telling Photoshop what layers are currently selected. There's no built in method for this, so you have to put them in a group, add them to an array and then put everything back to normal.

Then it's just a matter of looping over that little list and performing your function above. I've renamed it to do_that_thing()

So the whole thing looks like this:

// Switch off any dialog boxes
displayDialogs = DialogModes.NO; // OFF

var startRulerUnits = app.preferences.rulerUnits;  
app.preferences.rulerUnits = Units.PIXELS;  

var mySelection = [];
get_selected_layers(); // selected layers
// alert(selected_layers);

// loop over the selected layers
for (var i=0; i < mySelection.length; i++)
{
  do_that_thing();
}

//Reset units
app.preferences.rulerUnits = startRulerUnits;  

// Set Display Dialogs back to normal
displayDialogs = DialogModes.ALL; // NORMAL

//functions here:

function do_that_thing()
{
  var targetSize = 300;
  var bounds = activeDocument.activeLayer.bounds; 
  var width  = bounds[2].value - bounds[0].value;
  var height = bounds[3].value - bounds[1].value;

  if(width > height)
  {
    var newSize = (100 / width) * targetSize;  
  }
  else
  {
    var newSize = (100 / height) * targetSize; 
  }

  activeDocument.activeLayer.resize(newSize, newSize, AnchorPosition.MIDDLECENTER);
}


function get_selected_layers()
{
  // taken from
  // https://stackoverflow.com/questions/27255364/

  var resultLayers = [];
  try
  {
    // create a temp group
    var idGrp = stringIDToTypeID( "groupLayersEvent" );
    var descGrp = new ActionDescriptor();
    var refGrp = new ActionReference();
    refGrp.putEnumerated(charIDToTypeID( "Lyr " ),charIDToTypeID( "Ordn" ),charIDToTypeID( "Trgt" ));
    descGrp.putReference(charIDToTypeID( "null" ), refGrp );
    executeAction( idGrp, descGrp, DialogModes.NO );

    for (var ix=0; ix < app.activeDocument.activeLayer.layers.length; ix++)
      {
        resultLayers.push(app.activeDocument.activeLayer.layers[ix]);
        mySelection.push(app.activeDocument.activeLayer.layers[ix]);
      }

    // get rid of the temp group
    // delete history state
    // =======================================================
    var idDlt = charIDToTypeID( "Dlt " );
    var desc103 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
    var ref27 = new ActionReference();
    var idHstS = charIDToTypeID( "HstS" );
    var idCrnH = charIDToTypeID( "CrnH" );
    ref27.putProperty( idHstS, idCrnH );
    desc103.putReference( idnull, ref27 );
    executeAction( idDlt, desc103, DialogModes.NO );

  } 
  catch 
  (eeek)
  {
    alert("Something went wrong")
  };

  return resultLayers;
}   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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