Skip to main content
Inspiring
September 26, 2022
Answered

Align two objects, obj 2 aligned to top and center obj 1

  • September 26, 2022
  • 1 reply
  • 278 views

Hello friends, I have the following code:

var doc = app.activeDocument;
var objeto = doc.selection[0];
var posicion = objeto.position;
var WidthInicial = objeto.width;
var WidthFinal = WidthInicial-14.4;
var ScaleFactor = (WidthFinal / WidthInicial)*100;
objeto.width = WidthFinal;
objeto.resize(100,ScaleFactor, true, true, true, true, null, undefined);
var imgGroup = doc.groupItems.add();
//var rectRef = imgGroup.pathItems.rectangle(posicion[1],posicion[0],objeto.width, objeto.height);
var rectRef = imgGroup.pathItems.rectangle(posicion[1],posicion[0],75,75);
alert(objeto.width + " " + rectRef.width);
//objeto.remove();
//alert("objeto removido");

 

which gets the width of a selected object, reduces it by 0.2in (14.4px) and also reduces height proportionally, then since it did not scaled to the top center pisition I was trying to create a new square with the new measures, but I can't center the new object (obj 2) to the previous selected object (obj 1).

 

Can anyone help me please. To make it short I am trying to create a script to reduce a quare/object proportionally in steps of 0.2in and keep it centered in the top center point.

This topic has been closed for replies.
Correct answer m1b

Hi @AntonioPacheco, if I understand you correctly, please try this version:

var doc = app.activeDocument;
var objeto = doc.selection[0];
var dup = objeto.duplicate();
var WidthInicial = objeto.width;
var WidthFinal = WidthInicial - 14.4;
var ScaleFactor = (WidthFinal / WidthInicial) * 100;
dup.resize(ScaleFactor, ScaleFactor, true, true, true, true, null, Transformation.TOP);
doc.selection = [];
dup.selected = true;

If you run the script multiple times, it will have the effect you describe. But I wasn't sure what the rectangle was for.

- Mark

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
September 26, 2022

Hi @AntonioPacheco, if I understand you correctly, please try this version:

var doc = app.activeDocument;
var objeto = doc.selection[0];
var dup = objeto.duplicate();
var WidthInicial = objeto.width;
var WidthFinal = WidthInicial - 14.4;
var ScaleFactor = (WidthFinal / WidthInicial) * 100;
dup.resize(ScaleFactor, ScaleFactor, true, true, true, true, null, Transformation.TOP);
doc.selection = [];
dup.selected = true;

If you run the script multiple times, it will have the effect you describe. But I wasn't sure what the rectangle was for.

- Mark

Inspiring
September 26, 2022

let me try it, thanks @m1b