Drawing a line according to the script

I’m trying to make a script that will automatically create tags and bind them to the line. Everything turns out except one thing - I can’t bind to an empty point of the orange line, both marks are constantly tied to the top point, although they must be tied to both edges. Can you help me solve this problem?

Below is my script

/*{
    "author": "Your Name",
    "targets": ["omnigraffle"],
    "type": "action",
    "identifier": "com.example.annotations",
    "version": "1.0",
    "description": "Cable Marking",
    "label": "Cable Marking",
    "mediumLabel": "Cable Marking",
    "longLabel": "Cable Marking",
    "paletteLabel": "Cable Marking"
}*/

(() => {
    var action = new PlugIn.Action(function(selection) {
        // Check if there is at least one selected line
        if (selection.lines.length === 0) {
            var notification = new Alert("Line Count", "No lines selected");
            notification.show();
            return;
        }

        // Iterate through all selected lines
        selection.lines.forEach(function(selectedLine) {
            // Get the points of the line
            var linePoints = selectedLine.points;

            // Check if there are at least two points
            if (linePoints.length < 2) {
                var notification = new Alert("Line Error", "Line must have at least two points");
                notification.show();
                return;
            }

            // Get the name of the line
            var lineName = selectedLine.name;

            // Calculate the position for the first rectangle, 50 points to the right of the upper end of the line
            var rectangleX1 = linePoints[0].x + 50;
            var rectangleY1 = linePoints[0].y - 12.5; // Half of the rectangle's height

            // Create a new shape (rectangle)
            var rectangle1 = document.windows[0].selection.canvas.newShape();
            rectangle1.shape = 'RoundRect';

            // Set the rectangle dimensions based on the line name
            rectangle1.geometry = new Rect(rectangleX1, rectangleY1, lineName.includes('.') ? 76 : 47, 22);

            // Set the rectangle's text as the line name
            rectangle1.text = lineName;

            // Set the textInset to make the text flush with the rectangle edges
            rectangle1.textVerticalPadding = 0;
            rectangle1.textHorizontalPadding = 0;

            // Set the rectangle's stroke color to match the line's stroke color
            rectangle1.fillColor = selectedLine.strokeColor;
            rectangle1.textColor = Color.RGB(255, 255, 255);
            rectangle1.strokeThickness = 0.5; // Set the line thickness

            // Remove shadow from the rectangle
            rectangle1.shadowColor = null;

            // Calculate the center of the first rectangle
            var rectangleCenterX1 = rectangleX1 + rectangle1.geometry.width / 2;
            var rectangleCenterY1 = rectangleY1 + rectangle1.geometry.height / 2;

            // Create a new line shape connecting the center of the first rectangle and the center of the selected line
            var canvas = document.windows[0].selection.canvas;
            var line1 = canvas.connect(rectangle1, selectedLine);
            line1.lineType = LineType.Curved; // Set the line type to Curved
            line1.shadowColor = null; // Remove shadow from the line
            line1.headType = 'FilledCenterBall';

            // Check if the lineName contains a dot
            if (lineName.includes('.')) {
                // Calculate the position for the second rectangle, 50 points to the left of the last point of the line

		var lastPointIndex = linePoints.length - 1;
		var lastPoint = linePoints[lastPointIndex];
		var rectangleX2 = lastPoint.x + 50;
		var rectangleY2 = lastPoint.y + 12.5; // Half of the rectangle's height

		// Create a new shape (rectangle)
		var rectangle2 = document.windows[0].selection.canvas.newShape();
		rectangle2.shape = 'RoundRect';

		// Set the rectangle dimensions based on the line name
		rectangle2.geometry = new Rect(rectangleX2, rectangleY2, lineName.includes('.') ? 76 : 47, 22);

		// Set the rectangle's text as the line name
		rectangle2.text = lineName;

		// Set the textInset to make the text flush with the rectangle edges
		rectangle2.textVerticalPadding = 0;
		rectangle2.textHorizontalPadding = 0;

		// Set the rectangle's stroke color to match the line's stroke color
		rectangle2.fillColor = selectedLine.strokeColor;
		rectangle2.textColor = Color.RGB(255, 255, 255);
		rectangle2.strokeThickness = 0.5; // Set the line thickness

		// Remove shadow from the rectangle
		rectangle2.shadowColor = null;

		// Calculate the center of the second rectangle
		var rectangleCenterX2 = rectangleX2 + rectangle2.geometry.width / 2;
		var rectangleCenterY2 = rectangleY2 + rectangle2.geometry.height / 2;

		// Create a new line shape connecting the center of the second rectangle and the end of the selected line
		var line2 = canvas.connect(rectangle2, selectedLine.linePoints[0]);
		line2.lineType = LineType.Curved; // Set the line type to Curved
		line2.shadowColor = null; // Remove shadow from the line
		line2.headType = 'FilledCenterBall';

            }
        });
    });

    return action;
})();

Problem with “var line2 = canvas.connect(rectangle2, selectedLine.linePoints[0]);”, there is no way to set the coordinates of the opposite point. The text field is normally placed at the second point, but the line always tends to the first

“selectedLine.linePoints[0]” corresponds to the coordinates of the first point. Instead I would use the variable “lastPoint” you defined further up in your script.
Hope this helps.

Thanks for the answer

I tried different options, I get such errors, I listed some. it turns out to bind rectangle2 to different points of the line, but it doesn’t work at the opposite end

var line2 = canvas.connect(rectangle2, lastPoint); - Error: Function Canvas.connect argument “to” at index 1 requires a value of type Graphic, but was given a value of type Point

var line2 = canvas.connect(rectangle2, selectedLine.lastPoint); - Error: Function Canvas.connect argument “to” at index 1 requires a non-null value

var line2 = canvas.connect(rectangle2, lastPointIndex); - Error: Function Canvas.connect argument “to” at index 1 requires a value of type Graphic, but was given a value of type Number

With any option, I get either the execution of the script with an error, or as on the left in the picture, and I want to get the right option

Did you get no error with “var line2 = canvas.connect(rectangle2, selectedLine.linePoints[0]);”? Here the second argument is also a point and not a Graphic.

That’s the mistake - TypeError: undefined is not an object (evaluating ‘selectedLine.linePoints[0]’)

The problem is still relevant, if someone can help, it will be great, I will be very grateful