Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Need help creating and properly positioning trim marks relative to user-selected objects

Participant ,
Apr 05, 2024 Apr 05, 2024

Hey guys. I'm hoping someone can help me out here.

 

I'm trying to create a small script that takes a length and bleed value (for offset calculation) and creates trim marks in the corners of all user-selected objects based on those values. I'm really having trouble positioning these trim marks properly, as Illustrator's coordinate system is the most unintuitive thing ever. Especially since length has to be taken into account as well.

 

I know there's a very comprehensive script out there that does this, but it's huge and I'm unable to understand it deeply enough to extract this functionality from it. I need this trim mark creation part for another script I'm working on.

Hoping some of you might have some tips as to how to approach positioning these things. Thanks!

 

Pasting what I have so far here:

#target illustrator

function createTrimMarks(bleed, markLength) {
    if (app.documents.length > 0 && app.selection.length > 0) {
        var bleedPoints = bleed * 2.83464567; // Convert mm to points
        var lengthPoints = markLength * 2.83464567; // Convert mm to points
        var doc = app.activeDocument;

        for (var i = 0; i < app.selection.length; i++) {
            var item = app.selection[i];
            var bounds = item.geometricBounds; // [x1, y1, x2, y2]
            var x1 = bounds[0], y1 = bounds[1], x2 = bounds[2], y2 = bounds[3];

            // Calculate offset positions
            var offsetX = x1 - bleedPoints;
            var offsetY = y1 - bleedPoints;
            var endX = x2 + bleedPoints;
            var endY = y2 + bleedPoints;

            // Top-left corner
            drawMark(offsetX, offsetY, lengthPoints, true); // Vertical
            drawMark(offsetX, offsetY, lengthPoints, false); // Horizontal

            // Top-right corner
            drawMark(endX, offsetY, lengthPoints, true); // Vertical
            drawMark(endX - lengthPoints, offsetY, lengthPoints, false); // Horizontal

            // Bottom-left corner
            drawMark(offsetX, endY - lengthPoints, lengthPoints, true); // Vertical
            drawMark(offsetX, endY, lengthPoints, false); // Horizontal

            // Bottom-right corner
            drawMark(endX, endY - lengthPoints, lengthPoints, true); // Vertical
            drawMark(endX - lengthPoints, endY, lengthPoints, false); // Horizontal
        }
    } else {
        alert("No document open or nothing selected.");
    }
}

function drawMark(startX, startY, length, isVertical) {
    var doc = app.activeDocument;
    var path = doc.pathItems.add();
    path.setEntirePath([
        [startX, startY], 
        isVertical ? [startX, startY + length] : [startX + length, startY]
    ]);
    path.stroked = true;
    path.strokeWidth = 0.25;
    path.strokeColor = new CMYKColor();
    path.strokeColor.black = 100;
    path.filled = false;
}

function showDialog() {
    var dialog = new Window('dialog', 'Trim Marks Parameters');
    dialog.orientation = 'column';
    dialog.alignChildren = 'left';

    var bleedGroup = dialog.add('group');
    bleedGroup.add('statictext', undefined, 'Bleed Size (mm):');
    var bleedInput = bleedGroup.add('edittext', undefined, '3');
    bleedInput.characters = 5;

    var lengthGroup = dialog.add('group');
    lengthGroup.add('statictext', undefined, 'Mark Length (mm):');
    var lengthInput = lengthGroup.add('edittext', undefined, '5');
    lengthInput.characters = 5;

    var buttonGroup = dialog.add('group');
    buttonGroup.alignment = 'right';
    var okButton = buttonGroup.add('button', undefined, 'OK', { name: 'ok' });
    var cancelButton = buttonGroup.add('button', undefined, 'Cancel', { name: 'cancel' });

    okButton.onClick = function() {
        var bleed = parseFloat(bleedInput.text);
        var markLength = parseFloat(lengthInput.text);
        if (isNaN(bleed) || isNaN(markLength)) {
            alert('Both bleed size and mark length must be numbers.');
            return;
        }
        dialog.close();
        createTrimMarks(bleed, markLength);
    };

    dialog.show();
}

showDialog();

 

TOPICS
Scripting
188
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Apr 05, 2024 Apr 05, 2024

I see what you're trying to do with the offset calculations but they arent quite being used correctly and it might be confusing when trying to think that way. youre offsetting some values that shouldnt be offset like the X coord on a vertical line. 

 

i rewrote the drawMark values so they work now without using the offset and end vars

            // Top-left corner
            drawMark(x1, y1 + bleedPoints, lengthPoints, true); // Vertical
            drawMark(x1 - bleedPoints-lengthPoints, y1, len
...
Translate
Adobe
Participant ,
Apr 05, 2024 Apr 05, 2024

I see what you're trying to do with the offset calculations but they arent quite being used correctly and it might be confusing when trying to think that way. youre offsetting some values that shouldnt be offset like the X coord on a vertical line. 

 

i rewrote the drawMark values so they work now without using the offset and end vars

            // Top-left corner
            drawMark(x1, y1 + bleedPoints, lengthPoints, true); // Vertical
            drawMark(x1 - bleedPoints-lengthPoints, y1, lengthPoints, false); // Horizontal

            // Top-right corner
            drawMark(x2, y1 + bleedPoints, lengthPoints, true); // Vertical
            drawMark(x2+bleedPoints, y1, lengthPoints, false); // Horizontal

            // Bottom-left corner
            drawMark(x1, y2-bleedPoints-lengthPoints, lengthPoints, true); // Vertical
            drawMark(x1-bleedPoints-lengthPoints, y2, lengthPoints, false); // Horizontal

            // Bottom-right corner
            drawMark(x2, y2-bleedPoints-lengthPoints, lengthPoints, true); // Vertical
            drawMark(x2+bleedPoints, y2, lengthPoints, false); // Horizontal

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 06, 2024 Apr 06, 2024
LATEST

Thank you very much. Your solution wasn't exactly what I was looking for, but I suspect it's because I didn't explain my needs well enough. Regardless, I was able to figure out how the coordinate system works now using your simplified approach, and I managed to get it working for my use-case. I'll paste the code below for future reference if someone needs something like this. Thank you again!

      // Top-left corner
      drawMark(x1 + bleedPoints, y1, lengthPoints, true); // Vertical
      drawMark(x1 - lengthPoints, y1 - bleedPoints, lengthPoints, false); // Horizontal

      // Top-right corner
      drawMark(x2 - bleedPoints, y1, lengthPoints, true); // Vertical
      drawMark(x2, y1 - bleedPoints, lengthPoints, false); // Horizontal

      // Bottom-left corner
      drawMark(x1 + bleedPoints, y2 - lengthPoints, lengthPoints, true); // Vertical
      drawMark(x1 - lengthPoints, y2 + bleedPoints, lengthPoints, false); // Horizontal

      // Bottom-right corner
      drawMark(x2 - bleedPoints, y2 - lengthPoints, lengthPoints, true); // Vertical
      drawMark(x2, y2 + bleedPoints, lengthPoints, false); // Horizontal
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines