Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Convert to Shape and Resize

Contributor ,
Nov 18, 2021 Nov 18, 2021

Copy link to clipboard

Copied

Hello,

Not sure one this one, I'm being asked for a script that can Convert a selection of vector shapes to a single Shape - Rectangle, then
A. if the rectangle's width is equal to or smaller than the height, leave the shape as is.

B. OR if the rectangle's width is greater than it's height, make the height = the width

 

How can that be done?   Thanks in advance!

TOPICS
Scripting

Views

98

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
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Nov 19, 2021 Nov 19, 2021
here you go. I did some other cleanup as well and added some logic for changing colors at the bottom if you want it. #target Illustrator function test() { var docRef = app.activeDocument; var ab = docRef.artboards; var sel = docRef.selection; if(!sel.length) { alert("Please select some items and try again"); return; } var item; var path; var pathBounds; var pathWidth; var pathHeight; var newRect; var centerPoint; //loop through the selection backwards for(var i = sel.leng...

Likes

Translate

Translate
Adobe Community Professional ,
Nov 18, 2021 Nov 18, 2021

Copy link to clipboard

Copied

#target Illustrator
function test() {
	var docRef = app.activeDocument;
	var ab = docRef.artboards;

	var sel = docRef.selection;
	if(!sel.length)
	{
		alert("Please select some items and try again");
		return;
	}

	// Convert a selection of vector shapes to a single Shape - Rectangle, then
	// A. if the rectangle's width is equal to or smaller than the height, leave the shape as is.
	// B. OR if the rectangle's width is greater than it's height, make the height = the width

	var item;
	var path;
	var pathBounds;
	var pathWidth;
	var pathHeight;
	var newRect;

	//loop through the selection backwards
	for(var i = sel.length-1; i >= 0; i--)
	{
		item = sel[i];
		path = item;
		pathBounds = path.geometricBounds;
		pathWidth = pathBounds[2] - pathBounds[0];
		pathHeight = pathBounds[1] - pathBounds[3];

		if(pathWidth == pathHeight || pathWidth < pathHeight)
		{
			//do nothing
		}
		else
		{
			pathHeight = pathWidth;
		}
		item.remove();
		newRect = docRef.pathItems.rectangle(pathBounds[1], pathBounds[0], pathWidth, pathHeight);
		newRect.name = "rect " + i;
	}
}
test();

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
community guidelines
Contributor ,
Nov 19, 2021 Nov 19, 2021

Copy link to clipboard

Copied

@DilliamWowling Oh Wow!  That was a quick response and it worked perfectly!   Thank you for the assist!

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
community guidelines
Adobe Community Professional ,
Nov 19, 2021 Nov 19, 2021

Copy link to clipboard

Copied

no problem. it was a fun little "quick challenge". 😃

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
community guidelines
Contributor ,
Nov 19, 2021 Nov 19, 2021

Copy link to clipboard

Copied

Quick follow up question,  is it possible to have the shape remain centered when is adjusts in size?

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
community guidelines
Adobe Community Professional ,
Nov 19, 2021 Nov 19, 2021

Copy link to clipboard

Copied

LATEST

here you go. I did some other cleanup as well and added some logic for changing colors at the bottom if you want it.

 

#target Illustrator
function test() {
	var docRef = app.activeDocument;
	var ab = docRef.artboards;

	var sel = docRef.selection;
	if(!sel.length)
	{
		alert("Please select some items and try again");
		return;
	}

	var item;
	var path;
	var pathBounds;
	var pathWidth;
	var pathHeight;
	var newRect;
	var centerPoint;

	//loop through the selection backwards
	for(var i = sel.length-1; i >= 0; i--)
	{
		item = sel[i];
		path = item;
		pathBounds = path.geometricBounds;
		pathWidth = pathBounds[2] - pathBounds[0];
		pathHeight = pathBounds[1] - pathBounds[3];

		if(pathWidth > pathHeight)
		{
			//move the y coordinate up by half of the increase in height
			//to keep the rectangle centered to the original shape.
			pathBounds[1] += (pathWidth - pathHeight)/2;

			pathHeight = pathWidth;
		}
		item.remove();
		newRect = docRef.pathItems.rectangle(pathBounds[1], pathBounds[0], pathWidth, pathHeight);
		newRect.name = "rect " + i;

		//in case you want to change the fill/stroke.. use these
		//otherwise the new rect will simply use whatever fill/stroke
		//colors are active in your colors panel. 
		// newRect.fillColor = swatches["White"].color;
		// newRect.strokeColor = swatches["Black"].color;
	}

}
test();

  

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
community guidelines