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

Center object inside another object in illustrator script

Explorer ,
Jan 10, 2018 Jan 10, 2018

I have a simple problem, but I haven't found a solution it.

I have a object and rectangle grouped together and want to scale the object proportionally and have it centered inside the rectangle.

I'm looking for an automated solution to this because I have a lot of objects that need to be scaled and centered like this.

The art I'm working with is similar to this.

Screen Shot 2018-01-10 at 9.58.05 PM.png

TOPICS
Scripting
2.2K
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 1 Correct answer

Community Expert , Jan 11, 2018 Jan 11, 2018

here's some pseudo code that may help..

var icon = the icon artwork

var background = the background square

var iconDimension,scale;

//get the largest dimension of the icon

if icon.width > icon.height

    iconDimension = icon.width;

else

    iconDimension = icon.height;

//get the scale percentage

if iconDimension < background.width

    scale = (iconDimension / background.width) * 100

else

    scale = (background.width / iconDimension) * 100

//get 80% of the scale

scale = scale*.8;

//resize the icon

icon.resize(

...
Translate
Adobe
Valorous Hero ,
Jan 10, 2018 Jan 10, 2018

You simply do pageItem.resize(w, h).

#target illustrator

function test(){

  var doc = app.activeDocument;

  var obj = doc.pathItems.getByName("obj");

  obj.resize(150, 150);

};

test();

Before:

After:

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
Explorer ,
Jan 10, 2018 Jan 10, 2018

Silly-V​ thanks for response !

But I need different solution. I have different sizes of vector objects in artboard. I want to add each of them to groupped standart sized rectangles and make the icons be center and fit the rectangle. I just need script to make below " Before - After " thing.

Before:

Screen Shot 2018-01-10 at 11.22.10 PM.png

After:

Screen Shot 2018-01-10 at 9.58.05 PM.png

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
Valorous Hero ,
Jan 10, 2018 Jan 10, 2018

How do you decide what % of height is to be smaller than the rectangle, which gives it such amount of padding room?

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
Explorer ,
Jan 10, 2018 Jan 10, 2018

I guess icon's height has to be 80% of rectangle's height. Actually I want to resize all my icons in artboard proportionally / keeping the aspect ratio. Let's say my rectangle should be 500x500px. My icon's height should be not more than 400px.

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 Expert ,
Jan 11, 2018 Jan 11, 2018
LATEST

here's some pseudo code that may help..

var icon = the icon artwork

var background = the background square

var iconDimension,scale;

//get the largest dimension of the icon

if icon.width > icon.height

    iconDimension = icon.width;

else

    iconDimension = icon.height;

//get the scale percentage

if iconDimension < background.width

    scale = (iconDimension / background.width) * 100

else

    scale = (background.width / iconDimension) * 100

//get 80% of the scale

scale = scale*.8;

//resize the icon

icon.resize(scale,scale);

//make sure the icon is still centered in the background box

icon.left = (background.left + background.width/2) - icon.width/2

icon.top = (background.top - background.width/2) + icon.height/2

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