This worked out great to get the data matrix scannable. Any chance you could provide some insight as to where in the script I need to place coordinates so I can have it moved to a specific spot? I'm looking at having it near the bottom right corner of my layer 1 art if that helps at all.
Just want to thank you very much for what you've contributed as I've been racking my head on this issue for 2 weeks. Scripting is definitely not my forte however I learn better from examples and then applying it.
Okay, I updated the `drawDataMatrix` function to draw the data matrix on "Layer 1" at the bottom-right of the current artboard. If you want to use another layer just adjust the variable `defaultLayerName`. I have also included built-in padding so that the data matrix is padded from the bottom right. Currently, it is set to 36 points but if you want to adjust the amount, just change the `defaultPadding` variable.
Please note, if you run this script more than once in the same file, you will get multiple data matrix shapes.
function drawDataMatrix(str) {
// setup defaults
var defaultLayerName = "Layer 1";
var defaultGroupName = "Matrix";
var defaultRectangleScale = 10;
var defaultPadding = 36;
// convert the datamatrix string into an array
var matrix = str.split("\n");
// setup colors
var zeroColor = new GrayColor();
zeroColor.gray = 100; // black
var oneColor = new GrayColor();
oneColor.gray = 0; // white
// setup a layer and a group to hold the art
var layer, group;
try {
layer = app.activeDocument.layers.getByName(defaultLayerName);
} catch (e) {
layer = app.activeDocument.layers.add();
layer.name = defaultLayerName;
}
group = layer.groupItems.add();
group.name = defaultGroupName;
// calculate start x,y position for bottom-right placement with padding
var startX =
app.activeDocument.width -
matrix[0].length * defaultRectangleScale -
defaultPadding;
var startY =
-app.activeDocument.height + matrix.length * defaultRectangleScale + defaultPadding;
// iterate over the matrix and draw the rectangles
var rec;
for (var y = 0; y < matrix.length; y++) {
for (var x = 0; x < matrix[y].length; x++) {
rec = group.pathItems.rectangle(
startY - y * defaultRectangleScale, // top
startX + x * defaultRectangleScale, // left
defaultRectangleScale, // width
defaultRectangleScale // height
);
rec.filled = true;
rec.stroked = false;
rec.fillColor = matrix[y][x] === "0" ? zeroColor : oneColor;
}
}
}