Copy link to clipboard
Copied
I'm trying to make a script that will insert a 1pt thick keyline at a user specified distance form the outside of all corners of the artboard, either 1" or 2 " long. I've been trying using chatgpt but it just can't get the keyline inside the artboard. I tried using just one keyline, but would need it on all 4 corners of the document, as per the attached simplified proof. I don't know if it would make any difference or not, but some of these documents will be using the larger artboard (ofetn theyre around 600" w x 100" h)
The last code before I got too frustrated is:
// Function to convert inches to Illustrator points (72 points per inch)
function inchesToPoints(inches) {
return inches * 72;
}
// Function to create a 1pt black keyline inside the visible area of the artboard
function createKeyline(positionInInches, heightInInches) {
var doc = app.activeDocument;
var activeArtboard = doc.artboards[doc.artboards.getActiveArtboardIndex()];
var artboardRect = activeArtboard.artboardRect; // Artboard bounds
// Convert position and height to points
var positionInPoints = inchesToPoints(positionInInches);
var heightInPoints = inchesToPoints(heightInInches);
// Calculate positions for the keyline
var xPosition = artboardRect[0] + positionInPoints; // X position for the vertical line
var topPosition = artboardRect[1] + 1; // Top position inside artboard
var bottomPosition = topPosition + heightInPoints; // Bottom position
// Ensure the xPosition stays within the artboard bounds
if (xPosition < artboardRect[0]) {
xPosition = artboardRect[0];
} else if (xPosition > artboardRect[2]) {
xPosition = artboardRect[2];
}
// Create a new path item for the keyline
var keylinePath = doc.pathItems.add();
keylinePath.setEntirePath([
[xPosition, topPosition],
[xPosition, bottomPosition]
]);
// Style the keyline (black stroke, 1pt width)
keylinePath.stroked = true;
keylinePath.strokeWidth = 1; // 1pt stroke width
keylinePath.strokeColor = doc.swatches.getByName("Black").color;
}
// Prompt user for position and height
function promptForKeylineDetails() {
var position = parseFloat(prompt("Enter position in inches from left or right:", "1.0"));
var height = parseFloat(prompt("Enter height of the keyline in inches (1 or 2):", "1.0"));
// Check if valid input
if (!isNaN(position) && !isNaN(height) && (height === 1 || height === 2)) {
createKeyline(position, height);
} else {
alert("Invalid input. Please enter valid numbers (position in inches and height as 1 or 2 inches).");
}
}
// Run the script
promptForKeylineDetails();
Here's a real quick go:
(function () {
var doc = app.activeDocument,
docScale = doc.scaleFactor;
// Function to convert inches to Illustrator points (72 points per inch)
function inchesToPoints(inches) {
return inches * 72 / doc.scaleFactor;
}
// Function to create a 1pt black keyline inside the visible area of the artboard
function createKeylines(xOffsetInches, lengthInches) {
var activeArtboard = doc.artboards[doc.artboards.getActiveArtboar
...
Copy link to clipboard
Copied
Here's a real quick go:
(function () {
var doc = app.activeDocument,
docScale = doc.scaleFactor;
// Function to convert inches to Illustrator points (72 points per inch)
function inchesToPoints(inches) {
return inches * 72 / doc.scaleFactor;
}
// Function to create a 1pt black keyline inside the visible area of the artboard
function createKeylines(xOffsetInches, lengthInches) {
var activeArtboard = doc.artboards[doc.artboards.getActiveArtboardIndex()];
var artboardRect = activeArtboard.artboardRect; // Artboard bounds
// Convert position and height to points
var xOffset = inchesToPoints(xOffsetInches);
var yOffset = inchesToPoints(lengthInches);
// Calculate positions for the keyline
var x1 = artboardRect[0] + xOffset;
var x2 = artboardRect[2] - xOffset;
var topY1 = artboardRect[1];
var topY2 = topY1 - yOffset;
var bottomY1 = artboardRect[3] + yOffset;
var bottomY2 = artboardRect[3];
// top two lines
drawLine([x1, topY1], [x1, topY2]);
drawLine([x2, topY1], [x2, topY2]);
// bottom two lines
drawLine([x1, bottomY1], [x1, bottomY2]);
drawLine([x2, bottomY1], [x2, bottomY2]);
}
// Prompt user for position and height
function promptForKeylineDetails() {
var position = parseFloat(prompt("Enter position in inches from left or right:", "1.0"));
var height = parseFloat(prompt("Enter height of the keyline in inches (1 or 2):", "1.0"));
// Check if valid input
if (!isNaN(position) && !isNaN(height) && (height === 1 || height === 2)) {
createKeylines(position, height);
} else {
alert("Invalid input. Please enter valid numbers (position in inches and height as 1 or 2 inches).");
}
}
function drawLine(p1, p2) {
// Create a new path item for the keyline
var keylinePath = doc.pathItems.add();
keylinePath.setEntirePath([p1, p2]);
// Style the keyline (black stroke, 1pt width)
keylinePath.stroked = true;
keylinePath.strokeWidth = 1 / doc.scaleFactor; // 1pt stroke width
keylinePath.strokeColor = doc.swatches.getByName("Black").color;
};
// Run the script
promptForKeylineDetails();
})();
Edit 2024-06-21: added doc.scaleFactor to calculations to handle large artboards.
Copy link to clipboard
Copied
Absoliutely perfect - for smaller documents! Thanks so much! On larger ones (i did a test at 600"x100"), it creates keylines 10x the size and 10x the indents.
Copy link to clipboard
Copied
Sorry, I fixed this now. See updated script above. - Mark
Copy link to clipboard
Copied
I've kind of sorted it out - changing the return inches * 72 to 7.2 and making the keylinePath.strokeWidth to 0.1 means it works on large docs. So I've got two versions, one named for small docs and one for larger ones.
thanks so much for your skills on this one!
Copy link to clipboard
Copied
even more perfect!! Thanks so much!
Copy link to clipboard
Copied
You may also try Sergey Anosov's MakeCropMarks script.
It's pretty versatile, but currently it is also not prepared for large canvas documents. You would have to tweak it.
Copy link to clipboard
Copied
thanks, that's interesting, but the one above is absolutely perfect for my needs - other than the large canvas problem.