Script to add background layer along with import/initialize variables process
Hi! I'm extremely new to scripting for Illustrator, and to JS in general, but here's my use case: I write documentation in DITA, which gets translated into multiple languages. In order to translate image text, we wrap images <img> and text <p> inside <fig> tags, and use scripts to import and initialize those variables. Each language is shown in the Variables window as a data set, so we can manually check to make sure everything looks right in all languages. These images are all black-and-white line drawings, and are imported to a single layer ("Layer 1").
We've started running into issues with transparency--Illustrator displays everything against a white background, but the artboard is actually transparent, so unless you set a fill for your objects or manually place a white background layer, your objects don't display well against darker backgrounds. We've got thousands of images, and it's impossible to fix this manually. I'm looking to modify our import/initialize scripts to create a rectangle with a white fill (#FFFFFF) and empty stroke, using the same dimensions as the <Linked File> variable, and send that shape to the back, either within the existing layer, or by creating a new layer.
importVariables();
initVariables();
function importVariables() {
#target illustrator
//var variableLibraryDir = "W:\\xina\\svg-variable-libraries";
var variableLibraryDir = "\\\\ns-ni1\\xina\\variable-libraries";
//var variableLibraryDir = "C:\\Users\\dbadger\\Desktop\\Shure SVG";
var doc = app.activeDocument;
var docname = (doc.name.split('.'))[0]; // name without extension
var varLib = variableLibraryDir + "\\" + docname + ".xml";
doc.importVariables(File(varLib));
}
function initVariables() {
#target illustrator
var doc = app.activeDocument;
var docname = (doc.name.split('.'))[0]; // name without extension
var extension = (doc.name.split('.'))[1];
//user must open variable library manually the first time
var vars = doc.variables;
var flipMatrix = app.getScaleMatrix(100,-100);
var artBoardPosition = doc.artboards[0].rulerOrigin;
for (var i = 0; i < vars.length; i++){
var type = vars[i].kind;
var pageItems = vars[i].pageItems.length;
if(pageItems == 0){
if(type == VariableKind.TEXTUAL){
//create textobject and associate with this variable
var textRef = doc.textFrames.add();
textRef.position = artBoardPosition;
textRef.contentVariable = vars[i];
//doc.textFrames.appliedFont = app.fonts.item(NimbusSansGlobal);
}
else if(type == VariableKind.IMAGE){
//create graphic object and associate with this variable
var graphicRef = doc.placedItems.add();
graphicRef.position = artBoardPosition;
var graphicPath = vars[i];
graphicRef.contentVariable = graphicPath;
//need to flip vertical - Illustrator Bug?
graphicRef.transform(flipMatrix);
}
else{}
}
}
var ds = doc.dataSets;
for (var j = 0; j < ds.length; j++){
var thisLang = ds[j].name;
if(thisLang == 'en-US' || thisLang == 'ENG'){
ds[j].display();
redraw();
}
}
var textFrames = doc.textFrames;
var placedItems = doc.placedItems;
var alertString = "The following objects are not associated with a variable: \n";
for (var i = 0; i < textFrames.length; i++){
if(textFrames[i].contentVariable == undefined){
alertString = alertString + "\n" + "text: " + textFrames[i].contents;
}
}
for (var i = 0; i < placedItems.length; i++){
if(placedItems[i].contentVariable == undefined){
alertString = alertString + "\n" + "image: " + placedItems[i].file;
}
}
if(alertString != "The following objects are not associated with a variable: \n"){
alert(alertString);
}}
