Skip to main content
Known Participant
July 20, 2022
Answered

How to copy height to another object in illustrator script? thanks

  • July 20, 2022
  • 1 reply
  • 642 views

How does the script change the height of object 1 to the height of object 2? thanks

This topic has been closed for replies.
Correct answer Sergey Osokin

Quick modification. The clipping mask can be a group of paths or text in Adobe Illustrator. Then the code won't work. I didn't make it complicated. But in your example you have a mask from a simple path.

function resizeZero() {
  var aDoc = app.activeDocument;
  var zero = selection[0];
  var one = selection[1];
  var coeff = Math.abs(getHeight(one) / getHeight(zero) * 100);
  zero.resize(coeff, coeff);
  redraw();
}

function getHeight(item) {
  return (item.typename == 'GroupItem' && item.clipped) ? getMaskPath(item).height : item.height;
}

function getMaskPath(group) {
  for (var i = 0; i < group.pageItems.length; i++) {
    var item = group.pageItems[i];
    if ((item.typename === 'CompoundPathItem' && item.pathItems[0].clipping) || item.clipping) {
      return item;
    }
  }
}

resizeZero();

 

1 reply

rcraighead
Legend
July 20, 2022

With two objects selected this script will resize the width of top object to match the width of bottom object:

function resizeZero() {
    var aDoc = app.activeDocument;
    var Zero = selection[0];
    var One = selection[1];
    Zero.width = One.width;
    redraw();
}

resizeZero();
Known Participant
July 21, 2022

What is required is that the height of 1 becomes the height of 2, the width is increased in the same proportion, and the clipping mask is supported.

Sergey Osokin
Sergey OsokinCorrect answer
Inspiring
July 21, 2022

Quick modification. The clipping mask can be a group of paths or text in Adobe Illustrator. Then the code won't work. I didn't make it complicated. But in your example you have a mask from a simple path.

function resizeZero() {
  var aDoc = app.activeDocument;
  var zero = selection[0];
  var one = selection[1];
  var coeff = Math.abs(getHeight(one) / getHeight(zero) * 100);
  zero.resize(coeff, coeff);
  redraw();
}

function getHeight(item) {
  return (item.typename == 'GroupItem' && item.clipped) ? getMaskPath(item).height : item.height;
}

function getMaskPath(group) {
  for (var i = 0; i < group.pageItems.length; i++) {
    var item = group.pageItems[i];
    if ((item.typename === 'CompoundPathItem' && item.pathItems[0].clipping) || item.clipping) {
      return item;
    }
  }
}

resizeZero();