Highlighted

How to read values of an imported CSV in Illustrator

New Here ,
Oct 12, 2020

Copy link to clipboard

Copied

Hi,

 

I'm new to scripting in Illustrator and I can't seem to figure out this simple thing:

 

I have a .csv file that contains several variables. One the variables would be mapColor. In the csv I put 'mapColor' in row 1 and the corresponding value (a string containing a hex value) in the rows underneath. 

Programmatically I would like to access the mapColor values, so that I can assign a hex value to the background of a path item. But I can't seem to get the values of a specific variable.

const country = app.activeDocument.pathItems.getByName('country-fill-is');

const rgbValues = hexToRgb(`#${app.activeDocument.variables.getByName('mapColor')}`);
const colorRGB = new RGBColor();
color.red = rgbValues.r;
color.green = rgbValues.g;
color.blue = rgbValues.b;

country.fillColor = color;

function hexToRgb(hex) {
  const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  return result ? {
    r: parseInt(result[1], 16),
    g: parseInt(result[2], 16),
    b: parseInt(result[3], 16)
  } : null;
}

 

I would think I'd get the variable object value with variables.getByName(), but it only returns the variable name. Any ideas on how to get an object value by variable name?

 

Adobe Community Professional
Correct answer by Silly-V | Adobe Community Professional

Are you using the variable data feature with the datasets, or do you want to read from a csv directly?

If you are using the variables and are importing your csv as datasets, you will have to actually have a text box on the artboard that is bound to a variable so that you can view its displayed contents. This textbox can be hidden and the way it would work is you'd be able to grab the value of that text box after the dataset is changed.

But if you are not looking to use datasets and the batch processing feature and would rather read from the csv directly, this code can get you started:

	var csvFile = File.openDialog("Choose CSV file.", "*.csv");
	var csvContents = "";
	if (csvFile) {
		csvFile.open('r');
		csvContents = csvFile.read();
		csvFile.close();
	        var csvData = csvContents.split(/[\r\n]+/g);
	        var thisRow, firstColValue;
	        for (var i = 0; i < csvData.length; i++) {
		    thisRow = csvData[i];
		    csvData[i] = thisRow.split(",");
                    // skip the header row
                    if (i > 0) {
                        firstColValue = csvData[i][0];
                        // do stuff
                    }
	        }
        }
TOPICS
Scripting

Views

47

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

How to read values of an imported CSV in Illustrator

New Here ,
Oct 12, 2020

Copy link to clipboard

Copied

Hi,

 

I'm new to scripting in Illustrator and I can't seem to figure out this simple thing:

 

I have a .csv file that contains several variables. One the variables would be mapColor. In the csv I put 'mapColor' in row 1 and the corresponding value (a string containing a hex value) in the rows underneath. 

Programmatically I would like to access the mapColor values, so that I can assign a hex value to the background of a path item. But I can't seem to get the values of a specific variable.

const country = app.activeDocument.pathItems.getByName('country-fill-is');

const rgbValues = hexToRgb(`#${app.activeDocument.variables.getByName('mapColor')}`);
const colorRGB = new RGBColor();
color.red = rgbValues.r;
color.green = rgbValues.g;
color.blue = rgbValues.b;

country.fillColor = color;

function hexToRgb(hex) {
  const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  return result ? {
    r: parseInt(result[1], 16),
    g: parseInt(result[2], 16),
    b: parseInt(result[3], 16)
  } : null;
}

 

I would think I'd get the variable object value with variables.getByName(), but it only returns the variable name. Any ideas on how to get an object value by variable name?

 

Adobe Community Professional
Correct answer by Silly-V | Adobe Community Professional

Are you using the variable data feature with the datasets, or do you want to read from a csv directly?

If you are using the variables and are importing your csv as datasets, you will have to actually have a text box on the artboard that is bound to a variable so that you can view its displayed contents. This textbox can be hidden and the way it would work is you'd be able to grab the value of that text box after the dataset is changed.

But if you are not looking to use datasets and the batch processing feature and would rather read from the csv directly, this code can get you started:

	var csvFile = File.openDialog("Choose CSV file.", "*.csv");
	var csvContents = "";
	if (csvFile) {
		csvFile.open('r');
		csvContents = csvFile.read();
		csvFile.close();
	        var csvData = csvContents.split(/[\r\n]+/g);
	        var thisRow, firstColValue;
	        for (var i = 0; i < csvData.length; i++) {
		    thisRow = csvData[i];
		    csvData[i] = thisRow.split(",");
                    // skip the header row
                    if (i > 0) {
                        firstColValue = csvData[i][0];
                        // do stuff
                    }
	        }
        }
TOPICS
Scripting

Views

48

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 12, 2020 0
Adobe Community Professional ,
Oct 12, 2020

Copy link to clipboard

Copied

Are you using the variable data feature with the datasets, or do you want to read from a csv directly?

If you are using the variables and are importing your csv as datasets, you will have to actually have a text box on the artboard that is bound to a variable so that you can view its displayed contents. This textbox can be hidden and the way it would work is you'd be able to grab the value of that text box after the dataset is changed.

But if you are not looking to use datasets and the batch processing feature and would rather read from the csv directly, this code can get you started:

	var csvFile = File.openDialog("Choose CSV file.", "*.csv");
	var csvContents = "";
	if (csvFile) {
		csvFile.open('r');
		csvContents = csvFile.read();
		csvFile.close();
	        var csvData = csvContents.split(/[\r\n]+/g);
	        var thisRow, firstColValue;
	        for (var i = 0; i < csvData.length; i++) {
		    thisRow = csvData[i];
		    csvData[i] = thisRow.split(",");
                    // skip the header row
                    if (i > 0) {
                        firstColValue = csvData[i][0];
                        // do stuff
                    }
	        }
        }

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 12, 2020 1
New Here ,
Oct 12, 2020

Copy link to clipboard

Copied

Thanks man, that's just what I needed. Didn't know you could load a csv directly.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 12, 2020 1