Skip to main content
jorgea98580666
Participant
August 26, 2016
Question

Rescale text layer to concrete bounds

  • August 26, 2016
  • 2 replies
  • 296 views

Hi, I'm working on a script that replace the text of a concrete layer. I want to replace the text, and rescale a move it in order to fit it into a concrete bounds, and center the text in the middle of the canvas. I tryed this:

function rescaleText(doc, layer) {

    var textMargins = 100;

    var w = doc.width;

    var margins = {left: textMargins, right: w - textMargins};

    var newScale =   ((margins.right-margins.left)/(layer.bounds[2]-layer.bounds[0]))*100

    layer.resize(newScale, newScale ,AnchorPosition.MIDDLECENTER);

    }

It rescales correctly, but I can't center the text in the middle of the document. Is there any workarround to do that?

This topic has been closed for replies.

2 replies

Chuck Uebele
Community Expert
Community Expert
August 26, 2016

After I do a script resize, I just add a translate to move the layer where I want it. Just calculate the center of your text layer using bounds and then the center of the area you want to move it to. so say you want the text layer in the middle of your document (I'm going to assume the vertical position is correct):

var doc = activeDocument;

var textLayer = doc.activeLayer;

var textMiddle = textLayer.bounds[2]-textLayer.bounds[0];

textLayer.translate(doc.width/2-textMiddle,0);

JJMack
Community Expert
Community Expert
August 26, 2016

If it is resize to the size you want. With that layer the current activelayer do a select all and align the layer to the horizontal center and the vertical center of the selection

JJMack
jorgea98580666
Participant
August 26, 2016

Thanks!

But I need to repeat the same process 2 hundred times.

Is there a way to achieve that by script?

I can see the selection object in the Javascript reference but nothing about align.

JJMack
Community Expert
Community Expert
August 26, 2016

add the select all, align Hcenter, align Vcenter,deselect steps to your script. Record an action the has one step which runs your script.  Batch that action using the Image Processor script or Image Processor Pro plug-in script.

If you do not see something in Adobe DOM use action manager code from Adobe's scriptlistener plug-in. I use the code to position text over a placed image bounds selection

  //var Position = 5;

  var Position = Number(MICollage.msgPnl.grp5a.dd1.selection.index) + 1;

  switch (Position){

  case 1 : align('AdLf'); align('AdTp'); break;

  case 2 : align('AdCH'); align('AdTp'); break;

  case 3 : align('AdRg'); align('AdTp'); break;

  case 4 : align('AdLf'); align('AdCV'); break;

  case 5 : align('AdCH'); align('AdCV'); activeDocument.selection.deselect(); activeDocument.activeLayer.rotate(textAngle); break;

  case 6 : align('AdRg'); align('AdCV'); break;

  case 7 : align('AdLf'); align('AdBt'); break;

  case 8 : align('AdCH'); align('AdBt'); break;

  case 9 : align('AdRg'); align('AdBt'); break;

  default : break;

  }

function align(method) {

  var desc = new ActionDescriptor();

  var ref = new ActionReference();

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

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

  desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "ADSt" ), charIDToTypeID( method ) );

  try{

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

  }catch(e){}

}

JJMack