Skip to main content
Known Participant
December 24, 2024
Question

Illustrator javascript CSV to Array to pathItems coordinates

  • December 24, 2024
  • 1 reply
  • 240 views

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();
    }

 

This topic has been closed for replies.

1 reply

Participating Frequently
December 25, 2024

Hi,

Below is a sample code to convert the loaded csv into an array.

var csvFile = File.openDialog("Choose CSV file.", "*.csv");
var csvContents = "";
if (csvFile) {
   csvFile.open('r');
   csvContents = csvFile.read();
   csvFile.close();
   var positions = csvContents.split (',');
   for (var i=0;i<positions.length;i++){
       $.writeln(positions[i]);
   }
}