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