Highlighted

Need help to create vector shapes with CSV [WIDTH x HEIGHT] data

Explorer ,
Aug 20, 2020

Copy link to clipboard

Copied

Can anyone tell me if there is a way to generate multiple shapes simultaneously using WIDTH x HEIGHT from a CSV worksheet of dimensions? Similar to the way varaiable data is used in Adobe Illustrator. The attached image describes a more detailed process with letters and saving but I'd like to just find a way to accomplish making the shapes. I basiclly have a list of W x H dimensions and need way to generate a lot of boxes with a single task. I only need to process around 10 shapes at a time but I need to do about 5K shapes all with different dimensions. Entering every value manually will take forever and I was hoping to automate this task. 

 

 

 

Screen Shot 2020-08-16 at 1.26.58 PM.JPG

Adobe Community Professional
Correct answer by CarlosCanto | Adobe Community Professional

...added Letters to sillyV's script

createRectangleFromCSV.PNG

function test () {
	var COL_NAME = 0;
	var COL_WIDTH = 1;
	var COL_HEIGHT = 2;
    var COL_CHAR = 3;
    
	var OUTPUT_DIR = "~/Desktop/Ai Forums Test 08212020";
    Folder(OUTPUT_DIR).create();
    
	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;
	for (var i = 0; i < csvData.length; i++) {
		thisRow = csvData[i];
		csvData[i] = thisRow.split(",");
	}
	var workDoc = app.documents.add();
	var thisRecord, w, h, name, newRect, c;
	var opts = new PDFSaveOptions();
    
    var tframe = workDoc.textFrames.add();

	for (var i = 1; i < csvData.length; i++) {
		thisRecord = csvData[i];
		name = thisRecord[COL_NAME];
		w = thisRecord[COL_WIDTH] * 72;
		h = thisRecord[COL_HEIGHT] * 72;
         c = thisRecord[COL_CHAR];
         
		newRect = workDoc.pathItems.rectangle(0, 0, w, h);
         newRect.move(tframe, ElementPlacement.PLACEAFTER);
		newRect.selected = true;
		workDoc.fitArtboardToSelectedArt(0);
		newRect.stroked = false;
        
         tframe.contents = c;
         tframe.textRange.characterAttributes.size = 48;
         tframe.textRange.characterAttributes.fillColor = workDoc.swatches.getByName("White").color;
         tframe.position = [newRect.left + newRect.width/2 - tframe.width/2, newRect.top - newRect.height/2 + tframe.height/2];
         
		newRect.fillColor = workDoc.swatches.getByName("Black").color;
		workDoc.saveAs(File(OUTPUT_DIR + "/" + name + ".pdf"), opts);
		workDoc.pathItems[0].remove();
	}
	workDoc.close(SaveOptions.DONOTSAVECHANGES);
};
test();

 

Topics

Draw and design, How to, Scripting

Views

133

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

Need help to create vector shapes with CSV [WIDTH x HEIGHT] data

Explorer ,
Aug 20, 2020

Copy link to clipboard

Copied

Can anyone tell me if there is a way to generate multiple shapes simultaneously using WIDTH x HEIGHT from a CSV worksheet of dimensions? Similar to the way varaiable data is used in Adobe Illustrator. The attached image describes a more detailed process with letters and saving but I'd like to just find a way to accomplish making the shapes. I basiclly have a list of W x H dimensions and need way to generate a lot of boxes with a single task. I only need to process around 10 shapes at a time but I need to do about 5K shapes all with different dimensions. Entering every value manually will take forever and I was hoping to automate this task. 

 

 

 

Screen Shot 2020-08-16 at 1.26.58 PM.JPG

Adobe Community Professional
Correct answer by CarlosCanto | Adobe Community Professional

...added Letters to sillyV's script

createRectangleFromCSV.PNG

function test () {
	var COL_NAME = 0;
	var COL_WIDTH = 1;
	var COL_HEIGHT = 2;
    var COL_CHAR = 3;
    
	var OUTPUT_DIR = "~/Desktop/Ai Forums Test 08212020";
    Folder(OUTPUT_DIR).create();
    
	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;
	for (var i = 0; i < csvData.length; i++) {
		thisRow = csvData[i];
		csvData[i] = thisRow.split(",");
	}
	var workDoc = app.documents.add();
	var thisRecord, w, h, name, newRect, c;
	var opts = new PDFSaveOptions();
    
    var tframe = workDoc.textFrames.add();

	for (var i = 1; i < csvData.length; i++) {
		thisRecord = csvData[i];
		name = thisRecord[COL_NAME];
		w = thisRecord[COL_WIDTH] * 72;
		h = thisRecord[COL_HEIGHT] * 72;
         c = thisRecord[COL_CHAR];
         
		newRect = workDoc.pathItems.rectangle(0, 0, w, h);
         newRect.move(tframe, ElementPlacement.PLACEAFTER);
		newRect.selected = true;
		workDoc.fitArtboardToSelectedArt(0);
		newRect.stroked = false;
        
         tframe.contents = c;
         tframe.textRange.characterAttributes.size = 48;
         tframe.textRange.characterAttributes.fillColor = workDoc.swatches.getByName("White").color;
         tframe.position = [newRect.left + newRect.width/2 - tframe.width/2, newRect.top - newRect.height/2 + tframe.height/2];
         
		newRect.fillColor = workDoc.swatches.getByName("Black").color;
		workDoc.saveAs(File(OUTPUT_DIR + "/" + name + ".pdf"), opts);
		workDoc.pathItems[0].remove();
	}
	workDoc.close(SaveOptions.DONOTSAVECHANGES);
};
test();

 

Topics

Draw and design, How to, Scripting

Views

134

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
Adobe Community Professional ,
Aug 20, 2020

Copy link to clipboard

Copied

I don't think you can generate a rectangle with specific WxH using Variable Data. But you can using Scripting. If you know how to program and want to write a script yourself, I can point you where to download the documentation.

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...
Explorer ,
Aug 21, 2020

Copy link to clipboard

Copied

Thx Carlos

I don't know how to script. I've spent a lifetime creating artwork in Adobe's GUI world so all my experience is pretty surface visuals. I'm handy enough to come up with workarounds and smart/dangerous enough not to try too often. This is something that is over my head.

 

If you can point me in any direction it would be greatly appreciated. Not sure what to search to find a solution and Frankenstein something simple for myself.

 

I've tried:

  • Asking in forums like AutoCAD and print production software from ESKO.com.
  • Tried macros in Excel 
  • Searched for a solution in Javascript
  • Searched for a similar solution on a stock site like codecanyon.net to see if someone was doing a similar task I could purchase. 

 

I even stalked you read your posts and found your script for doing page numbering in illustrator [PS can I exchange numbers for letters] to incorporate into this project.

 

I found a simple code for building SVG shapes online [see code below] from w3schools.com but don't understand how to build an interface to easily cut and paste the W x H data to generate the boxes.

 

I've found bits and pieces but I don't know what to search to get the results. Surprised I'm the only one who needs to do something like this.

 

<html>
<body>

<svg width="20" height="50">
<rect width="20" height="30" style="fill:cmyk(0,0,0,0)" />
Sorry, your browser does not support inline SVG.
</svg>

<svg width="20" height="50">
<rect width="20" height="30" style="fill:cmyk(0,0,0,0)" />
Sorry, your browser does not support inline SVG.
</svg>

<svg width="20" height="50">
<rect width="20" height="30" style="fill:cmyk(0,0,0,0)" />
Sorry, your browser does not support inline SVG.
</svg>

</body>
</html>

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...
Adobe Community Professional ,
Aug 21, 2020

Copy link to clipboard

Copied

Here is a simple example which you or someone else could edit or use as starting point.

 

#target illustrator
function test () {
	var COL_NAME = 0;
	var COL_WIDTH = 1;
	var COL_HEIGHT = 2;
	var OUTPUT_DIR = "~/Desktop/Ai Forums Test 08212020";
	var csvFile = File.openDialog("Choose CSV file.", "*.csv");
	var csvContents = "";
	if (csvFile) {
		csvFile.open('r');
		csvContents = csvFile.read();
		csvFile.close();
	} else {
                return;
        }
	var csvData = csvContents.split(/[\r\n]+/g);
	var thisRow;
	for (var i = 0; i < csvData.length; i++) {
		thisRow = csvData[i];
		csvData[i] = thisRow.split(",");
	}
	var workDoc = app.documents.add();
	var thisRecord, w, h, name, newRect;
	var opts = new PDFSaveOptions();
	for (var i = 1; i < csvData.length; i++) {
		thisRecord = csvData[i];
		name = thisRecord[COL_NAME];
		w = thisRecord[COL_WIDTH] * 72;
		h = thisRecord[COL_HEIGHT] * 72;
		newRect = workDoc.pathItems.rectangle(0, 0, w, h);
		newRect.selected = true;
		workDoc.fitArtboardToSelectedArt(0);
		newRect.stroked = false;
		newRect.fillColor = workDoc.swatches.getByName("Black").color;
		workDoc.saveAs(File(OUTPUT_DIR + "/" + name + ".pdf"), opts);
		workDoc.pathItems[0].remove();
	}
	workDoc.close(SaveOptions.DONOTSAVECHANGES);
};
test();

 

It uses the following data csv:

Silly-V_0-1598028670641.png

And it makes the following pdfs:

Silly-V_1-1598028756863.png

 

 

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...
Explorer ,
Aug 21, 2020

Copy link to clipboard

Copied

Thanks Silly-V

This look amazing... I see you even converted it from inches to pixels which is great. I actually did that on my excel sheet but if the script does that, even better. Can I just use the script in my script editor and save it to test or do I need to modify it? Should I take out any parts to make it save? I tried just saving it and it said "Expected end of line, etc. but found identifier."

 

This simple thing you did is still over my head. I tried testing it in shellcheck.net but it was telling me to "Add a Shebang or 'shell' Directive"

 

 

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...
Adobe Community Professional ,
Aug 21, 2020

Copy link to clipboard

Copied

If you can copy the text into notepad and save as .jsx, you can go from Illustrator File > Scripts > Other Scripts and choose this .jsx file wherever you saved it. Then it should run. I just tried to do this and it worked for me.
You should also be able to run it from the old ESTK or the VSCode Adobe plugin just fine.

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...
Explorer ,
Aug 23, 2020

Copy link to clipboard

Copied

This ran perfectly. Thanks again!

Had a follow-up question to the script. When the script asks to select the CSV file, how are the artboard specs defined? I see the script is using the W x H to create a rectangle and then using the
menu command to fitArtboardToSelectedArt.

 

Can the file specs be set to inches rather than pixels so you don't need to calculate from pixels to inches? 

w = thisRecord[COL_WIDTH] * 72;
h = thisRecord[COL_HEIGHT] * 72;

Could the bleed area also be turned on to include a .25 bleed around the files?

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...
Adobe Community Professional ,
Aug 21, 2020

Copy link to clipboard

Copied

...added Letters to sillyV's script

createRectangleFromCSV.PNG

function test () {
	var COL_NAME = 0;
	var COL_WIDTH = 1;
	var COL_HEIGHT = 2;
    var COL_CHAR = 3;
    
	var OUTPUT_DIR = "~/Desktop/Ai Forums Test 08212020";
    Folder(OUTPUT_DIR).create();
    
	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;
	for (var i = 0; i < csvData.length; i++) {
		thisRow = csvData[i];
		csvData[i] = thisRow.split(",");
	}
	var workDoc = app.documents.add();
	var thisRecord, w, h, name, newRect, c;
	var opts = new PDFSaveOptions();
    
    var tframe = workDoc.textFrames.add();

	for (var i = 1; i < csvData.length; i++) {
		thisRecord = csvData[i];
		name = thisRecord[COL_NAME];
		w = thisRecord[COL_WIDTH] * 72;
		h = thisRecord[COL_HEIGHT] * 72;
         c = thisRecord[COL_CHAR];
         
		newRect = workDoc.pathItems.rectangle(0, 0, w, h);
         newRect.move(tframe, ElementPlacement.PLACEAFTER);
		newRect.selected = true;
		workDoc.fitArtboardToSelectedArt(0);
		newRect.stroked = false;
        
         tframe.contents = c;
         tframe.textRange.characterAttributes.size = 48;
         tframe.textRange.characterAttributes.fillColor = workDoc.swatches.getByName("White").color;
         tframe.position = [newRect.left + newRect.width/2 - tframe.width/2, newRect.top - newRect.height/2 + tframe.height/2];
         
		newRect.fillColor = workDoc.swatches.getByName("Black").color;
		workDoc.saveAs(File(OUTPUT_DIR + "/" + name + ".pdf"), opts);
		workDoc.pathItems[0].remove();
	}
	workDoc.close(SaveOptions.DONOTSAVECHANGES);
};
test();

 

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...
Explorer ,
Aug 23, 2020

Copy link to clipboard

Copied

Thanks so much Carlos. I took a look at the code to help me understand how you inserted the page numbering. I was able to increase the type size.

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...
Explorer ,
Aug 23, 2020

Copy link to clipboard

Copied

In the script, the artboard is defined by the newRect shape from the CSV file which is exactly what I need. Could I also like to insert another action [like Carlos did with the labels] to increase the newRect shape by .5 inches to the width and height just before the file is saved? This would make the shape bleed off the edge by .25
inches on every side.

 

Could I insert the code after this line:

 

newRect.fillColor = workDoc.swatches.getByName("Black").color;

 

Then use this to select the shape:

newRect.selected = true;

Not sure what to write after this increase the dimensions.  I tried this:

 

		newRect.width = +.5;
		newRect.height = +.5;

 

...but it just trasformed the shapes into tiny 5 x 5 pixel boxes. 

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...
Adobe Community Professional ,
Aug 23, 2020

Copy link to clipboard

Copied

do you want to increase the rectangle size by 1/2" but keep the artboard size as is? or do you want to increase the artboard as well?

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...
Explorer ,
Aug 24, 2020

Copy link to clipboard

Copied

Just the rectangle size. The idea is to have artboard one size and rect 1/2" larger. The rect will extend and bleed off the artboard.

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...
Explorer ,
Aug 28, 2020

Copy link to clipboard

Copied

Hey Silly-V + Carlos,

I just marked this thread as solved. Thanks again for your help.

Let me know if there's an easy way to increase the size of the shape on the artboard. I wasn't sure how to reference the shape to increase it.

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...
Adobe Community Professional ,
Aug 29, 2020

Copy link to clipboard

Copied

While it's possible to do this with just mathematically editing the artboard's artboardRect coordinates to get the fastest results, I wanted to revisit the liveEffectXml method of making offset paths since unfortunately a lot of old thread links are broken and I want to bump this feature for fun. Luckily I was able to find an old one that shows the offset path code!

	function makeOffsetPathWithEffect (value, Path) {
		var PathCopy = Path.duplicate();
		var offsetvalueinput = value;
		var liveXmlString = "<LiveEffect name='Adobe Offset Path'><Dict data='R mlim 4 R ofst " + offsetvalueinput + "  I jntp 2 '/></LiveEffect>";
		PathCopy.applyEffect(liveXmlString);
		app.activeDocument.selection = null;
		PathCopy.selected = true;
		app.executeMenuCommand("expandStyle");
		return app.activeDocument.selection[0];
	};

	var offsetRect = makeOffsetPathWithEffect(-0.25 * 72, app.activeDocument.pageItems[0]);

In this snippet you can see the function which you only need to paste at some point in your javascript code, and then you can call it similar to my example. The example creates a negative offset, but you can take away the minus to make it a positive. After this you would size the artboard to the offset copy and then remove the offset copy.

In the latest code , this is an example of how the function can be called inside there. I'm not sure what is being done with the stroke, so adjust as you will.

newRect = workDoc.pathItems.rectangle(0, 0, w, h);
newRect.move(tframe, ElementPlacement.PLACEAFTER);
newRect.stroked = false;
var rectOffsetCopy = makeOffsetPathWithEffect(0.2 * 72, newRect); // a 1/5th inch add
workDoc.fitArtboardToSelectedArt(0);
rectOffsetCopy.remove();

 

 

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...
Explorer ,
Aug 29, 2020

Copy link to clipboard

Copied

This is great...except it's doing the opposite of what I need. The code is making the artboard larger than the rect object. Is there a way to reverse the results and make the rect larger than the artboard? The rect should be an additional .25" from the dimensions on the CSV. That would make the rect bleed off the edges of the artboard by .25". I tried changing several things including the (-) and (+) of the .25" but I couldn't get it to config corectly. Here's what I have:

 

#target illustrator
function test () {
	var COL_NAME = 0;
	var COL_WIDTH = 1;
	var COL_HEIGHT = 2;
   	var COL_CHAR = 3;
    
	var OUTPUT_DIR = "~/Desktop/Artboards";
    Folder(OUTPUT_DIR).create();
    
	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;
	for (var i = 0; i < csvData.length; i++) {
		thisRow = csvData[i];
		csvData[i] = thisRow.split(",");
	}
	var workDoc = app.documents.add();
	var thisRecord, w, h, name, newRect, c;
	var opts = new PDFSaveOptions();
    
    var tframe = workDoc.textFrames.add();

	for (var i = 1; i < csvData.length; i++) {
		thisRecord = csvData[i];
		name = thisRecord[COL_NAME];
		w = thisRecord[COL_WIDTH] * 72;
		h = thisRecord[COL_HEIGHT] * 72;
         c = thisRecord[COL_CHAR];
         
	function makeOffsetPathWithEffect (value, Path) {
		var PathCopy = Path.duplicate();
		var offsetvalueinput = value;
		var liveXmlString = "<LiveEffect name='Adobe Offset Path'><Dict data='R mlim 4 R ofst " + offsetvalueinput + "  I jntp 2 '/></LiveEffect>";
		PathCopy.applyEffect(liveXmlString);
		app.activeDocument.selection = null;
		PathCopy.selected = true;
		app.executeMenuCommand("expandStyle");
		return app.activeDocument.selection[0];
	};

	var offsetRect = makeOffsetPathWithEffect(-0.25 * 72, app.activeDocument.pageItems[0]);

newRect = workDoc.pathItems.rectangle(0, 0, w, h);
newRect.move(tframe, ElementPlacement.PLACEAFTER);
newRect.stroked = false;
var rectOffsetCopy = makeOffsetPathWithEffect(0.25 * 72, newRect); // a 1/4 inch add
workDoc.fitArtboardToSelectedArt(0);
rectOffsetCopy.remove();

         tframe.contents = c;
         tframe.textRange.characterAttributes.size = 120;
         tframe.textRange.characterAttributes.fillColor = workDoc.swatches.getByName("White").color;
         tframe.position = [newRect.left + newRect.width/2 - tframe.width/2, newRect.top - newRect.height/2 + tframe.height/2];

		newRect.fillColor = workDoc.swatches.getByName("Black").color;
		workDoc.saveAs(File(OUTPUT_DIR + "/" + name + ".pdf"), opts);
		workDoc.pathItems[0].remove();
	}
	workDoc.close(SaveOptions.DONOTSAVECHANGES);
};
test();

 

 

 

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...
Silly-V LATEST
Adobe Community Professional ,
Aug 30, 2020

Copy link to clipboard

Copied

Using the -0.25 value should do what you want and shrink the copied rectangle so that when the next lines size down the artboard, it should be 0.25 smaller than the original rectangle.

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...