I have a CSV File with the following data:
10, 23, 55, 123, 150 ...
I have a Javascript File which I use in Adobe Illustrator.
The following code creates 10 rectangles that are each shifted by 20 pixels and placed on the artboard. So far it works.
var myDocument = app.activeDocument;
var artLayer = myDocument.activeLayer;
var mypos = 0;
var i = 0;
while (i < 10) {
var rect = artLayer.pathItems.rectangle( 0, mypos, 200, 600 );
i++;
mypos = mypos + 20;
}
Now I want to use the values from the CSV file for the coordinates/position of the rectangles instead of mypos + 20
. The open dialog appears and I can select the CSV file.So far it works.
Now I don’t know how to process the data from the CSV file and go through the array to use the values.
var csvFile = File.openDialog("Choose CSV file.", "*.csv");
var csvContents = "";
if (csvFile) {
csvFile.open('r');
csvContents = csvFile.read();
csvFile.close();
}
2
Answers
This is roughly what you’ll want to do:
To create one row of rectangles (i.e. all with the same
y
coordinates), you want to use afor
loop:Now of course you might want to draw rectangles using different
y
coordinates as well — in that case, you can use two for-loops, splitting your CSV file first into rows (using.split('n')
) then each row into individual coordinates (using.split(',')
). Then your code might look something like this:It can be something like this:
The result for the csv data
10,23,55,123,150
: