Skip to main content
Known Participant
September 5, 2024
Answered

Script - Proportionally rounded corners by percentage

  • September 5, 2024
  • 1 reply
  • 543 views

Hi,
I posted a similar question a while ago, but the script I was recommended didn't work.

 

Now I've made some adjustments, and what I need is a script that can do the following:
How to get 12% proportionally rounded corners on squares of different sizes.

 

Can anyone help me with this script? (I've also tried ChatGPT without success)

 

Thanks!

 

This topic has been closed for replies.
Correct answer jduncan

This should get you what you need. Please note that this creates new pathItems directly above each selected object for demonstration purposes. If you are not working with perfect squares, you must decide if you want the radius calculated using the target object's width or height (see script comments).

 

var doc = app.activeDocument;
var sel = doc.selection;

var color = new RGBColor();
color.red = 255;
color.green = color.blue = 0;

var target, targetBounds, width, height, radius, replacement;
for (var i = 0; i < sel.length; i++) {
  // get info of the target rectangle (square)
  target = sel[i];
  targetBounds = target.geometricBounds;
  width = targetBounds[2] - targetBounds[0];
  height = targetBounds[1] - targetBounds[3];

  // calculate radius at 12% of `width`, or if you prefer to use `height`
  // just change the line below (doesn't matter if you are dealing with squares)
  radius = width * 0.12; // change to width to `height` if you prefer

  // create the replacement rounder rectangle
  replacement = doc.pathItems.roundedRectangle(
    targetBounds[1],
    targetBounds[0],
    width,
    height,
    radius,
    radius
  );
  replacement.fillColor = color;
  replacement.move(target, ElementPlacement.PLACEBEFORE);
}

 

1 reply

jduncan
Community Expert
jduncanCommunity ExpertCorrect answer
Community Expert
September 5, 2024

This should get you what you need. Please note that this creates new pathItems directly above each selected object for demonstration purposes. If you are not working with perfect squares, you must decide if you want the radius calculated using the target object's width or height (see script comments).

 

var doc = app.activeDocument;
var sel = doc.selection;

var color = new RGBColor();
color.red = 255;
color.green = color.blue = 0;

var target, targetBounds, width, height, radius, replacement;
for (var i = 0; i < sel.length; i++) {
  // get info of the target rectangle (square)
  target = sel[i];
  targetBounds = target.geometricBounds;
  width = targetBounds[2] - targetBounds[0];
  height = targetBounds[1] - targetBounds[3];

  // calculate radius at 12% of `width`, or if you prefer to use `height`
  // just change the line below (doesn't matter if you are dealing with squares)
  radius = width * 0.12; // change to width to `height` if you prefer

  // create the replacement rounder rectangle
  replacement = doc.pathItems.roundedRectangle(
    targetBounds[1],
    targetBounds[0],
    width,
    height,
    radius,
    radius
  );
  replacement.fillColor = color;
  replacement.move(target, ElementPlacement.PLACEBEFORE);
}

 

Reddhare_Author
Known Participant
September 5, 2024

Thank you so much, this worked exactly as I wanted!