Copy link to clipboard
Copied
Hey Guys
I'm pretty new to all this so please forgive my ignorance I'm currently working on a project using java Variable-importer, Create Command Layer and Recolor scripts to create the needed artwork from a excel spreadsheet but the text on the artwork needs to be different fonts and I currently have to manually change all the fonts and cant seem to figure this part out. I was wondering if it was even possible to change the font of text using a variable in illustrator with scripting?
This looks good, let's invent a new convention to have the "_text" and "_font" go after variable names. Have those appear after the variable columns you wish, but ensure that the name part of the column name is spelled the same. The method will be to keep working along the same lines as the recolor workflow but use a new script to also post-process the datasets during your batch action.
But before doing much, we must establish the special computer-friendly font-names that your computer uses so
...Copy link to clipboard
Copied
It's possible with custom script that you also run at data-set processing time.
But first do you have any screenshots of what kind of art you have that needs to be variable?
Copy link to clipboard
Copied
Hey Silly-V
I hope this is what your asking for. Here is the artwork and the data sheet I am using. Every text box can be a different font but at the moment its only using the default font the template I use is set to.
Copy link to clipboard
Copied
var myFrame = [the desired textFrame object];
myFrame.textRange.characterAttributes.textFont = app.textFonts["Name of font"];
Copy link to clipboard
Copied
This looks good, let's invent a new convention to have the "_text" and "_font" go after variable names. Have those appear after the variable columns you wish, but ensure that the name part of the column name is spelled the same. The method will be to keep working along the same lines as the recolor workflow but use a new script to also post-process the datasets during your batch action.
But before doing much, we must establish the special computer-friendly font-names that your computer uses so that these can be adhered to in your spreadsheets going forward. Run this script to output a text file to your desktop which lists all of the font names as Illustrator would like to see them.
#target illustrator
function test(){
var arr = [];
var destFile = File("~/Desktop/My-Illustrator-Scripting-Text-Fonts.txt");
for(var i = 0; i < app.textFonts.length; i++){
arr.push(app.textFonts[i].name);
}
destFile.open("w");
destFile.write(arr.join("\n"));
destFile.close();
destFile.execute();
};
test();
Just like in the Recolor workflow, your textbox containing your font's name will need to be present in the document but hidden away to not be seen as visual art.
Here's a sample font-styling script based on the assumption you are doing binding of variables by name.
#target illustrator
function test(){
var doc = app.activeDocument;
var thisVar, thisTextVar, thisTextFrame;
for (var i = 0; i < doc.variables.length; i++) {
thisVar = doc.variables[i];
if (thisVar.name.indexOf("_font") > -1) {
try {
thisTextVar = doc.variables.getByName(thisVar.name.replace("_font", "_text"));
for (var j = 0; j < thisTextVar.pageItems.length; j++) {
thisTextFrame = thisTextVar.pageItems[j];
thisTextFrame.textRange.characterAttributes.textFont = app.textFonts.getByName(thisVar.pageItems[0].contents);
};
} catch (e) {
// do nothing
}
}
};
};
test();
Copy link to clipboard
Copied
Hi Silly-V,
I was curious if there is a way to add more 'Fields' in the 'Assign Dataset Names' window of the VariableImporter script?
Ideally, some of my uses may need 8 or 9 'Fields' assigned for file name and distribution purposes.
Thank you!
Copy link to clipboard
Copied
Hi, @Silly-V
i just test your script to change font, but i get error 1302:no such element,
Can you help please,
Thanks
Copy link to clipboard
Copied
Hi, please try it now - I have edited the answer.
There was a forum update that removed some bits of code from code which was pasted into answers, so we take care of these one-at-a-time if that was our thing and people find these and say they aren't working.
I will probably go back here to copy and paste the above statement many times as folks try some of these snippets out there, but meanwhile I hope it's back working for you.
Copy link to clipboard
Copied
Thank you, @Silly-V