Oops, it's working!
It seems that the problem has been solved by loading the updated script via File / Script / Other File... option. Before the stroke width change wasn't affecting the result. I guess it was some caching. I'll post the answer anyway, maybe it will be useful for someone.

Original question:

The purpose of the following Illustrator JavaScript is to create a rotated rectangle that spans the selected line and place these object in the middle of it:

  1. a small circle
  2. a vertical line rotated the same way as the rectangle
  3. a vertical line rotated the same way as the rectangle

I'm doing it by rotating the selected line with a reversed angle, creating objects and rotate everything back.

The problem is that the objects are not centered, as you can see below. It seems that the degree of misalignmet depends on a with of the selected line, but even with a zero width there is a problem. I tried to apply a zero width stroke to each object but it doesn't seem to work. They have some predefined width that I don't know how to change. Please help.
enter image description here
enter image description here

#target Illustrator

// Define the new color 1
var newRGBColor = new RGBColor();
newRGBColor.red = 255;
newRGBColor.green = 0;
newRGBColor.blue = 0;
app.activeDocument.defaultFillColor = newRGBColor;

// Define the new color 2
var newRGBColor_Green = new RGBColor();
newRGBColor_Green.red = 0;
newRGBColor_Green.green = 255;
newRGBColor_Green.blue = 0;

// Define the new color 3
var newRGBColor_Black = new RGBColor();
newRGBColor_Black.red = 0;
newRGBColor_Black.green = 0;
newRGBColor_Black.blue = 0;

var idoc = app.activeDocument;
// This doesn't seem to work
idoc.defaultStrokeWidth = 55;

// My desired angle
var ang = 11.947;

// Stroke withs and element sizes.
// 1 point = 1p = 1/2660 m = 0.37594 mm
var ellRadius = 0.566/2.0/0.37594;
var ellDiameter = ellRadius*2.0;
var ellStrokeWidth = ellRadius/10.0;
var zeroStrokeWidth = 0.0001;
var defaultStrokeWidth = ellRadius;

var mySelection = new Object; 

// Get active selection
selections = idoc.selection;

    mySelection = idoc.selection[i];


    var newRectWidth = mySelection.width; // width is Geometric width, so we need to make it smaller...to accomodate the visible portion.  
    var newRectHeight = mySelection.height;  
    var newRectTop = mySelection.top; // Top is actually Visible top  
    var newRectLeft = mySelection.left; // dito for Left

    //mySelection.remove();container.pathItems. (top, left, width, height, undefined, true); 

    // Create a rectangle
    var rect1Full = idoc.pathItems.rectangle(newRectTop, newRectLeft, newRectWidth, newRectHeight);
    rect1Full.strokeWidth = zeroStrokeWidth;
    rect1Full.strokeColor = newRGBColor; // defined above
    rect1Full.fillColor = new NoColor();

    // Create a vertical line (by reducing rectangle's width)
    var rect2Vert = idoc.pathItems.rectangle(newRectTop, newRectLeft, newRectWidth, newRectHeight); 
    rect2Vert.strokeWidth = zeroStrokeWidth;
    rect2Vert.strokeColor = newRGBColor; // defined above
    rect2Vert.fillColor = new NoColor();        

    // Create a horizontal line (by reducing rectangle's height)
    var rect3Horiz = idoc.pathItems.rectangle(newRectTop, newRectLeft, newRectWidth, newRectHeight);
    rect3Horiz.strokeWidth = zeroStrokeWidth;
    rect3Horiz.strokeColor = newRGBColor; // defined above
    rect3Horiz.fillColor = new NoColor();       

    // Create an ellipse
    var ellip1 = idoc.pathItems.ellipse(rect1Full.top-rect1Full.height/2 +ellRadius, rect1Full.left+rect1Full.width/2.0 - ellRadius, ellDiameter, ellDiameter, undefined, true);
    ellip1.strokeWidth = zeroStrokeWidth;
    ellip1.strokeColor = newRGBColor_Black; // defined above
    ellip1.fillColor = newRGBColor_Green;
    ellip1.strokeWidth = ellStrokeWidth;

alert(selections.length+" objects replaced");
  • @CAI Oops, I'm sorry, I've just done it.
    – ellockie
    Commented Apr 5, 2016 at 11:55

1 Answer 1


I'm sorry for the confusion, but the script is working after a small correction:

// This doesn't seem to work
idoc.defaultStrokeWidth = 55;

// This does
idoc.defaultStrokeWidth = 0;

Before I put this huge number to check if it makes any difference, but there wasn't any. Later, after loading the script via "File / Script / Other File..." option the defaultStrokeWidth suddenly the new stroke started to be visible. And that seemed to be the root of the original problem. Like the visual boundaries are taken into consideration by Illustrator with the operations used, not path points positions.

So, also it seems that Illustrator was caching my script this time, although usually it doesn't. Strange.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.