Skip to main content
Known Participant
April 14, 2021
Answered

Resize Object with Variable Data

  • April 14, 2021
  • 2 replies
  • 4548 views

Hi there,

 

Is there any way to resize objects with variable data in Illustrator? I have a pdf file with rectangles. I want to change rectangle dimensions with variable data. Is this possible to do? 

 

Thanks!

This topic has been closed for replies.
Correct answer Disposition_Dev

That would be great if you can help with select dialog. I'll be waiting, thank you very much again!


To get the csv file at runtime with a modal dialog, use this:

 

var csvFile = File.openDialog("Select CSV File");

 

To convert mm to points, use this:

function mmToPt(mm)
{
    return mm * 2.834645669391;
}

var myObj = app.activeDocument.pageItems[0];
myObj.width = mmToPt(50); // set width to 50 mm
myObj.height = mmToPt(100); // set height to 100mm

 

updated code on github:

https://github.com/wdjsdev/public_illustrator_scripts/blob/master/resize_with_variable_data.js

2 replies

Disposition_Dev
Legend
April 15, 2021

As Carlos mentioned, we don't really have enough information to go on. But I figured I'd take a stab at it anyway.

 

/*
	Script Name: resize with variable data
	Author: William Dowling
	Paypal: paypal.me/illustratordev

	Description: Given a csv file, find and
		resize any objects in the document
		matching the name in the first column
		of the csv.

		Sample of expected CSV formatting:
			Name,Width,Height
			myItem1,95,85
			Second Item,120,45
			Other Item Name,50,50

*/

#target Illustrator

function resizeWithVariableData()
{
	var doc = app.activeDocument;
	var layers = doc.layers;
	


	var csvFile = File("~/Desktop/my_csv.csv");
	csvFile.open("r");
	var csvContents = csvFile.read();
	csvFile.close();

	

	var rows = csvContents.split("\n");
	var splitRow,curRect;
	for(var x=1;x<rows.length;x++)
	{
		splitRow = rows[x].split(",");
		try
		{
			curRect = doc.pageItems[splitRow[0]];
			curRect.width = splitRow[1];
			curRect.height = splitRow[2];
		}
		catch(e)
		{
			alert("Failed to find a pageItem called: " + splitRow[0]);
		}
	}
}
resizeWithVariableData();

 

https://github.com/wdjsdev/public_illustrator_scripts/blob/master/resize_with_variable_data.js

Known Participant
April 15, 2021

Hi there,

 

Thank you very much for the answer! Despite I couldn't manage to run the script, it's exaclty what I am looking for to achive.

 

To be more clear, I attached the file I'm working. You'll see 6 rectangles. Currently, I am using VariableDataImporter script to place images inside of these rectangles. What I'm trying to do is transform them, with the reference point set bottom left, then use VariableDataImporter to replace my images in resized rectangles. I think I missed something when I tried to run your script. I made this csv file (attached), then change file path on the script: 

 

var csvFile = File("/Users/tugberkdilbaz/Desktop/abc.csv");

 

Maybe this is where I get stuck. I'm using Mac. I copied file path with "Copy as Path Name" command. It didn't seem to work. Thanks again for your help!

Disposition_Dev
Legend
April 15, 2021

Did you get an error? Or were the results simply not what you expected? what happened during/after the script? Are you sure there's a rectangle in the file called "img1"?

 

is that csv file the exact file you're using? if so, you need to have a "title row" like in my example. the first line should describe what's going to be in each column. This isn't strictly necessary for the functionality to work, but it's good practice so that when you look at your CSV later, you'll still remember what the columns represent.. I think the problem is that the loop that parses the CSV data is never executing because it starts at the second row (in order to skip the title row), and you don't have a second row, so the loop condition is never true.

CarlosCanto
Community Expert
Community Expert
April 15, 2021

please describe what you need in more detail. Post screen shots or better yet sample files.