Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Hight reduction script

Community Beginner ,
Mar 16, 2023 Mar 16, 2023

I am trying to write simple script that reduces hight of selection (single object or many objects on different layers) by fixed parameter. In my case 6mm. The problem is that in result redictionis 2.117mm instead of expected 6mm. Do you have any clue why and how the proper code wshould look like?

 

var doc = app.activeDocument;
var sel = doc.selection;
for(var i = 0; i < sel.length; i++){
sel[i].height = sel[i].height - 6;
}

TOPICS
Scripting
874
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Mar 16, 2023 Mar 16, 2023

Hi @irek289170064v73, scripts use points for measurements by default. So you can do it like this:

var mm = 2.834645;
var doc = app.activeDocument;
var sel = doc.selection;
for (var i = 0; i < sel.length; i++) {
    sel[i].height = sel[i].height - 6 * mm;
}

 There are other ways, but I find this very simple and I prefer it in almost all cases.

- Mark

Translate
Enthusiast , Mar 16, 2023 Mar 16, 2023

Perhaps you can use the ResiteToSize script for this and other tasks to change the absolute or relative size https://github.com/creold/illustrator-scripts/blob/master/md/Item.md#resizetosize
2023-03-17 11.09.36.gif

Translate
Adobe
Community Expert ,
Mar 16, 2023 Mar 16, 2023

Hi @irek289170064v73, scripts use points for measurements by default. So you can do it like this:

var mm = 2.834645;
var doc = app.activeDocument;
var sel = doc.selection;
for (var i = 0; i < sel.length; i++) {
    sel[i].height = sel[i].height - 6 * mm;
}

 There are other ways, but I find this very simple and I prefer it in almost all cases.

- Mark

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 16, 2023 Mar 16, 2023

I have been trying for 2 days to get GPT chat to help me, and despite my efforts, we couldn't. On the forum I got help in a few minutes. However, people are irreplaceable.

Thank you!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 16, 2023 Mar 16, 2023

Perhaps you can use the ResiteToSize script for this and other tasks to change the absolute or relative size https://github.com/creold/illustrator-scripts/blob/master/md/Item.md#resizetosize
2023-03-17 11.09.36.gif

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 17, 2023 Mar 17, 2023

Thnak you. I am just trying to automet hight reduction dependin on the file name I work on. But it doesn't work properly. 

Basing on the latest response my code is as below but. Transformation is not correct and it is not centered. 

For me inportant is also that reduction should base on selection and its bounding box size, so the masked elements or pictures would have no influance on transformation.

var fileName = app.activeDocument.name;
var reduction = 0;
if (fileName.indexOf("1.14") !== -1 || fileName.indexOf("1,14") !== -1) {
reduction = 6;
} else if (fileName.indexOf("1.7") !== -1 || fileName.indexOf("1,7") !== -1) {
reduction = 10.2;
}

var mm = 2.834645;
var doc = app.activeDocument;
var sel = doc.selection;
for (var i = 0; i < sel.length; i++) {
var item = sel[i];
var bounds = item.geometricBounds;
var height = bounds[3] - bounds[1];
var newHeight = height - reduction;
var centerX = (bounds[0] + bounds[2])/2;
var centerY = (bounds[1] + bounds[3])/2 - reduction * mm / 2;
item.resize(100, (newHeight/height)*100, true, true, true, true);
item.position = [centerX, centerY];
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 17, 2023 Mar 17, 2023
LATEST

You forgot to convert the newHeight variable to mm.

quote

Transformation is not correct and it is not centered....reduction should base on selection and its bounding box size, so the masked elements or pictures would have no influance on transformation.

This is wrong. geometricBounds is defined incorrectly for the clipping group. So the transformation is not from the center of the mask shape, but from the center of all elements in the clipping group.

var doc = app.activeDocument;
var fileName = doc.name;
var sel = doc.selection;
var reduction = 0;

if (/1(,|\.)14/.test(fileName)) {
  reduction = 6;
} else if (/1(,|\.)7/.test(fileName)) {
  reduction = 10.2;
}

for (var i = 0; i < sel.length; i++) {
  var item = sel[i];
  var bounds = item.geometricBounds;
  var height = bounds[3] - bounds[1];
  var newHeight = height - mm2pt(reduction);
  var coeff = (newHeight / height) * 100;
  item.resize(100, coeff, true, true, true, true, 100, Transformation.CENTER);
}

// Convert mm to pt
function mm2pt(n) {
  return n * 2.83464567;
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines