Highlighted

SCRIPT ILLUSTRATOR

New Here ,
Apr 21, 2020

Copy link to clipboard

Copied

HELP, I´m trying make a script to align two objects, somebody know how please!!!

Adobe Community Professional
Correct answer by Silly-V | Adobe Community Professional

Are you already able to make basic scripts by yourself?

There are some levels of complexity when trying to replicate the Illustrator UI's alginment but let me provide a rudimentary sample which works on just path objects such as a couple of rectangles.

Here we shall start with one green and one yellow square which have different sizes:

Silly-V_0-1587481384194.png

Let's try to align the yellow one to the green one.

First concept is that in an Illustrator document, the art objects are inside array-like collections and those are directly tied to their stacking order. If we wanted to grab a hold of the green one, it would be the 2nd item in this collection of Document.pathItems at index 1 because in javascript all counting is started at index 0. The yellow item would be in index 0 therefore.

Here's a sample script:

#target illustrator
function test(){
	var doc = app.activeDocument;
	var yellowSquare = doc.pathItems[0];
	var greenSquare = doc.pathItems[1];

	yellowSquare.top = greenSquare.top;
};
test();

And now the result shows that after making the yellow square's .top property equal to the green square's .top property, they are aligned to the top of the green square!

Silly-V_1-1587481648657.png

Now let's try to align the green square to the vertical middle of the yellow one. This is a little more intense as you have to do some calculation in your script.

Here is a sample snippet that works in a bad way:

#target illustrator
function test(){
	var doc = app.activeDocument;
	var yellowSquare = doc.pathItems[0];
	var greenSquare = doc.pathItems[1];

	var heightDiff = yellowSquare.height - greenSquare.height;
	greenSquare.top = yellowSquare.top + (heightDiff / 2);
};
test();

First, I get the difference in height between the yellow and green square because they are of different height sizes. When this difference is divided by two, theoretically we should be able to add this number to the .top of the green square to get it vertically aligned to the middle of the yellow one. However, the result I get is not as expected:

Silly-V_2-1587481921502.png

This was done because 1) I needed to refresh my own memory obviously, it wasn't on purpose 🙂 and 2) to also show you an Illustrator elimentary 'gotcha' when it comes to the coordinate system: it's actually reversed vertically. So unlike in its own UI or in web design or anywhere else, in Illustrator scripting the Y values have to be subtracted from instead of added-to in order to move something downward and the reverse for moving something upward.

Let's try the fixed snippet now:

#target illustrator
function test(){
	var doc = app.activeDocument;
	var yellowSquare = doc.pathItems[0];
	var greenSquare = doc.pathItems[1];

	var heightDiff = yellowSquare.height - greenSquare.height;
	greenSquare.top = yellowSquare.top - (heightDiff / 2);
};
test();

And now we can see the green square is perfectly vertically aligned to the middle of the yellow square.

Silly-V_3-1587482145020.png

The difference being, I changed the operative line greenSquare.top = yellowSquare.top + (heightDiff / 2); to use the minus instead of plus so it looks like this: greenSquare.top = yellowSquare.top - (heightDiff / 2);.

 

And this is just a simple example, as there are more complexities when using paths with strokes or using clipped art and it's especially horrible when trying anything with opacity masks because it requires playing an action to undo the opacity masks first and then playing one to re-apply them (in most cases over the years of this forum this has not been a prevalent need and I would venture to say that most users learned to work around such issues instead of attempting to script them).

 

So now you can ask more questions specific to the kind of alignment you want and/or use the PDF scripting guide along with these provided snippets to make your own script.

TOPICS
Scripting

Views

924

Likes

Translate

Translate

Report

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

SCRIPT ILLUSTRATOR

New Here ,
Apr 21, 2020

Copy link to clipboard

Copied

HELP, I´m trying make a script to align two objects, somebody know how please!!!

Adobe Community Professional
Correct answer by Silly-V | Adobe Community Professional

Are you already able to make basic scripts by yourself?

There are some levels of complexity when trying to replicate the Illustrator UI's alginment but let me provide a rudimentary sample which works on just path objects such as a couple of rectangles.

Here we shall start with one green and one yellow square which have different sizes:

Silly-V_0-1587481384194.png

Let's try to align the yellow one to the green one.

First concept is that in an Illustrator document, the art objects are inside array-like collections and those are directly tied to their stacking order. If we wanted to grab a hold of the green one, it would be the 2nd item in this collection of Document.pathItems at index 1 because in javascript all counting is started at index 0. The yellow item would be in index 0 therefore.

Here's a sample script:

#target illustrator
function test(){
	var doc = app.activeDocument;
	var yellowSquare = doc.pathItems[0];
	var greenSquare = doc.pathItems[1];

	yellowSquare.top = greenSquare.top;
};
test();

And now the result shows that after making the yellow square's .top property equal to the green square's .top property, they are aligned to the top of the green square!

Silly-V_1-1587481648657.png

Now let's try to align the green square to the vertical middle of the yellow one. This is a little more intense as you have to do some calculation in your script.

Here is a sample snippet that works in a bad way:

#target illustrator
function test(){
	var doc = app.activeDocument;
	var yellowSquare = doc.pathItems[0];
	var greenSquare = doc.pathItems[1];

	var heightDiff = yellowSquare.height - greenSquare.height;
	greenSquare.top = yellowSquare.top + (heightDiff / 2);
};
test();

First, I get the difference in height between the yellow and green square because they are of different height sizes. When this difference is divided by two, theoretically we should be able to add this number to the .top of the green square to get it vertically aligned to the middle of the yellow one. However, the result I get is not as expected:

Silly-V_2-1587481921502.png

This was done because 1) I needed to refresh my own memory obviously, it wasn't on purpose 🙂 and 2) to also show you an Illustrator elimentary 'gotcha' when it comes to the coordinate system: it's actually reversed vertically. So unlike in its own UI or in web design or anywhere else, in Illustrator scripting the Y values have to be subtracted from instead of added-to in order to move something downward and the reverse for moving something upward.

Let's try the fixed snippet now:

#target illustrator
function test(){
	var doc = app.activeDocument;
	var yellowSquare = doc.pathItems[0];
	var greenSquare = doc.pathItems[1];

	var heightDiff = yellowSquare.height - greenSquare.height;
	greenSquare.top = yellowSquare.top - (heightDiff / 2);
};
test();

And now we can see the green square is perfectly vertically aligned to the middle of the yellow square.

Silly-V_3-1587482145020.png

The difference being, I changed the operative line greenSquare.top = yellowSquare.top + (heightDiff / 2); to use the minus instead of plus so it looks like this: greenSquare.top = yellowSquare.top - (heightDiff / 2);.

 

And this is just a simple example, as there are more complexities when using paths with strokes or using clipped art and it's especially horrible when trying anything with opacity masks because it requires playing an action to undo the opacity masks first and then playing one to re-apply them (in most cases over the years of this forum this has not been a prevalent need and I would venture to say that most users learned to work around such issues instead of attempting to script them).

 

So now you can ask more questions specific to the kind of alignment you want and/or use the PDF scripting guide along with these provided snippets to make your own script.

TOPICS
Scripting

Views

925

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 21, 2020 0
Adobe Community Professional ,
Apr 21, 2020

Copy link to clipboard

Copied

Are you already able to make basic scripts by yourself?

There are some levels of complexity when trying to replicate the Illustrator UI's alginment but let me provide a rudimentary sample which works on just path objects such as a couple of rectangles.

Here we shall start with one green and one yellow square which have different sizes:

Silly-V_0-1587481384194.png

Let's try to align the yellow one to the green one.

First concept is that in an Illustrator document, the art objects are inside array-like collections and those are directly tied to their stacking order. If we wanted to grab a hold of the green one, it would be the 2nd item in this collection of Document.pathItems at index 1 because in javascript all counting is started at index 0. The yellow item would be in index 0 therefore.

Here's a sample script:

#target illustrator
function test(){
	var doc = app.activeDocument;
	var yellowSquare = doc.pathItems[0];
	var greenSquare = doc.pathItems[1];

	yellowSquare.top = greenSquare.top;
};
test();

And now the result shows that after making the yellow square's .top property equal to the green square's .top property, they are aligned to the top of the green square!

Silly-V_1-1587481648657.png

Now let's try to align the green square to the vertical middle of the yellow one. This is a little more intense as you have to do some calculation in your script.

Here is a sample snippet that works in a bad way:

#target illustrator
function test(){
	var doc = app.activeDocument;
	var yellowSquare = doc.pathItems[0];
	var greenSquare = doc.pathItems[1];

	var heightDiff = yellowSquare.height - greenSquare.height;
	greenSquare.top = yellowSquare.top + (heightDiff / 2);
};
test();

First, I get the difference in height between the yellow and green square because they are of different height sizes. When this difference is divided by two, theoretically we should be able to add this number to the .top of the green square to get it vertically aligned to the middle of the yellow one. However, the result I get is not as expected:

Silly-V_2-1587481921502.png

This was done because 1) I needed to refresh my own memory obviously, it wasn't on purpose 🙂 and 2) to also show you an Illustrator elimentary 'gotcha' when it comes to the coordinate system: it's actually reversed vertically. So unlike in its own UI or in web design or anywhere else, in Illustrator scripting the Y values have to be subtracted from instead of added-to in order to move something downward and the reverse for moving something upward.

Let's try the fixed snippet now:

#target illustrator
function test(){
	var doc = app.activeDocument;
	var yellowSquare = doc.pathItems[0];
	var greenSquare = doc.pathItems[1];

	var heightDiff = yellowSquare.height - greenSquare.height;
	greenSquare.top = yellowSquare.top - (heightDiff / 2);
};
test();

And now we can see the green square is perfectly vertically aligned to the middle of the yellow square.

Silly-V_3-1587482145020.png

The difference being, I changed the operative line greenSquare.top = yellowSquare.top + (heightDiff / 2); to use the minus instead of plus so it looks like this: greenSquare.top = yellowSquare.top - (heightDiff / 2);.

 

And this is just a simple example, as there are more complexities when using paths with strokes or using clipped art and it's especially horrible when trying anything with opacity masks because it requires playing an action to undo the opacity masks first and then playing one to re-apply them (in most cases over the years of this forum this has not been a prevalent need and I would venture to say that most users learned to work around such issues instead of attempting to script them).

 

So now you can ask more questions specific to the kind of alignment you want and/or use the PDF scripting guide along with these provided snippets to make your own script.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 21, 2020 1
Adobe Community Professional ,
Apr 21, 2020

Copy link to clipboard

Copied

Great tutorial, Vasily. 👍😀

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 21, 2020 1
Community Beginner ,
Apr 24, 2020

Copy link to clipboard

Copied

This is great. Would it be possible to align grouped objects, text objects etc.?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 24, 2020 0
Adobe Community Professional ,
Apr 21, 2020

Copy link to clipboard

Copied

great tut Vasily, thanks for sharing!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 21, 2020 1
Adobe Community Professional ,
Apr 21, 2020

Copy link to clipboard

Copied

I just got here first to regurgitate the same stuff you've been posting for years, lol! 😜

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 21, 2020 1
New Here ,
Apr 21, 2020

Copy link to clipboard

Copied

THANKS!!!!!!!!!!!!!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 21, 2020 1
jatkin LATEST
Community Beginner ,
Apr 24, 2020

Copy link to clipboard

Copied

This is great. Would it be possible to align grouped objects, text objects etc.?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 24, 2020 0