Skip to main content
Participant
May 16, 2021
Answered

Script for shortening paths

  • May 16, 2021
  • 1 reply
  • 1054 views

Hello - 

 

Does anyone know of a script for Illustrator that can shorten paths from the end points? The script would affect multiple paths on the same layer.

For example:

Paths on layer is 10 pts, 20 pts and 25 pts long.

Script prompts user for how much they want reduce length of path. 

User enters 2 pts

Paths are now 8 pts, 18 pts and 23 pts long and are located in the same position as it was previously (except a little shorter on each end)

Thanks for your help!

This topic has been closed for replies.
Correct answer femkeblanco
// select your lines before running
var diff = prompt("Decrease path length by (points):", "", " ");
var sel = app.activeDocument.selection;
for (var i = 0; i < sel.length; i++) {
    var h = sel[i].height,
    w = sel[i].width;
    var c = Math.sqrt(Math.pow(h, 2) + Math.pow(w, 2));
    var percentage = ((c - diff) / c) * 100;
    selection[i].resize(percentage, percentage, true, false, false, false, 0, Transformation.CENTER);
}

1 reply

CarlosCanto
Community Expert
Community Expert
May 16, 2021

no I haven't seen a script for such a task.

 

can you show the kind of paths you want to shorten? '

 

post a screen shot or better yet a sample ai file

Participant
May 16, 2021

Hello - thank you for your reply.  I made an example file showing a hypothetical before and after.  In my example, the lines are shorted by 10pts each, regardless of the original size of the line.  In addition, the angles of the lines remain the same (those lines that are not straight up and down).  In addition, the lines are shorted equalty from each end, so that the midpoint of the line is in the same location.  Apologes for the pdf file, but it would not let me an an AI file for some reason.

femkeblanco
femkeblancoCorrect answer
Legend
May 16, 2021
// select your lines before running
var diff = prompt("Decrease path length by (points):", "", " ");
var sel = app.activeDocument.selection;
for (var i = 0; i < sel.length; i++) {
    var h = sel[i].height,
    w = sel[i].width;
    var c = Math.sqrt(Math.pow(h, 2) + Math.pow(w, 2));
    var percentage = ((c - diff) / c) * 100;
    selection[i].resize(percentage, percentage, true, false, false, false, 0, Transformation.CENTER);
}