I'm very new to Illustrator Scripting. How to load data in csv file in to separate layers of illustrator file? the data should be formatted with separate font styles and sizes prior to insert in to layers.
sample CSV data:
L1,L2,L3,L4,L5,VL5,SewP,Ref,Fname,Old,VL1,VL2,VL3,VL4
2,30,00,DNP,X SHORT,XSHT,T,55L,55L_2_XSHT_T.pdf,55L_WP_SD_S_Data Set 1,UK/AUS/NZ • 2,EUR • 30,CAN/US • 00,
2,30,00,DNP,SHORT,SHT,T,55L,55L_2_SHT_T.pdf,55L_WP_SD_S_Data Set 2,UK/AUS/NZ • 2,EUR • 30,CAN/US • 00,
2,30,00,DNP,REGULAR,REG,T,55L,55L_2_REG_T.pdf,55L_WP_SD_S_Data Set 3,UK/AUS/NZ • 2,EUR • 30,CAN/US • 00,
2,30,00,DNP,MEDIUM,MED,T,55L,55L_2_MED_T.pdf,55L_WP_SD_S_Data Set 4,UK/AUS/NZ • 2,EUR • 30,CAN/US • 00,
2,30,00,DNP,LONG,LNG,T,55L,55L_2_LNG_T.pdf,55L_WP_SD_S_Data Set 5,UK/AUS/NZ • 2,EUR • 30,CAN/US • 00,
2,30,00,DNP,X LONG,XLNG,T,55L,55L_2_XLNG_T.pdf,55L_WP_SD_S_Data Set 6,UK/AUS/NZ • 2,EUR • 30,CAN/US • 00,
2,30,00,DNP,SHORT/REGULAR,SHT_REG,T,55L,55L_2_SHT_REG_T.pdf,55L_WP_SD_S_Data Set 7,UK/AUS/NZ • 2,EUR • 30,CAN/US • 00,
2,30,00,DNP,REGULAR/LONG,REG_LNG,T,55L,55L_2_REG_LNG_T.pdf,55L_WP_SD_S_Data Set 8,UK/AUS/NZ • 2,EUR • 30,CAN/US • 00,
2,30,00,DNP,LONG/X LONG,LNG_XLNG,T,55L,55L_2_LNG_XLNG_T.pdf,55L_WP_SD_S_Data Set 9,UK/AUS/NZ • 2,EUR • 30,CAN/US • 00,
4,32,0,152/176-76-84,X SHORT,XSHT,T,55L,55L_4_XSHT_T.pdf,55L_WP_SD_S_Data Set 10,UK/AUS/NZ • 4,EUR • 32,CAN/US • 0,RU • 152/176-76-84
4,32,0,152/176-76-84,SHORT,SHT,T,55L,55L_4_SHT_T.pdf,55L_WP_SD_S_Data Set 11,UK/AUS/NZ • 4,EUR • 32,CAN/US • 0,RU • 152/176-76-84
Final result has to be populated as below.
// data -------------------------------------------------------------
var csv = '''L1,L2,L3,L4,L5,VL5,SewP,Ref,Fname,Old,VL1,VL2,VL3,VL4
2,30,00,DNP,X SHORT,XSHT,T,55L,55L_2_XSHT_T.pdf,55L_WP_SD_S_Data Set 1,UK/AUS/NZ • 2,EUR • 30,CAN/US • 00,
2,30,00,DNP,SHORT,SHT,T,55L,55L_2_SHT_T.pdf,55L_WP_SD_S_Data Set 2,UK/AUS/NZ • 2,EUR • 30,CAN/US • 00,
2,30,00,DNP,REGULAR,REG,T,55L,55L_2_REG_T.pdf,55L_WP_SD_S_Data Set 3,UK/AUS/NZ • 2,EUR • 30,CAN/US • 00,
2,30,00,DNP,MEDIUM,MED,T,55L,55L_2_MED_T.pdf,55L_WP_SD_S_Data Set 4,UK/AUS/NZ • 2,EUR • 30,CAN/US • 00,
2,30,00,DNP,LONG,LNG,T,55L,55L_2_LNG_T.pdf,55L_WP_SD_S_Data Set 5,UK/AUS/NZ • 2,EUR • 30,CAN/US • 00,
2,30,00,DNP,X LONG,XLNG,T,55L,55L_2_XLNG_T.pdf,55L_WP_SD_S_Data Set 6,UK/AUS/NZ • 2,EUR • 30,CAN/US • 00,
2,30,00,DNP,SHORT/REGULAR,SHT_REG,T,55L,55L_2_SHT_REG_T.pdf,55L_WP_SD_S_Data Set 7,UK/AUS/NZ • 2,EUR • 30,CAN/US • 00,
2,30,00,DNP,REGULAR/LONG,REG_LNG,T,55L,55L_2_REG_LNG_T.pdf,55L_WP_SD_S_Data Set 8,UK/AUS/NZ • 2,EUR • 30,CAN/US • 00,
2,30,00,DNP,LONG/X LONG,LNG_XLNG,T,55L,55L_2_LNG_XLNG_T.pdf,55L_WP_SD_S_Data Set 9,UK/AUS/NZ • 2,EUR • 30,CAN/US • 00,
4,32,0,152/176-76-84,X SHORT,XSHT,T,55L,55L_4_XSHT_T.pdf,55L_WP_SD_S_Data Set 10,UK/AUS/NZ • 4,EUR • 32,CAN/US • 0,RU • 152/176-76-84
4,32,0,152/176-76-84,SHORT,SHT,T,55L,55L_4_SHT_T.pdf,55L_WP_SD_S_Data Set 11,UK/AUS/NZ • 4,EUR • 32,CAN/US • 0,RU • 152/176-76-84'''
var lines = csv.split("\n");
// MAIN -------------------------------------------------------------
// make character styles
var FONT1 = make_style("font1", "ArialMT", 5.5);
var FONT2 = make_style("font2", "Verdana", 5.5);
var FONT3 = make_style("font3", "TimesNewRomanPSMT", 6.3);
// process lines
for (var i=1; i<lines.length; i++) {
var data = get_data_from(lines[i]);
make_layer(data.name)
var text = make_text(data.contents);
apply_styles(text);
put_in_center(text);
}
// END
// functions --------------------------------------------------------
function make_style(style_name, font_name, size) {
// try to add a new style
try { var style = app.activeDocument.characterStyles.add(style_name) }
// or pick a style with the same name if it exists already
catch(e) { var style = app.activeDocument.characterStyles.getByName(style_name) }
style.characterAttributes.size = size;
style.characterAttributes.textFont = textFonts.getByName(font_name);
return style;
}
function get_data_from(line) {
var arr = line.split(",");
var L5 = arr[4];
var Fname = arr[8].replace(".pdf", "");
var VL1 = arr[10];
var VL2 = arr[11];
var VL3 = arr[12];
var VL4 = arr[13];
return {"name":Fname, "contents":[VL1, VL2, VL3, VL4, L5]};
}
function make_layer(layer_name) {
var new_layer = app.activeDocument.layers.add();
new_layer.name = layer_name;
}
function make_text(array) {
var text = app.activeDocument.textFrames.add();
text.contents = array.join("\n");
return text;
}
function apply_styles(text) {
// not the best piece of code, I'm sure it can be done better
text.textRange.paragraphAttributes.justification = Justification.CENTER;
FONT1.applyTo(text.textRange);
var chars = text.textRange.characters;
for (var i=0; i<chars.length; i++) {
var ch = chars[i];
if (ch.contents == "•") {
FONT2.applyTo(ch);
i++;
i++;
for (var j=i; j<chars.length; j++) {
ch = chars[j];
if (ch.contents != "\r") {
FONT3.applyTo(ch);
continue;
}
i=j;
break;
}
}
}
}
function put_in_center(obj) {
var rect = app.activeDocument.artboards[0].artboardRect;
var page_w = rect[2] - rect[0];
var page_h = rect[1] - rect[3];
var shift_x = page_w/2 - obj.width/2;
var shift_y = -page_h/2 + obj.height/2;
obj.position = [rect[0] + shift_x, rect[1] + shift_y];
}
Update
To read csv data from a file you need to replace this part:
var csv = '''
...
...
... '''
with:
var csv_file = File("c:/temp/data.csv"); // <-- here is the full path to the csv file
csv_file.open("r")
var csv = csv_file.read();
csv_file.close()
Or you can use openDialog()
method if you want to select csv file via Open dialog:
var csv_file = File.openDialog();
csv_file.open("r")
var csv = csv_file.read();
csv_file.close()