Skip to main content
Participant
January 17, 2025
Answered

share a script for sign production

  • January 17, 2025
  • 2 replies
  • 398 views

Im looking for script that I can run, that will draw a cross-hair in the center of every circle in my file. this is for precise drilling locations where the holes are marked with a circle. thanks

Correct answer jduncan

I already had a script that performs something similar to what you described (for sign production as well) so I simplified it and added defaults that you can easily adjust to change the crosshair color, width, height, and weight.

 

A few things to note:

  1. The script requires you to select the objects (circles) you want to mark the center of
  2. The script doesn't check to ensure any of the selected objects are actual circles so it will place a center mark on anything that is selected.
  3. You need to adjust the defaults to match your requirements
  4. The script utilizes unit values (docs) so you can enter things like ".125 in", "3 cm", "4 pt", for the default values.
  5. All crosshair marks are grouped by their parts and placed on a separate layer.

 

Let me know if this does what you need or if you have any questions/issues? Cheers!

// Draw a crosshair mark at the center of each selected object.

(function () {
  // get the active document
  try {
    var doc = app.activeDocument;
  } catch (e) {
    alert("No active document.\n" + e);
    return;
  }

  // get the document selection
  var sel = doc.selection;
  if (!selection.length) {
    alert("Empty selection.\nMake a selection and rerun the script.");
    return;
  }

  // setup script defaults
  var marksLayerName = "Center Crosshair Marks";
  var crosshairStrokeWeight = ".0625 in";
  var crosshairWidth = ".5 in";
  var crosshairHeight = ".25 in";
  var crosshairStrokeColor = new RGBColor();
  crosshairStrokeColor.red = 255;
  crosshairStrokeColor.green = 0;
  crosshairStrokeColor.blue = 0;

  // convert default unit values to points
  var crosshairStrokeWeight = UnitValue(crosshairStrokeWeight).as("pt");
  var crosshairWidth = UnitValue(crosshairWidth).as("pt");
  var crosshairHeight = UnitValue(crosshairHeight).as("pt");

  // create a layer to hold the crosshair marks
  var crosshairLayer = createLayer(marksLayerName);

  // draw center crosshair mark for each selected object
  var bounds, centerX, centerY;
  for (var i = 0; i < sel.length; i++) {
    bounds = sel[i].geometricBounds; // [left, top, right, bottom]
    var centerX = bounds[0] + (bounds[2] - bounds[0]) / 2;
    var centerY = -(bounds[1] - (bounds[1] - bounds[3]) / 2);
    drawCrosshair(
      crosshairLayer,
      centerX,
      centerY,
      crosshairWidth,
      crosshairHeight,
      crosshairStrokeColor,
      crosshairStrokeWeight
    );
  }

  /**
   * ************************************
   *           HELPER FUNCTIONS
   * ************************************
   */
  function createLayer(name) {
    var layer;
    try {
      layer = doc.layers.getByName(name);
    } catch (e) {
      layer = doc.layers.add();
      layer.name = name;
    }
    return layer;
  }

  function drawCrosshair(layer, x, y, width, height, strokeColor, strokeWeight) {
    // make a group to hold the parts
    var crosshair = layer.groupItems.add();
    // draw x-line
    var xLine = crosshair.pathItems.add();
    xLine.setEntirePath([
      [x - width / 2, -y],
      [x + width / 2, -y],
    ]);
    xLine.strokeColor = strokeColor;
    xLine.stroked = true;
    xLine.strokeWidth = strokeWeight;
    xLine.filled = false;
    // draw y-line
    var yLine = crosshair.pathItems.add();
    yLine.setEntirePath([
      [x, -y + height / 2],
      [x, -y - height / 2],
    ]);
    yLine.strokeColor = strokeColor;
    yLine.stroked = true;
    yLine.strokeWidth = strokeWeight;
    yLine.filled = false;
  }
})();

 

2 replies

Kurt Gold
Community Expert
Community Expert
January 17, 2025

There are some non-scripting ways, for example Global Edit.

 

You may share a sample Illustrator file, so one can check if that would work.

jduncan
Community Expert
jduncanCommunity ExpertCorrect answer
Community Expert
January 17, 2025

I already had a script that performs something similar to what you described (for sign production as well) so I simplified it and added defaults that you can easily adjust to change the crosshair color, width, height, and weight.

 

A few things to note:

  1. The script requires you to select the objects (circles) you want to mark the center of
  2. The script doesn't check to ensure any of the selected objects are actual circles so it will place a center mark on anything that is selected.
  3. You need to adjust the defaults to match your requirements
  4. The script utilizes unit values (docs) so you can enter things like ".125 in", "3 cm", "4 pt", for the default values.
  5. All crosshair marks are grouped by their parts and placed on a separate layer.

 

Let me know if this does what you need or if you have any questions/issues? Cheers!

// Draw a crosshair mark at the center of each selected object.

(function () {
  // get the active document
  try {
    var doc = app.activeDocument;
  } catch (e) {
    alert("No active document.\n" + e);
    return;
  }

  // get the document selection
  var sel = doc.selection;
  if (!selection.length) {
    alert("Empty selection.\nMake a selection and rerun the script.");
    return;
  }

  // setup script defaults
  var marksLayerName = "Center Crosshair Marks";
  var crosshairStrokeWeight = ".0625 in";
  var crosshairWidth = ".5 in";
  var crosshairHeight = ".25 in";
  var crosshairStrokeColor = new RGBColor();
  crosshairStrokeColor.red = 255;
  crosshairStrokeColor.green = 0;
  crosshairStrokeColor.blue = 0;

  // convert default unit values to points
  var crosshairStrokeWeight = UnitValue(crosshairStrokeWeight).as("pt");
  var crosshairWidth = UnitValue(crosshairWidth).as("pt");
  var crosshairHeight = UnitValue(crosshairHeight).as("pt");

  // create a layer to hold the crosshair marks
  var crosshairLayer = createLayer(marksLayerName);

  // draw center crosshair mark for each selected object
  var bounds, centerX, centerY;
  for (var i = 0; i < sel.length; i++) {
    bounds = sel[i].geometricBounds; // [left, top, right, bottom]
    var centerX = bounds[0] + (bounds[2] - bounds[0]) / 2;
    var centerY = -(bounds[1] - (bounds[1] - bounds[3]) / 2);
    drawCrosshair(
      crosshairLayer,
      centerX,
      centerY,
      crosshairWidth,
      crosshairHeight,
      crosshairStrokeColor,
      crosshairStrokeWeight
    );
  }

  /**
   * ************************************
   *           HELPER FUNCTIONS
   * ************************************
   */
  function createLayer(name) {
    var layer;
    try {
      layer = doc.layers.getByName(name);
    } catch (e) {
      layer = doc.layers.add();
      layer.name = name;
    }
    return layer;
  }

  function drawCrosshair(layer, x, y, width, height, strokeColor, strokeWeight) {
    // make a group to hold the parts
    var crosshair = layer.groupItems.add();
    // draw x-line
    var xLine = crosshair.pathItems.add();
    xLine.setEntirePath([
      [x - width / 2, -y],
      [x + width / 2, -y],
    ]);
    xLine.strokeColor = strokeColor;
    xLine.stroked = true;
    xLine.strokeWidth = strokeWeight;
    xLine.filled = false;
    // draw y-line
    var yLine = crosshair.pathItems.add();
    yLine.setEntirePath([
      [x, -y + height / 2],
      [x, -y - height / 2],
    ]);
    yLine.strokeColor = strokeColor;
    yLine.stroked = true;
    yLine.strokeWidth = strokeWeight;
    yLine.filled = false;
  }
})();

 

Participant
January 20, 2025

perfect! thanks!

 

Sergey Osokin
Inspiring
January 17, 2025

Do you manually select the circles before running the script? Because it's easier than having the script analyze every shape in the document to see if it's a circle.

 

What crosshair settings are preferred? You may need some stroke color, stroke weight, line length (pixels, millimeters, or other units).