Skip to main content
Inspiring
November 30, 2022
解決済み

Script: Make rectangle based on size of object on layer

  • November 30, 2022
  • 返信数 2.
  • 2259 ビュー

Hello beautiful minds,
Would anyone happen to know how to make the following work in a script, if its possible:

 

Create a rectangle, based on the height(100%) of a text box, and the width(90%) of the same textbox.
Then have it go to page center.

Thanks for any help, let me know if you have any questions.

このトピックへの返信は締め切られました。
解決に役立った回答 Sergey Osokin

Change the percentage (decimal) in the "ratio".

(function (){
  if (!documents.length) return;
  if (!selection.length || selection[0].typename !== 'TextFrame') return;
  var ratio = 0.9 // Width scale
  var tf = selection[0];
  var color = new RGBColor();
  color.red = 255;
  color.green = 0;
  color.blue = 0;

  var dup = tf.duplicate().createOutline();
  var bnds = dup.geometricBounds;
  dup.remove();
  var w = Math.abs(bnds[2] - bnds[0]);
  var h = Math.abs(bnds[3] - bnds[1]);
  var top = bnds[1];
  var left = bnds[0] + w * 0.5 * (1 - ratio);

  var rect = tf.layer.pathItems.rectangle(top, left, w * ratio, h);
  rect.fillColor = color;
  rect.stroked = false;
  rect.move(tf, ElementPlacement.PLACEAFTER);
})();

 

返信数 2

pixxxelschubser
Community Expert
Community Expert
November 30, 2022

And please describe the kind of your textbox. (Because: 90% of point text does not make sense.)

Aidan_C作成者
Inspiring
December 2, 2022

Ive posted an image above if that helps.

Sergey Osokin
Inspiring
November 30, 2022

Hi. It would make more sense to the community if you could show before and after pictures.

Aidan_C作成者
Inspiring
December 2, 2022

So I would like to be able to run a script, that automatically creates the red rectangle you see behind the text.
Its the same height as the text, but slightly smaller width - possibly 90% ish

Sergey Osokin
Sergey Osokin解決!
Inspiring
December 2, 2022

Change the percentage (decimal) in the "ratio".

(function (){
  if (!documents.length) return;
  if (!selection.length || selection[0].typename !== 'TextFrame') return;
  var ratio = 0.9 // Width scale
  var tf = selection[0];
  var color = new RGBColor();
  color.red = 255;
  color.green = 0;
  color.blue = 0;

  var dup = tf.duplicate().createOutline();
  var bnds = dup.geometricBounds;
  dup.remove();
  var w = Math.abs(bnds[2] - bnds[0]);
  var h = Math.abs(bnds[3] - bnds[1]);
  var top = bnds[1];
  var left = bnds[0] + w * 0.5 * (1 - ratio);

  var rect = tf.layer.pathItems.rectangle(top, left, w * ratio, h);
  rect.fillColor = color;
  rect.stroked = false;
  rect.move(tf, ElementPlacement.PLACEAFTER);
})();