Copy link to clipboard
Copied
Hi everyone,
I'm currently utilizing this script (https://github.com/tualo/tualo-datamatrix/blob/master/lib/datamatrix.js) in Illustrator. I've however been tasked with transforming the output which is currently 0s and 1s into a scannable data matrix. I'm able to take the output of datamatrix.js and format using excel to create a working barcode but can't come close to replicating this in Adobe Illustrator.
I'd be fine if I could find a way to do an "if" this equals 0 then do white square else do black square. Even doing a format to change font formatting for 0 to be white font, white background and 1 is black font, black background. I've been trying to slice and dice this script and I'm definitely no expert when it comes to creating scripts in Adobe Illustrator.
Any help would be greatly appreciated. Thx 😊
After you generate your DataMatrix string, just call the `drawDataMatrix` function from the script below. Provide the `ascii` string as the only argument and you'll get a new layer with a "Matrix" group of rectangles representing the 0s and 1s as black and white shapes respectively.
There are some defaults you can adjust like the layer name, group name, and size of the shapes (currently 10 points). The grid of shapes is drawn at the top-left of the artboard but could be changed to easily if you
...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 mu
...Copy link to clipboard
Copied
Here's what it currently outputs:
This is what I'm expecting when all is said and done, used Excel to create this Data Matrix:
Copy link to clipboard
Copied
Replace "var Matrix", "Matrix.prototype" and "new Matrix" everywhere with any other variable name, for example "MatrixObj" - @Sergey Osokin
Remove the "export" statment at the end and add the following code below to the end: - @Silly-V
/*
... the datamatrix.js code pasted in above ...
*/
var doc = app.activeDocument;
var str = "ABCDEFG!";
var dm = new Datamatrix();
var ascii = dm.getDigit(str, false);
var newText = doc.textFrames.add();
newText.size = [500, 500];
newText.contents = ascii;
For my specific situation I'm testing with "var str = "L123456P";
Copy link to clipboard
Copied
After you generate your DataMatrix string, just call the `drawDataMatrix` function from the script below. Provide the `ascii` string as the only argument and you'll get a new layer with a "Matrix" group of rectangles representing the 0s and 1s as black and white shapes respectively.
There are some defaults you can adjust like the layer name, group name, and size of the shapes (currently 10 points). The grid of shapes is drawn at the top-left of the artboard but could be changed to easily if you needed it elsewhere.
Try it out and let me know if you have any questions?
// do you datamatrix stuff here
// var dm = new Datamatrix();
// var ascii = dm.getDigit(str, false);
// below I am just using your example from the forum
// it seems the datamatrix package gives you a string separated with new lines
// so I just split the string into an array
var ascii =
"00000000000000\
01010101010100\
01101001110110\
01001010100000\
01110010111010\
01101001100000\
01111011101110\
01110001101000\
01101110111010\
01111010001000\
01100011111010\
01100100010100\
01111111111110\
00000000000000";
// call the `drawDataMatrix` function to draw the shapes at the top-left of the artboard
drawDataMatrix(ascii);
function drawDataMatrix(str) {
// setup defaults
var defaultLayerName = "Matrix";
var defaultGroupName = defaultLayerName;
var defaultRectangleScale = 10;
// 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);
layer.groupItems.removeAll();
} catch (e) {
layer = app.activeDocument.layers.add();
layer.name = defaultLayerName;
}
group = layer.groupItems.add();
group.name = defaultGroupName;
// 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(
-y * defaultRectangleScale,
x * defaultRectangleScale,
defaultRectangleScale,
defaultRectangleScale
);
rec.filled = true;
rec.stroked = false;
rec.fillColor = matrix[y][x] === "0" ? zeroColor : oneColor;
}
}
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
}
}
}
Copy link to clipboard
Copied
This worked out great. Thanks for the assist! 😊:clinking_beer_mugs: