Skip to main content
Participating Frequently
December 18, 2022
Question

Can somone look at this script and help?

  • December 18, 2022
  • 1 reply
  • 419 views

Can somone look at this code and tell me why its not drawing the black .25" circles as expected?  My goal with this script is to let the user choose either top and bottom cutter marks (for a digital flatbed Colex cuttert) and draw the circles .35: in diameter and place them .25" frin from the edge pof the artboard if the user chooses top/bottom it will draw 3 evenly spaced circles evenly distributed across the top and 4 across the  bottom.  Same goes if they choos left and right it draws them 3 on left and 4 on right? Im stuck.  The script seems to run but draws nothing in the document:

 

var y = y2;
var ellipse = doc.pathItems.ellipse(x, y, diameter, diameter);
ellipse.filled = true;
ellipse.fillColor = new RGBColor();
ellipse.fillColor.red = 0;
ellipse.fillColor.green = 0;
ellipse.fillColor.blue = 0;
ellipse.stroked = false;
}
}

// Close the UI panel when the Cancel button is clicked
cancelButton.onClick = function() {
panel.close();
}

This topic has been closed for replies.

1 reply

Participating Frequently
December 18, 2022

// Create the UI panel
var panel = new Window('dialog', 'Colex Cutter Marks');

// Add a radio button group for the Margins options
var marginsGroup = panel.add('group');
marginsGroup.orientation = 'row';
var topBottomRadio = marginsGroup.add('radiobutton', undefined, 'Top/Bottom');
var leftRightRadio = marginsGroup.add('radiobutton', undefined, 'Left/Right');

// Add the OK and Cancel buttons
var buttonsGroup = panel.add('group');
buttonsGroup.alignChildren = 'center';
var okButton = buttonsGroup.add('button', undefined, 'OK');
var cancelButton = buttonsGroup.add('button', undefined, 'Cancel');

// Show the UI panel
panel.show();

// Run the specified code when the OK button is clicked
okButton.onClick = function() {
// Get the active document and its artboard size
var doc = app.activeDocument;
var artboardRect = doc.artboards[doc.artboards.getActiveArtboardIndex()].artboardRect;

// Determine the diameter and position of the cutter marks
var diameter = 18; // .25 inches
var offset = 18; // .25 inches
var x1, x2, y1, y2;
if (topBottomRadio.value) {
x1 = artboardRect[0] + offset;
x2 = artboardRect[2] - offset;
y1 = artboardRect[1] + offset;
y2 = artboardRect[3] - offset;
} else if (leftRightRadio.value) {
x1 = artboardRect[0] + offset;
x2 = artboardRect[2] - offset;
y1 = artboardRect[1] + offset;
y2 = artboardRect[3] - offset;
}

// Calculate the number and spacing of the cutter marks
var numMarks = 3;
var spacing = (x2 - x1 - diameter * numMarks) / (numMarks - 1);

// Draw the cutter marks along the top or left of the artboard
for (var i = 0; i < numMarks; i++) {
var x = x1 + i * (diameter + spacing);
var y = y1;
var ellipse = doc.pathItems.ellipse(x, y, diameter, diameter);
ellipse.filled = true;
ellipse.fillColor = new RGBColor();
ellipse.fillColor.red = 0;
ellipse.fillColor.green = 0;
ellipse.fillColor.blue = 0;
ellipse.stroked = false;
}

// Calculate the number and spacing of the cutter marks
numMarks = 4;
spacing = (x2 - x1 - diameter * numMarks) / (numMarks - 1);

// Draw the cutter marks along the bottom or right of the artboard
for (var i = 0; i < numMarks; i++) {
var x = x1 + i * (diameter + spacing);
var y = y2;
var ellipse = doc.pathItems.ellipse(x, y, diameter, diameter);
ellipse.filled = true;
ellipse.fillColor = new RGBColor();
ellipse.fillColor.red = 0;
ellipse.fillColor.green = 0;
ellipse.fillColor.blue = 0;
ellipse.stroked = false;
}
}

// Close the UI panel when the Cancel button is clicked
cancelButton.onClick = function() {
panel.close();
}

 

m1b
Community Expert
Community Expert
December 18, 2022

Hi @chrish93067135, you're really close, so well done. I've made some small tweaks below. Have a look through and see what I did.

 

// Create the UI panel
var panel = new Window('dialog', 'Colex Cutter Marks');
// Add a radio button group for the Margins options
var marginsGroup = panel.add('group');
marginsGroup.orientation = 'row';
var topBottomRadio = marginsGroup.add('radiobutton', undefined, 'Top/Bottom');
var leftRightRadio = marginsGroup.add('radiobutton', undefined, 'Left/Right');
// Add the OK and Cancel buttons
var buttonsGroup = panel.add('group');
buttonsGroup.alignChildren = 'center';
var okButton = buttonsGroup.add('button', undefined, 'OK');
var cancelButton = buttonsGroup.add('button', undefined, 'Cancel');
// event handling
okButton.onClick = function () { panel.close(1) };
cancelButton.onClick = function () { panel.close(2) };
// Show the UI panel
var result = panel.show();
// Run the specified code when the OK button is clicked
if (result == 1) {
    // Get the active document and its artboard size
    var doc = app.activeDocument;
    var artboardRect = doc.artboards[doc.artboards.getActiveArtboardIndex()].artboardRect;
    // Determine the diameter and position of the cutter marks
    var diameter = 18; // .25 inches
    var offset = 18; // .25 inches
    var x1, x2, y1, y2;
    if (topBottomRadio.value) {
        x1 = artboardRect[0] + offset;
        x2 = artboardRect[2] - offset;
        y1 = artboardRect[1] + offset;
        y2 = artboardRect[3] - offset;
    } else if (leftRightRadio.value) {
        x1 = artboardRect[0] + offset;
        x2 = artboardRect[2] - offset;
        y1 = artboardRect[1] + offset;
        y2 = artboardRect[3] - offset;
    }
    // Calculate the number and spacing of the cutter marks
    var numMarks = 3;
    var spacing = (x2 - x1 - diameter * numMarks) / (numMarks - 1);
    // Draw the cutter marks along the top or left of the artboard
    for (var i = 0; i < numMarks; i++) {
        var x = x1 + i * (diameter + spacing);
        var y = y1;
        var ellipse = doc.pathItems.ellipse(x, y, diameter, diameter);
        ellipse.filled = true;
        ellipse.fillColor = new RGBColor();
        ellipse.fillColor.red = 0;
        ellipse.fillColor.green = 0;
        ellipse.fillColor.blue = 0;
        ellipse.stroked = false;
    }
    // Calculate the number and spacing of the cutter marks
    numMarks = 4;
    spacing = (x2 - x1 - diameter * numMarks) / (numMarks - 1);
    // Draw the cutter marks along the bottom or right of the artboard
    for (var i = 0; i < numMarks; i++) {
        var x = x1 + i * (diameter + spacing);
        var y = y2;
        var ellipse = doc.pathItems.ellipse(x, y, diameter, diameter);
        ellipse.filled = true;
        ellipse.fillColor = new RGBColor();
        ellipse.fillColor.red = 0;
        ellipse.fillColor.green = 0;
        ellipse.fillColor.blue = 0;
        ellipse.stroked = false;
    }
}

 

I've restructured the way you do the UI panel result so that it handles the result after the UI is closed. This is a nice clean way to do it, but if you expect the panel to stay open, then you need to move the okButton.onClick above the panel.show() line. Let me know if this is enough for you to go on for now.

- Mark

m1b
Community Expert
Community Expert
December 18, 2022

For contrast, this version will keep the panel open after drawing the cutter marks:

// Create the UI panel
var panel = new Window('dialog', 'Colex Cutter Marks');
// Add a radio button group for the Margins options
var marginsGroup = panel.add('group');
marginsGroup.orientation = 'row';
var topBottomRadio = marginsGroup.add('radiobutton', undefined, 'Top/Bottom');
var leftRightRadio = marginsGroup.add('radiobutton', undefined, 'Left/Right');
// Add the OK and Cancel buttons
var buttonsGroup = panel.add('group');
buttonsGroup.alignChildren = 'center';
var okButton = buttonsGroup.add('button', undefined, 'OK');
var cancelButton = buttonsGroup.add('button', undefined, 'Cancel');
cancelButton.onClick = function () { panel.close() };
okButton.onClick = drawCutterMarks;
// Show the UI panel
panel.show();


// Run the specified code when the OK button is clicked
function drawCutterMarks() {
    // Get the active document and its artboard size
    var doc = app.activeDocument;
    var artboardRect = doc.artboards[doc.artboards.getActiveArtboardIndex()].artboardRect;
    // Determine the diameter and position of the cutter marks
    var diameter = 18; // .25 inches
    var offset = 18; // .25 inches
    var x1, x2, y1, y2;
    if (topBottomRadio.value) {
        x1 = artboardRect[0] + offset;
        x2 = artboardRect[2] - offset;
        y1 = artboardRect[1] + offset;
        y2 = artboardRect[3] - offset;
    } else if (leftRightRadio.value) {
        x1 = artboardRect[0] + offset;
        x2 = artboardRect[2] - offset;
        y1 = artboardRect[1] + offset;
        y2 = artboardRect[3] - offset;
    }
    // Calculate the number and spacing of the cutter marks
    var numMarks = 3;
    var spacing = (x2 - x1 - diameter * numMarks) / (numMarks - 1);
    // Draw the cutter marks along the top or left of the artboard
    for (var i = 0; i < numMarks; i++) {
        var x = x1 + i * (diameter + spacing);
        var y = y1;
        var ellipse = doc.pathItems.ellipse(x, y, diameter, diameter);
        ellipse.filled = true;
        ellipse.fillColor = new RGBColor();
        ellipse.fillColor.red = 0;
        ellipse.fillColor.green = 0;
        ellipse.fillColor.blue = 0;
        ellipse.stroked = false;
    }
    // Calculate the number and spacing of the cutter marks
    numMarks = 4;
    spacing = (x2 - x1 - diameter * numMarks) / (numMarks - 1);
    // Draw the cutter marks along the bottom or right of the artboard
    for (var i = 0; i < numMarks; i++) {
        var x = x1 + i * (diameter + spacing);
        var y = y2;
        var ellipse = doc.pathItems.ellipse(x, y, diameter, diameter);
        ellipse.filled = true;
        ellipse.fillColor = new RGBColor();
        ellipse.fillColor.red = 0;
        ellipse.fillColor.green = 0;
        ellipse.fillColor.blue = 0;
        ellipse.stroked = false;
    }
    // redraw screen so you can see the cutter marks
    app.redraw();
}